Re: sprintf and 64-bit integers
On 9/14/09 9:54 PM, Andrew Farmer said: >> Yes, sprintf is pure evil. snprintf is less evil. >> >> Also, I recommend adding -fstack-protector -D_FORTIFY_SOURCE=2, they >> can >> help catch some of these problems. And valgrind would probably have >> caught this problem immediately. > >Sadly, valgrind hasn't yet been updated to run under Snow Leopard. True, but the OP said "I'm updating my code for Snow Leopard" which, I'm guessing, means he can also run on 10.5. Of course, if he's using Garbage Collection, he's also out of luck since valgrind does not support that either. -- Sean McBride, B. Eng s...@rogue-research.com Rogue Researchwww.rogue-research.com Mac Software Developer Montréal, Québec, Canada ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: sprintf and 64-bit integers
On 14 Sep 2009, at 11:48, Sean McBride wrote: On 9/13/09 12:01 PM, Jens Alfke said: It would be best to convert all your sprintf calls to snprintf, which is a safer equivalent that won't overflow the buffer. Yes, sprintf is pure evil. snprintf is less evil. Also, I recommend adding -fstack-protector -D_FORTIFY_SOURCE=2, they can help catch some of these problems. And valgrind would probably have caught this problem immediately. Sadly, valgrind hasn't yet been updated to run under Snow Leopard. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: sprintf and 64-bit integers
On 9/13/09 12:01 PM, Jens Alfke said: >It would be best to convert all your sprintf calls to snprintf, which >is a safer equivalent that won't overflow the buffer. Yes, sprintf is pure evil. snprintf is less evil. Also, I recommend adding -fstack-protector -D_FORTIFY_SOURCE=2, they can help catch some of these problems. And valgrind would probably have caught this problem immediately. -- Sean McBride, B. Eng s...@rogue-research.com Rogue Researchwww.rogue-research.com Mac Software Developer Montréal, Québec, Canada ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: sprintf and 64-bit integers
On Sun, Sep 13, 2009 at 8:42 PM, Greg Guerin wrote: > Or just try the simple expedient: > > char str[80]; > > -- GG Right, it was a buffer overrun. Simple as that. Thanks guys! ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: sprintf and 64-bit integers
It would be best to convert all your sprintf calls to snprintf, which is a safer equivalent that won't overflow the buffer. If the value is too long to fit, it'll be truncated instead of overflowing and corrupting the stack. (This type of overflow is one of the main causes of security holes in apps.) Either that or use -[NSString stringWithFormat:] instead, if it's not especially performance-sensitive code. —Jens___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: sprintf and 64-bit integers
Sprintf() is returning the same value, using %d on 10.5. I meant sprintf()'s returned int value, which is the count of formatted output chars, not including the trailing null. Read 'man sprintf'. Or just try the simple expedient: char str[80]; -- GG ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: sprintf and 64-bit integers
In this case val was exactly nine digits: 213294334. But it can also be greater sometimes. Sprintf() is returning the same value, using %d on 10.5. On Sun, Sep 13, 2009 at 8:21 PM, Greg Guerin wrote: >> char str[10]; >> sprintf(str, "%d", val); > > What is the value of val at the time the crash occurs? Will it always > convert to 9 digits or less? > > What value is sprintf() returning? > > You might want to use snprintf() or asprintf(). > > -- GG > > ___ > > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > > Please do not post admin requests or moderator comments to the list. > Contact the moderators at cocoa-dev-admins(at)lists.apple.com > > Help/Unsubscribe/Update your Subscription: > http://lists.apple.com/mailman/options/cocoa-dev/slasktrattenator%40gmail.com > > This email sent to slasktrattena...@gmail.com > ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: sprintf and 64-bit integers
char str[10]; sprintf(str, "%d", val); What is the value of val at the time the crash occurs? Will it always convert to 9 digits or less? What value is sprintf() returning? You might want to use snprintf() or asprintf(). -- GG ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: sprintf and 64-bit integers
On Sun, Sep 13, 2009 at 8:10 PM, slasktrattena...@gmail.com wrote: > On Sun, Sep 13, 2009 at 8:01 PM, Bill Bumgarner wrote: >> On Sep 13, 2009, at 10:59 AM, slasktrattena...@gmail.com wrote: >> I'm updating my code for Snow Leopard and ran into this problem. The >>> >>> app crashes at this line: >>> >>> sprintf(str, "%d", val); >>> >>> where val is a CFIndex. According to the string programming guide here... >>> >>> >>> http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html >>> >>> ...I need to cast my CFIndex to long and replace the %d format >>> specifier to %ld. I tried that but still got the crash. So I kept >>> trying with all the format specifiers in the book, declaring my >>> variable a NSInteger, unsigned int, etc, but no matter what the app >>> kept crashing. The only that that actually worked was %lx, but then I >>> get the numbers all wrong. It seems that sprintf only accepts 32-bit >>> integers. Is this correct? If so, what's the workaround? I'm compiling >>> for both 10.5 and 10.6. Advice appreciated, thanks. >> >> You are off in the weeds. >> >> There is nothing about a value conversion that could cause a crash. Wrong >> value? Sure. But not a crash. Thus, the formatting string is *not* causing >> a crash. >> >> The problem is almost assuredly that 'str' is pointing to garbage, >> uninitialized or otherwise wrong. >> >> Post the code for how str is created. >> >> b.bum > > Sorry, str is simply created like this: > > char str[10]; > sprintf(str, "%d", val); Are you positive that val is a nine digit number? If not, then you've got a buffer overrun. This could have been failing in 32-bit as well, but you were running off the end of the buffer by a smaller amount. -- Clark S. Cox III clarkc...@gmail.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: sprintf and 64-bit integers
On Sep 13, 2009, at 11:10 AM, slasktrattena...@gmail.com wrote: On Sun, Sep 13, 2009 at 8:01 PM, Bill Bumgarner wrote: On Sep 13, 2009, at 10:59 AM, slasktrattena...@gmail.com wrote: I'm updating my code for Snow Leopard and ran into this problem. The app crashes at this line: sprintf(str, "%d", val); where val is a CFIndex. According to the string programming guide here... http://developer.apple.com/mac/library/documentation/Cocoa/ Conceptual/Strings/Articles/formatSpecifiers.html ...I need to cast my CFIndex to long and replace the %d format specifier to %ld. I tried that but still got the crash. So I kept trying with all the format specifiers in the book, declaring my variable a NSInteger, unsigned int, etc, but no matter what the app kept crashing. The only that that actually worked was %lx, but then I get the numbers all wrong. It seems that sprintf only accepts 32-bit integers. Is this correct? If so, what's the workaround? I'm compiling for both 10.5 and 10.6. Advice appreciated, thanks. You are off in the weeds. There is nothing about a value conversion that could cause a crash. Wrong value? Sure. But not a crash. Thus, the formatting string is *not* causing a crash. The problem is almost assuredly that 'str' is pointing to garbage, uninitialized or otherwise wrong. Post the code for how str is created. b.bum Sorry, str is simply created like this: char str[10]; sprintf(str, "%d", val); For a 64-bit unsigned integer, the maximum decimal value is 18446744073709551615. A quick count shows that to be 20 characters long, not including the null-terminator. Stuffing 20 characters into a local buffer is likely to trash the stack frame. steve ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: sprintf and 64-bit integers
On Sun, Sep 13, 2009 at 8:01 PM, Bill Bumgarner wrote: > On Sep 13, 2009, at 10:59 AM, slasktrattena...@gmail.com wrote: > I'm updating my code for Snow Leopard and ran into this problem. The >> >> app crashes at this line: >> >> sprintf(str, "%d", val); >> >> where val is a CFIndex. According to the string programming guide here... >> >> >> http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html >> >> ...I need to cast my CFIndex to long and replace the %d format >> specifier to %ld. I tried that but still got the crash. So I kept >> trying with all the format specifiers in the book, declaring my >> variable a NSInteger, unsigned int, etc, but no matter what the app >> kept crashing. The only that that actually worked was %lx, but then I >> get the numbers all wrong. It seems that sprintf only accepts 32-bit >> integers. Is this correct? If so, what's the workaround? I'm compiling >> for both 10.5 and 10.6. Advice appreciated, thanks. > > You are off in the weeds. > > There is nothing about a value conversion that could cause a crash. Wrong > value? Sure. But not a crash. Thus, the formatting string is *not* causing > a crash. > > The problem is almost assuredly that 'str' is pointing to garbage, > uninitialized or otherwise wrong. > > Post the code for how str is created. > > b.bum Sorry, str is simply created like this: char str[10]; sprintf(str, "%d", val); ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: sprintf and 64-bit integers
On Sep 13, 2009, at 10:59 AM, slasktrattena...@gmail.com wrote: I'm updating my code for Snow Leopard and ran into this problem. The app crashes at this line: sprintf(str, "%d", val); where val is a CFIndex. According to the string programming guide here... http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html ...I need to cast my CFIndex to long and replace the %d format specifier to %ld. I tried that but still got the crash. So I kept trying with all the format specifiers in the book, declaring my variable a NSInteger, unsigned int, etc, but no matter what the app kept crashing. The only that that actually worked was %lx, but then I get the numbers all wrong. It seems that sprintf only accepts 32-bit integers. Is this correct? If so, what's the workaround? I'm compiling for both 10.5 and 10.6. Advice appreciated, thanks. You are off in the weeds. There is nothing about a value conversion that could cause a crash. Wrong value? Sure. But not a crash. Thus, the formatting string is *not* causing a crash. The problem is almost assuredly that 'str' is pointing to garbage, uninitialized or otherwise wrong. Post the code for how str is created. b.bum ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
sprintf and 64-bit integers
Hi, I'm updating my code for Snow Leopard and ran into this problem. The app crashes at this line: sprintf(str, "%d", val); where val is a CFIndex. According to the string programming guide here... http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html ...I need to cast my CFIndex to long and replace the %d format specifier to %ld. I tried that but still got the crash. So I kept trying with all the format specifiers in the book, declaring my variable a NSInteger, unsigned int, etc, but no matter what the app kept crashing. The only that that actually worked was %lx, but then I get the numbers all wrong. It seems that sprintf only accepts 32-bit integers. Is this correct? If so, what's the workaround? I'm compiling for both 10.5 and 10.6. Advice appreciated, thanks. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com