[comp.sys.apple2] ORCA/C v1.2 bug in sscanf

neufeld@aurora.physics.utoronto.ca (Christopher Neufeld) (04/18/91)

   I just got my ORCA/C update yesterday to v1.2, and discovered a bug
after a few hours of searching. The sscanf() function doesn't work
properly. If a string is assigned which contains a long field, and a
later null-terminated string is assigned which is shorter than that
field, the sscanf() function merrily runs over the null character in its
zeal to locate two fields where only one exists. It is supposed to
return only a single string and return the actual number of fields read.
Instead it gleefully runs across the null character to get the fields it
needs. In some circumstances it works properly. It seems OK if the
string it is operating upon was never any longer than it is at the
moment it is called. This is strange. Anyway, here's a code fragment
which demonstrates the error, with the output as produced under ORCA/C
v1.2 and on a Sun/4. If anybody can suggest a useful workaround, I'd be
glad to hear it. I tried copying an extra null character after the one
already at the end of the string. That had no effect. The problem seems
to be that sscanf() is treating the null character as whitespace, and so
it's skipping it. This has been responsible for it picking up garbage
which wasn't ever in the input string, because it kept looking off the
end of the memory reserved for the string.
   For now I'm planning to move to the strtok() function for the things
I want to do. It's more natural in my program anyway, but the bug is
still a nuisance.


#include <stdio.h>
#include <string.h>

void main()
{
  char test[100];
  char s1[100],s2[100];
  int n;
  
  strcpy(test,"1234567890 ABCDEFGHIJ");  /* sscanf should find two strings */
  n = sscanf(test,"%s %s",s1,s2);
  printf("n=%i, s1=%s, s2=%s\n",n,s1,s2);
  strcpy(test,"abc");             /* sscanf should find one string  */
  n = sscanf(test,"%s %s",s1,s2);
  printf("n=%i, s1=%s, s2=%s\n",n,s1,s2);
}

Compiled under ORCA/C   v1.2 with ORCAGLIB library
n=2, s1=1234567890, s2=ABCDEFGHIJ
n=2, s1=abc, s2=567890


Compiled on a Sun/4:

n=2, s1=1234567890, s2=ABCDEFGHIJ
n=1, s1=abc, s2=ABCDEFGHIJ

That's the right answer. It doesn't make the second conversion, so s2 is
unchanged.


-- 
 Christopher Neufeld....Just a graduate student  | Flash: morning star seen
 neufeld@aurora.physics.utoronto.ca    Ad astra! | in evening! Baffled
 cneufeld@{pnet91,pro-cco}.cts.com               | astronomers: "could mean
 "Don't edit reality for the sake of simplicity" | second coming of Elvis!"