Bug 13445 - division by zero
Summary: division by zero
Status: CLOSED INVALID
Alias: None
Product: Drivers
Classification: Unclassified
Component: Sound(ALSA) (show other bugs)
Hardware: All Linux
: P1 normal
Assignee: Jaroslav Kysela
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-06-03 19:28 UTC by Martin Ettl
Modified: 2009-06-29 12:02 UTC (History)
3 users (show)

See Also:
Kernel Version: 2.6.29.3
Subsystem:
Regression: No
Bisected commit-id:


Attachments

Description Martin Ettl 2009-06-03 19:28:14 UTC
Hi,

i detected a few division by zeros in file:linux-2.6.29.3/sound/soc/codecs/tlv320aic23.c
This is deteted with the static code analyis tool cppcheck. Cppcheck prints the following output:
[linux-2.6.29.3/sound/soc/codecs/tlv320aic23.c:296]: (error) Division by zero
[linux-2.6.29.3/sound/soc/codecs/tlv320aic23.c:297]: (error) Division by zero
[linux-2.6.29.3/sound/soc/codecs/tlv320aic23.c:300]: (error) Division by zero
[linux-2.6.29.3/sound/soc/codecs/tlv320aic23.c:301]: (error) Division by zero


Take a loop at the code tlv320aic23.c at line 296:

#define SR_MULT (11*12)
#define A(x) (x) ? (SR_MULT/x) : 0
static const unsigned char sr_adc_mult_table[] = {
	A(2), A(2), A(12), A(12),  A(0), A(0), A(3), A(1),
	A(2), A(2), A(11), A(11),  A(0), A(0), A(0), A(1)
};

indeed, there is a division by zero! At the lines 297, 300 and 301 happens the same!


Best regards

Ettl Martin
Comment 1 Andrew Morton 2009-06-03 21:45:46 UTC
Reassigned to ALSA.
Comment 2 Takashi Iwai 2009-06-05 05:59:55 UTC
Which compiler are you using?  As you see in the definition A(), it does zero-check before doing the division.  So, if the compiler does the right job, zero division must not happen.  Thus I suspect it's a compiler-dependent issue.

As a workaround, just replace A(0) with 0.
Comment 3 Martin Ettl 2009-06-05 07:00:08 UTC
i created the following testcase:

#define SR_MULT (11*12)
#define A(x) (x) ? (SR_MULT/x) : 0
static const unsigned char sr_adc_mult_table[] = {
    A(2), A(2), A(12), A(12),  A(0), A(0), A(3), A(1),
    A(2), A(2), A(11), A(11),  A(0), A(0), A(0), A(1)
};

int main()
{}


My compiler says:

$ g++ -o test test.cpp 
test.cpp:4: warning: division by zero
test.cpp:4: warning: division by zero
test.cpp:5: warning: division by zero
test.cpp:5: warning: division by zero
test.cpp:5: warning: division by zero

I am using g++-4.3.3 on ubuntu linux

g++ -v

Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.3.3-5ubuntu4' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.3.3 (Ubuntu 4.3.3-5ubuntu4)

Regards

Martin
Comment 4 Martin Ettl 2009-06-05 07:01:20 UTC
By the way, at linux-2.6.29.3/sound/soc/codecs/tlv320aic23.c
happens the same!


[linux-2.6.29.3/sound/soc/codecs/tlv320aic23.c:296]: (error) Division by zero
[linux-2.6.29.3/sound/soc/codecs/tlv320aic23.c:297]: (error) Division by zero
[linux-2.6.29.3/sound/soc/codecs/tlv320aic23.c:300]: (error) Division by zero
[linux-2.6.29.3/sound/soc/codecs/tlv320aic23.c:301]: (error) Division by zero
Comment 5 Takashi Iwai 2009-06-05 09:11:21 UTC
Do you get the same error with gcc, not g++?

It shouldn't give any errors.  It seems like a C++ specific issue.
Comment 6 Martin Ettl 2009-06-05 09:54:02 UTC
Indeed, its a g++ specific problem. I tested also g++-3.3, it prints the following message:

$g++-3.3 -o test test.c
test.c:4: warning: division by zero in `132 / 0'
test.c:4: warning: division by zero in `132 / 0'
test.c:5: warning: division by zero in `132 / 0'
test.c:5: warning: division by zero in `132 / 0'
test.c:5: warning: division by zero in `132 / 0'


gcc compiles without errors or warnings.

Thats strange.

Note You need to log in before you can comment on or make changes to this bug.