[En-Nut-Discussion] ICC printf wierdness - inet_ntoa, Traps for young players
Brett Abbott
Brett.Abbott at digital-telemetry.com
Wed Jun 30 22:18:37 CEST 2004
Joe,
inet_ntoa is not thread safe as it uses local memory (char[16]) to build the string to be returned. As it is only returning the pointer, the next time inet_ntoa is called, the memory holding the string is overwritten. ICC evaluates all of the functions in the passed parameters before calling inet_ntoa.
The pragmatic solution here is to either adopt your first code example or perhaps something like:
u_char IP1[16];
strcpy(IP3, inet_ntoa(confnet.cdn_ip_addr)); // Fetch the code before I get it.
printf("addr %s mask %s\n", IP1, inet_ntoa(confnet.cdn_ip_mask));
There of course may be a more elegant way. Im interested to know if gcc works the same way.
Have a look at the inet_ntoa source in net/inet.c, it is quite self explanatory.
A caution for its use however. If you have interupts or device drivers (called by interupts) that use this function, you really need to modify the function to take a char* pointer to your own local parameter and have it place the results in there. The current function works fine normally but if an interupt function uses inet_ntoa half way through your printf call, you are likely to get corruption.
Cheers
Brett
-----------------------------------------------------------------
Brett Abbott, Managing Director, Digital Telemetry Limited
Email: Brett.Abbott at digital-telemetry.com
PO Box 24 036 Manners Street, Wellington, New Zealand
Phone +64 (4) 5666-860 Mobile +64 (21) 656-144
------------------- Commercial in confidence --------------------
Message: 4
Date: Wed, 30 Jun 2004 11:19:14 -0600
From: "Joe Doubek" <jdoubek at control4.com>
Subject: [En-Nut-Discussion] ICC printf wierdness
To: <en-nut-discussion at egnite.de>
Message-ID:
<B4585452AFA70C49BDD355A79291DE4445BEF8 at c4exchange.control4.com>
Content-Type: text/plain; charset="us-ascii"
Hello all... I've have a puzzler under ICC... when I have an IP address
of 10.11.11.234, any ideas why:
printf("addr %s ", inet_ntoa(confnet.cdn_ip_addr));
printf("mask %s\n", inet_ntoa(confnet.cdn_ip_mask));
prints out:
addr 10.11.11.234 mask 255.255.0.0
whereas:
printf("addr %s mask %s\n", inet_ntoa(confnet.cdn_ip_addr),
inet_ntoa(confnet.cdn_ip_mask));
prints out:
addr 255.255.0.0 mask 255.255.0.0
strange hmm?
-Joe
--
-----------------------------------------------------------------
Brett Abbott, Managing Director, Digital Telemetry Limited
Email: Brett.Abbott at digital-telemetry.com
PO Box 24 036 Manners Street, Wellington, New Zealand
Phone +64 (4) 5666-860 Mobile +64 (21) 656-144
------------------- Commercial in confidence --------------------
More information about the En-Nut-Discussion
mailing list