[Python-checkins] CVS: python/dist/src/Modules structmodule.c,2.38,2.39

Martin v. Löwis python-dev@python.org
Fri, 15 Sep 2000 00:32:01 -0700


Update of /cvsroot/python/python/dist/src/Modules
In directory slayer.i.sourceforge.net:/tmp/cvs-serv8124

Modified Files:
	structmodule.c 
Log Message:
Check range for bytes and shorts. Closes bug #110845.


Index: structmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/structmodule.c,v
retrieving revision 2.38
retrieving revision 2.39
diff -C2 -r2.38 -r2.39
*** structmodule.c	2000/09/01 23:29:27	2.38
--- structmodule.c	2000/09/15 07:31:57	2.39
***************
*** 478,481 ****
--- 478,501 ----
  	if (get_long(v, &x) < 0)
  		return -1;
+ 	if (x < -128 || x > 127){
+ 		PyErr_SetString(StructError,
+ 				"byte format requires -128<=number<=127");
+ 		return -1;
+ 	}
+ 	*p = (char)x;
+ 	return 0;
+ }
+ 
+ static int
+ np_ubyte(char *p, PyObject *v, const formatdef *f)
+ {
+ 	long x;
+ 	if (get_long(v, &x) < 0)
+ 		return -1;
+ 	if (x < 0 || x > 255){
+ 		PyErr_SetString(StructError,
+ 				"ubyte format requires 0<=number<=255");
+ 		return -1;
+ 	}
  	*p = (char)x;
  	return 0;
***************
*** 500,503 ****
--- 520,528 ----
  	if (get_long(v, &x) < 0)
  		return -1;
+ 	if (x < -32768 || x > 32767){
+ 		PyErr_SetString(StructError,
+ 				"short format requires -32768<=number<=32767");
+ 		return -1;
+ 	}
  	* (short *)p = (short)x;
  	return 0;
***************
*** 505,508 ****
--- 530,548 ----
  
  static int
+ np_ushort(char *p, PyObject *v, const formatdef *f)
+ {
+ 	long x;
+ 	if (get_long(v, &x) < 0)
+ 		return -1;
+ 	if (x < 0 || x > 65535){
+ 		PyErr_SetString(StructError,
+ 				"short format requires 0<=number<=65535");
+ 		return -1;
+ 	}
+ 	* (unsigned short *)p = (unsigned short)x;
+ 	return 0;
+ }
+ 
+ static int
  np_int(char *p, PyObject *v, const formatdef *f)
  {
***************
*** 588,597 ****
  	{'x',	sizeof(char),	0,		NULL},
  	{'b',	sizeof(char),	0,		nu_byte,	np_byte},
! 	{'B',	sizeof(char),	0,		nu_ubyte,	np_byte},
  	{'c',	sizeof(char),	0,		nu_char,	np_char},
  	{'s',	sizeof(char),	0,		NULL},
  	{'p',	sizeof(char),	0,		NULL},
  	{'h',	sizeof(short),	SHORT_ALIGN,	nu_short,	np_short},
! 	{'H',	sizeof(short),	SHORT_ALIGN,	nu_ushort,	np_short},
  	{'i',	sizeof(int),	INT_ALIGN,	nu_int,		np_int},
  	{'I',	sizeof(int),	INT_ALIGN,	nu_uint,	np_uint},
--- 628,637 ----
  	{'x',	sizeof(char),	0,		NULL},
  	{'b',	sizeof(char),	0,		nu_byte,	np_byte},
! 	{'B',	sizeof(char),	0,		nu_ubyte,	np_ubyte},
  	{'c',	sizeof(char),	0,		nu_char,	np_char},
  	{'s',	sizeof(char),	0,		NULL},
  	{'p',	sizeof(char),	0,		NULL},
  	{'h',	sizeof(short),	SHORT_ALIGN,	nu_short,	np_short},
! 	{'H',	sizeof(short),	SHORT_ALIGN,	nu_ushort,	np_ushort},
  	{'i',	sizeof(int),	INT_ALIGN,	nu_int,		np_int},
  	{'I',	sizeof(int),	INT_ALIGN,	nu_uint,	np_uint},