Re: XForms: problems with fl_get_choice_text

Steve Lamont (spl@szechuan.ucsd.edu)
Thu, 6 Nov 97 06:53:03 PST

To subscribers of the xforms list from spl@szechuan.ucsd.edu (Steve Lamont) :

> Thanks for the idea, but this should be no problem.
> To give some identifiers:
>
> sscanf((char *)datum(),"%*s %s %d %s %d",monat,&tag,time_string,&jahr);
>
> int tag;
> char monat[6];
> int jahr;
> char *time_string;
>
> /* (tag=day, monat=month, jahr=year)
>
> datum() returns something like "Thu Nov 6 11:12:34 1997"
>
> and the sscanf should get the things into the different variables
>
> %*s omits the first (Thu)
> %s writes "Nov" into monat (=char monat[6])
> %d writes 6 into tag
> %s writes "11:12:34" into time_string
> %d writes 1997 into jahr
>
> Ok, that's it. I think that should work, or did I misinterpret something?

Bingo!

Is the pointer `time_string' initialized? If not, you're going to be
writing into who knows where. sscanf() does not do any memory
allocation, it simply returns data to where ever it thinks it's told
to write. If `time_string' isn't initialized, then it will have
whatever value just happens to be on the stack in that slot.

You should change your code to read

char *time_string;

[...]

time_string = ( char *) malloc( TIME_STRING_SIZE );

[...]

sscanf( ... );

where TIME_STRING_SIZE is whatever size is appropriate -- maybe 9?

or

char time_string[TIME_STRING_SIZE];

I expect this is the reason why your pointer is getting trampled.

spl

_________________________________________________
To unsubscribe, send the message "unsubscribe" to
xforms-request@bob.usuf2.usuhs.mil or see
http://bob.usuf2.usuhs.mil/mailserv/xforms.html
Xforms Home Page: http://bragg.phys.uwm.edu/xforms
List Archive: http://bob.usuf2.usuhs.mil/mailserv/list-archives/