pairochjulrat / python-on-a-chip

Automatically exported from code.google.com/p/python-on-a-chip
Other
0 stars 0 forks source link

false positive in string/sequence comparison #245

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Email from Gil Ross on 2013/01/10:

I have found a bug in the string_compare (strobj.c) and seq_compare (seq.c) 
routines. Both these routines use sli_strncmp to determine equality. Depending 
on the value of HAVE_STRING_H, sli_strncmp compiles into a call to sli_strncmp 
(sli.c) or a call to strncmp (string.h). Because the strings are not always 
null-terminated ASCII strings, the call to strncmp may not compare the entire 
contents of the two strings. This could result in a false positive. I have 
changes these calls to memcmp, and this seems to work. This was responsible for 
the problem reported in my last post.

Original issue reported on code.google.com by dwhall...@gmail.com on 14 Feb 2013 at 1:41

GoogleCodeExporter commented 8 years ago
Email from Gil Ross on 2013/01/16:

If HAVE_STRING_H is true, and strncmp is implemented as follows:

      int strncmp(const char *s1, const char *s2, size_t n)
      {       /* compare unsigned char s1[max n], s2[max n] */
        for (; 0 < n; ++s1, ++s2, --n)
          if (*s1 != *s2)
            return (  *(unsigned char *)s1
                    < *(unsigned char *)s2 ? -1 : +1);
          else if (*s1 == '\0')
            return (0);
        return (0);
      }

The code below should assert:

seq1 = bytearray( [0xAA, 0xAA, 0xAA, 0xAA, 0x00, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA] )
seq2 = bytearray( [0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA] )
assert seq1 == seq2

Original comment by dwhall...@gmail.com on 14 Feb 2013 at 1:41