Skip to contentSkip to conversation

: the *_bit routines are broken for systems with sizeof(long) != 4

ALSA - tools 0001533 [email protected] 1 participant 1 message Nov 7, 2005

lore
Share & tools
lore
#0 lore raw

The following issue has been SUBMITTED.
======================================================================
<https://bugtrack.alsa-project.org/alsa-bug/view.php?id=1533>
======================================================================
Reported By: Hugh
Assigned To:
======================================================================

Project:                    ALSA - tools
Issue ID:                   1533
Category:                   ld10k1
Reproducibility:            have not tried
Severity:                   major
Priority:                   normal
Status:                     new
======================================================================
Date Submitted:             11-07-2005 22:02 CET
Last Modified:              11-07-2005 22:02 CET

======================================================================
Summary: the *_bit routines are broken for systems with
sizeof(long) != 4
Description:
These problems were discovered by reading the code. No testing has been done. The code in question is in
http://cvs.sourceforge.net/viewcvs.py/alsa/alsa-tools/ld10k1/src/bitops.h?rev=1.2&view=log

I have made a number of fixes to the code. Please find attached the new version. Of course you should be wary of untested code. Do read it and understand what I've done and why this fixes several bugs.

CHANGES:

The shift right of nr to calculate the subscript of addr was by sizeof(long) + 1. The correct amount to shift by would be log2(sizeof(unsigned long) * 8). It is awkward to calculate this value in a compile-time constant.
Since gcc is smart enough to implement division or remainder of an unsigned by a power-of-two constant using shift or mask, I changed the code to use these clearer operations.

Each parameter "nr" changed from int to unsigned.
This more correctly reflects the fact that it must not be negative. (If it were supposed to allow negative numbers, it ought to have been of type ptrdiff_t since int might not work in systems where sizeof(int) < sizeof(void *).)
It makes bitwise operations on nr more sensible.
It allows the compiler to convert division-by-power-of-2 to shift.

Each local variable "mask" changed from int to unsigned long. This matches the type of *addr, with which it will be combined. This prevents overflow when it is computed.

Each assignment to mask was done with "1" shifted left. This needs to be done in unsigned long, so "1UL" is now used.

Add "const" to type of *addr int test_bit (not a bug fix).

Fixed the comments to be accurate.

======================================================================

Issue History
Date Modified Username Field Change
======================================================================
11-07-05 22:02 Hugh New Issue
11-07-05 22:02 Hugh File Added: bitops.h
======================================================================

-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download it for free - -and be entered to win a 42" plasma tv or your very own Sony(tm)PSP. Click here to play: http://sourceforge.net/geronimo.php