This is string-arg-overwirtten-in-sprintf-call in view mode; [Up]
Date: Sun 22-Aug-1991 17:48:17 From: woo@ornl.gov (John W. Wooten) Subject: string arg overwirtten in sprintf call Having a curious problem with the following piece of code. The definition for user_record isn't important, and MAXIN is #defined to be 60. The problem is that a temporary file name \tmp\tmp<PID> is created and stored in character string tmpfile. The file is created sucessfully and can be written into. It is then closed with the fclose. Just before the sprintf statement building mail_it in preparation for a system call, tmpfile is printed out and is correct. After the call to sprintf, tmpfile is printed out and contains only "gov"!!! The string mail_it is created correctly and contains the proper filename \tmp\tmp<PID>. I know somehow I'm stepping on something, but I can't figure out how or where. If you see my stupid error, please point it out. Everyone can have a big laugh at my stupidity, BUT maybe I can get past this stumbling point and get the code finished. ---- int mk_newuser(user_record,uid,pass) struct urec *user_record ; char *uid, *pass; { FILE *fptr; char tmpfile[MAXIN]; char mail_it[MAXIN]; int i; i = getpid(); sprintf(tmpfile,"/tmp/tmp%d",i); printf("\n\n---------Generating new account for uid %s\n",uid); printf("-------------User is %s %s %s\n\n", user_record[0].data, user_record[1].data, user_record[2].data); fptr = fopen(tmpfile,"w"); #if 0 fprintf(fptr,"\n\n---------Generating new account for uid %s\n",uid); for(i=0;i<NRECS;i++) { fprintf(fptr,"%s\t%s\n", user_record[i].prompt,user_record[i].data); } #endif fclose(fptr); printf("Before sprintf tmpfile = >%s<\n",tmpfile); sprintf(mail_it,"/bin/cat %s | mail -s 'Applicant %s' %s", tmpfile,uid,AUTH_AGENT); printf("After sprintf tmpfile = >%s<\n",tmpfile); printf("%s\n",mail_it); #if 0 system(mail_it); #endif if( remove(tmpfile) != 0) { printf("tmpfile = >%s<\n",tmpfile); perror("Error encountered attempting removal of tmpfile"); } return(TRUE); }
Date: Sun 22-Aug-1991 19:28:30 From: dkeisen@leland.Stanford.EDU (Dave Eisen) Subject: Re: string arg overwirtten in sprintf call In article <1991Aug22.174817.18163@ornl.gov> woo@ornl.gov (John W. Wooten) writes: >Having a curious problem with the following piece of code. >The definition for user_record isn't important, and MAXIN is #defined to be 60. > > char tmpfile[MAXIN]; > char mail_it[MAXIN]; .... > printf("Before sprintf tmpfile = >%s<\n",tmpfile); > sprintf(mail_it,"/bin/cat %s | mail -s 'Applicant %s' %s", > tmpfile,uid,AUTH_AGENT); > printf("After sprintf tmpfile = >%s<\n",tmpfile); My guess is that 60 bytes isn't big enough and your sprintf into mail_it is overwriting the next variable, tmpfile. Try it with MAXIN defines to be 1024 or something and see if you still have problems.
Date: Sun 23-Aug-1991 00:44:10 From: rearl@piggy.ucsb.edu (Robert Earl) Subject: Re: string arg overwirtten in sprintf call Have you considered naming the `tmpfile' variable to something else, preferably something that isn't already a library function?
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Marcel Waldvogel and Netfuture.ch.