Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 872
  • Last Modified:

sscanf causing core dump

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");

    return;
}

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?
0
DLyall
Asked:
DLyall
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:

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.


Kent
0
 
stefan73Commented:
Hi DLyall,
Replace (unsigned int) by (unsigned long).


Cheers!

Stefan
0
 
Kent OlsenData Warehouse Architect / DBACommented:

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.


Kent
0
Become an IT Security Management Expert

In today’s fast-paced, digitally transformed world of business, the need to protect network data and ensure cloud privacy has never been greater. With a B.S. in Network Operations and Security, you can get the credentials it takes to become an IT security management expert.

 
Kent OlsenData Warehouse Architect / DBACommented:

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

Kent
0
 
AlexFMCommented:
Out of topic: I wonder how this is working -

char    tmp[30];
memset(&tmp, 0, 30 );         // should be tmp and not &tmp
0
 
van_dyCommented:
although memset(&tmp, 0, 30) is used wrongly, it can still work, only zeroing some other memory
0
 
Kent OlsenData Warehouse Architect / DBACommented:

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

Kent

0
 
DLyallAuthor Commented:
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

Don
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now