[Python-checkins] CVS: python/dist/src/Modules ucnhash.c,1.6,1.7

Fredrik Lundh effbot@users.sourceforge.net
Fri, 19 Jan 2001 11:45:04 -0800


Update of /cvsroot/python/python/dist/src/Modules
In directory usw-pr-cvs1:/tmp/cvs-serv340/Modules

Modified Files:
	ucnhash.c 
Log Message:


gethash/cmpname both looked beyond the end of the character name.
This patch makes u"\N{x}" a bit less dependent on pure luck...


Index: ucnhash.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/ucnhash.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -r1.6 -r1.7
*** ucnhash.c	2001/01/19 11:52:33	1.6
--- ucnhash.c	2001/01/19 19:45:02	1.7
***************
*** 12,27 ****
  
  static unsigned long
! gethash(const char *s)
  {
      unsigned long h = 0;
!     unsigned long i;
!     while (*s) {
          /* magic value 47 was chosen to minimize the number
             of collisions for the uninames dataset.  see the
             makeunicodedata script for more background */
!         h = (h * 47) + (unsigned char) toupper(*s++);
!         i = h & 0xff000000;
!         if (i)
!             h = (h ^ ((i>>24) & 0xff)) & 0x00ffffff;
      }
      return h;
--- 12,28 ----
  
  static unsigned long
! gethash(const char *s, int len)
  {
+     int i;
      unsigned long h = 0;
!     unsigned long ix;
!     for (i = 0; i < len; i++) {
          /* magic value 47 was chosen to minimize the number
             of collisions for the uninames dataset.  see the
             makeunicodedata script for more background */
!         h = (h * 47) + (unsigned char) toupper(s[i]);
!         ix = h & 0xff000000;
!         if (ix)
!             h = (h ^ ((ix>>24) & 0xff)) & 0x00ffffff;
      }
      return h;
***************
*** 81,85 ****
  
  static int
! cmpname(int code, const char* name)
  {
      /* check if code corresponds to the given name */
--- 82,86 ----
  
  static int
! cmpname(int code, const char* name, int namelen)
  {
      /* check if code corresponds to the given name */
***************
*** 88,99 ****
      if (!getname(code, buffer, sizeof(buffer)))
          return 0;
!     i = 0;
!     for (;;) {
          if (toupper(name[i]) != buffer[i])
              return 0;
-         if (!name[i] || !buffer[i])
-             return 1;
-         i++;
      }
  }
  
--- 89,97 ----
      if (!getname(code, buffer, sizeof(buffer)))
          return 0;
!     for (i = 0; i < namelen; i++) {
          if (toupper(name[i]) != buffer[i])
              return 0;
      }
+     return buffer[namelen] == '\0';
  }
  
***************
*** 109,118 ****
         details */
  
!     h = (unsigned int) gethash(name);
      i = (~h) & mask;
      v = code_hash[i];
      if (!v)
          return 0;
!     if (cmpname(v, name)) {
          *code = v;
          return 1;
--- 107,116 ----
         details */
  
!     h = (unsigned int) gethash(name, namelen);
      i = (~h) & mask;
      v = code_hash[i];
      if (!v)
          return 0;
!     if (cmpname(v, name, namelen)) {
          *code = v;
          return 1;
***************
*** 126,130 ****
          if (!v)
              return -1;
!         if (cmpname(v, name)) {
              *code = v;
              return 1;
--- 124,128 ----
          if (!v)
              return -1;
!         if (cmpname(v, name, namelen)) {
              *code = v;
              return 1;