Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


sscanf causing core dump

Posted on 2004-10-19
Medium Priority
Last Modified: 2010-04-15
I have come across a bit of code that uses sscanf.  The function containing it is as follwos,

void f_extract_n(char *string, SINT *value, short *ind, int len )
    char    tmp[30];

    memset(&tmp, 0, 30 );
    strncpy( tmp, string, len );
printf("calling sscanf\n");
    if ( (sscanf(tmp, "%d", (unsigned int) value )) == EOF )
        *ind   = -1;
printf("sscanf ok\n");


When run under AIX 4.3(32bit) it works fine (where value in tmp is "001108") but when run under AIX 5.2(64 bit) the sscanf is core dumping (same value of tmp).  If I remove the (unsigned int) cast it works ok.  Can anyone tell me why the (unsigned int) cast is causing the core dump?
Question by:DLyall
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 46

Expert Comment

by:Kent Olsen
ID: 12346623

sscanf needs a target location to place the converted value.  In this case you're recasting an address into an integer!  C is dutifully placing the integer on the stack prior to calling sscanf() and using this value as the address to store the conversion.

I suspect that the recast to an int (32-bits?) is dropping the upper 32 bits of the 64-bit address in the *value parameter.

LVL 12

Expert Comment

ID: 12346679
Hi DLyall,
Replace (unsigned int) by (unsigned long).


LVL 46

Accepted Solution

Kent Olsen earned 2000 total points
ID: 12346795

Hi Stephan,

That is still an implementation specific solution because the pointer is being recast to an integer.  And recasting it to be a pointer to an integer (or pointer to a long) is no better because the function header explicitly declares the value to be a pointer to a short.  sscanf() could overwrite something that it shouldn't if the variable is improperly recast.

The best solution is to simply throw away the recasting and let the C compiler (and libraries) work.

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 46

Expert Comment

by:Kent Olsen
ID: 12346809

I suspect that the code should recast the return value of sscanf(), not the pointer being passed.

LVL 48

Expert Comment

ID: 12347136
Out of topic: I wonder how this is working -

char    tmp[30];
memset(&tmp, 0, 30 );         // should be tmp and not &tmp

Expert Comment

ID: 12347332
although memset(&tmp, 0, 30) is used wrongly, it can still work, only zeroing some other memory
LVL 46

Expert Comment

by:Kent Olsen
ID: 12347378

I believe that the compiler will simply disregard the unnecessarey '&'.  Depending on the compiler options, it should produce a "superfluous '&'" warning.



Author Comment

ID: 12347927
Cheers Kent

Removing the cast works on both the 32 and 64 bit environments.  Also I output the values of &tmp and tmp and they are the same so it looks like the & is ignored at compile time.

Thanks for your Help


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

671 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question