| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Re: A Little Bug |
Hi Nhan,
NT> Just found a little gem in one of our code...
NT> #include
NT> #include
NT> #include
NT> int main()
NT> {
NT> int offset, len = 8;
NT> const char str[] = "hello, world";
NT> offset = (len - strlen(str)) / 2;
^
NEVER, EVER, do subtraction with an unsigned ! strlen() returns size_t,
which is always unsigned. Also, since size_t is often unsigned long, len
will get promoted to unsigned long & wraparound ensues.
NT> offset = max(0, offset);
^^^
Where is max() defined? If it's a macro, at least the programmer was
sensible enough to (attempt to) evaluate offset outside the macro & avoid
the ugliness of double evaluation.
NT> printf("%d\n", offset);
NT> return (0);
NT> }
NT> The programmer expected offset to be 0 in this case, but it isn't...
The moral of the story: do your arithmetic with the proper type.
Michael Stapleton of Graphic Bits.
___I'm only here for the taglines.
--- Blue Wave/RA v0.7 Q-Blue
* Origin: The Three Amigas - better than two (3:713/615.0)SEEN-BY: 54/54 99 711/401 430 807 808 809 932 934 712/627 713/111 317 601 611 SEEN-BY: 713/615 618 700 729 805 888 906 714/906 @PATH: 713/615 888 54/99 711/808 809 934 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.