[Python-checkins] python/dist/src/Modules _ssl.c,1.2,1.3 cPickle.c,2.82,2.83 cdmodule.c,1.27,1.28 clmodule.c,2.28,2.29 linuxaudiodev.c,2.17,2.18 regexmodule.c,1.47,1.48 socketmodule.c,1.215,1.216 stropmodule.c,2.87,2.88 zlibmodule.c,2.60,2.61

tim_one@sourceforge.net tim_one@sourceforge.net
Sat, 27 Apr 2002 11:44:34 -0700


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

Modified Files:
	_ssl.c cPickle.c cdmodule.c clmodule.c linuxaudiodev.c 
	regexmodule.c socketmodule.c stropmodule.c zlibmodule.c 
Log Message:
Repair widespread misuse of _PyString_Resize.  Since it's clear people
don't understand how this function works, also beefed up the docs.  The
most common usage error is of this form (often spread out across gotos):

	if (_PyString_Resize(&s, n) < 0) {
		Py_DECREF(s);
		s = NULL;
		goto outtahere;
	}

The error is that if _PyString_Resize runs out of memory, it automatically
decrefs the input string object s (which also deallocates it, since its
refcount must be 1 upon entry), and sets s to NULL.  So if the "if"
branch ever triggers, it's an error to call Py_DECREF(s):  s is already
NULL!  A correct way to write the above is the simpler (and intended)

	if (_PyString_Resize(&s, n) < 0)
		goto outtahere;

Bugfix candidate.


Index: _ssl.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/_ssl.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** _ssl.c	20 Apr 2002 07:47:40 -0000	1.2
--- _ssl.c	27 Apr 2002 18:44:29 -0000	1.3
***************
*** 332,337 ****
  		return PySSL_SetError(self, count);
  	}
! 	if (count != len && _PyString_Resize(&buf, count) < 0)
! 		return NULL;
  	return buf;
  }
--- 332,337 ----
  		return PySSL_SetError(self, count);
  	}
! 	if (count != len)
! 		_PyString_Resize(&buf, count);
  	return buf;
  }

Index: cPickle.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/cPickle.c,v
retrieving revision 2.82
retrieving revision 2.83
diff -C2 -d -r2.82 -r2.83
*** cPickle.c	21 Apr 2002 23:44:34 -0000	2.82
--- cPickle.c	27 Apr 2002 18:44:29 -0000	2.83
***************
*** 1253,1264 ****
  	}
  	*p = '\0';
! 	if (_PyString_Resize(&repr, p - q))
! 		goto onError;
! 
  	return repr;
- 
-   onError:
- 	Py_DECREF(repr);
- 	return NULL;
  }
  
--- 1253,1258 ----
  	}
  	*p = '\0';
! 	_PyString_Resize(&repr, p - q);
  	return repr;
  }
  

Index: cdmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/cdmodule.c,v
retrieving revision 1.27
retrieving revision 1.28
diff -C2 -d -r1.27 -r1.28
*** cdmodule.c	17 Jan 2002 23:15:58 -0000	1.27
--- cdmodule.c	27 Apr 2002 18:44:31 -0000	1.28
***************
*** 252,257 ****
  	}
  	if (n < numframes)
! 		if (_PyString_Resize(&result, n * sizeof(CDFRAME)))
! 			return NULL;
  
  	return result;
--- 252,256 ----
  	}
  	if (n < numframes)
! 		_PyString_Resize(&result, n * sizeof(CDFRAME));
  
  	return result;

Index: clmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/clmodule.c,v
retrieving revision 2.28
retrieving revision 2.29
diff -C2 -d -r2.28 -r2.29
*** clmodule.c	2 Apr 2002 18:26:33 -0000	2.28
--- clmodule.c	27 Apr 2002 18:44:31 -0000	2.29
***************
*** 136,141 ****
  
  	if (compressedBufferSize < frameBufferSize)
! 		if (_PyString_Resize(&compressedBuffer, compressedBufferSize))
! 			return NULL;
  
  	return compressedBuffer;
--- 136,140 ----
  
  	if (compressedBufferSize < frameBufferSize)
! 		_PyString_Resize(&compressedBuffer, compressedBufferSize);
  
  	return compressedBuffer;

Index: linuxaudiodev.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/linuxaudiodev.c,v
retrieving revision 2.17
retrieving revision 2.18
diff -C2 -d -r2.17 -r2.18
*** linuxaudiodev.c	12 Jan 2002 11:05:06 -0000	2.17
--- linuxaudiodev.c	27 Apr 2002 18:44:31 -0000	2.18
***************
*** 159,164 ****
      }
      self->x_icount += count;
!     if (_PyString_Resize(&rv, count) == -1)
! 	return NULL;
      return rv;
  }
--- 159,163 ----
      }
      self->x_icount += count;
!     _PyString_Resize(&rv, count);
      return rv;
  }

Index: regexmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/regexmodule.c,v
retrieving revision 1.47
retrieving revision 1.48
diff -C2 -d -r1.47 -r1.48
*** regexmodule.c	31 Mar 2002 15:27:00 -0000	1.47
--- regexmodule.c	27 Apr 2002 18:44:31 -0000	1.48
***************
*** 517,525 ****
  	}
  	/* _PyString_Resize() decrements npattern on failure */
! 	if (_PyString_Resize(&npattern, n - v) == 0)
! 		return npattern;
! 	else {
! 		return NULL;
! 	}
  
  }
--- 517,522 ----
  	}
  	/* _PyString_Resize() decrements npattern on failure */
! 	_PyString_Resize(&npattern, n - v);
! 	return npattern;
  
  }

Index: socketmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/socketmodule.c,v
retrieving revision 1.215
retrieving revision 1.216
diff -C2 -d -r1.215 -r1.216
*** socketmodule.c	12 Apr 2002 02:41:24 -0000	1.215
--- socketmodule.c	27 Apr 2002 18:44:31 -0000	1.216
***************
*** 1403,1408 ****
  		return s->errorhandler();
  	}
! 	if (n != len && _PyString_Resize(&buf, n) < 0)
! 		return NULL;
  	return buf;
  }
--- 1403,1408 ----
  		return s->errorhandler();
  	}
! 	if (n != len)
! 		_PyString_Resize(&buf, n);
  	return buf;
  }

Index: stropmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/stropmodule.c,v
retrieving revision 2.87
retrieving revision 2.88
diff -C2 -d -r2.87 -r2.88
*** stropmodule.c	2 Apr 2002 18:17:57 -0000	2.87
--- stropmodule.c	27 Apr 2002 18:44:31 -0000	2.88
***************
*** 216,223 ****
  			slen = PyString_GET_SIZE(item);
  			while (reslen + slen + seplen >= sz) {
! 				if (_PyString_Resize(&res, sz * 2)) {
! 					Py_DECREF(res);
  					return NULL;
- 				}
  				sz *= 2;
  				p = PyString_AsString(res) + reslen;
--- 216,221 ----
  			slen = PyString_GET_SIZE(item);
  			while (reslen + slen + seplen >= sz) {
! 				if (_PyString_Resize(&res, sz * 2) < 0)
  					return NULL;
  				sz *= 2;
  				p = PyString_AsString(res) + reslen;
***************
*** 232,239 ****
  			reslen += slen;
  		}
! 		if (_PyString_Resize(&res, reslen)) {
! 			Py_DECREF(res);
! 			res = NULL;
! 		}
  		return res;
  	}
--- 230,234 ----
  			reslen += slen;
  		}
! 		_PyString_Resize(&res, reslen);
  		return res;
  	}
***************
*** 258,263 ****
  		slen = PyString_GET_SIZE(item);
  		while (reslen + slen + seplen >= sz) {
! 			if (_PyString_Resize(&res, sz * 2)) {
! 				Py_DECREF(res);
  				Py_DECREF(item);
  				return NULL;
--- 253,257 ----
  		slen = PyString_GET_SIZE(item);
  		while (reslen + slen + seplen >= sz) {
! 			if (_PyString_Resize(&res, sz * 2) < 0) {
  				Py_DECREF(item);
  				return NULL;
***************
*** 276,283 ****
  		Py_DECREF(item);
  	}
! 	if (_PyString_Resize(&res, reslen)) {
! 		Py_DECREF(res);
! 		res = NULL;
! 	}
  	return res;
  }
--- 270,274 ----
  		Py_DECREF(item);
  	}
! 	_PyString_Resize(&res, reslen);
  	return res;
  }
***************
*** 990,995 ****
  	}
  	/* Fix the size of the resulting string */
! 	if (inlen > 0 &&_PyString_Resize(&result, output-output_start))
! 		return NULL;
  	return result;
  }
--- 981,986 ----
  	}
  	/* Fix the size of the resulting string */
! 	if (inlen > 0)
! 		_PyString_Resize(&result, output - output_start);
  	return result;
  }

Index: zlibmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/zlibmodule.c,v
retrieving revision 2.60
retrieving revision 2.61
diff -C2 -d -r2.60 -r2.61
*** zlibmodule.c	19 Apr 2002 14:37:07 -0000	2.60
--- zlibmodule.c	27 Apr 2002 18:44:31 -0000	2.61
***************
*** 256,262 ****
  	case(Z_OK):
  	    /* need more memory */
! 	    if (_PyString_Resize(&result_str, r_strlen << 1) == -1) {
  		inflateEnd(&zst);
- 		result_str = NULL;
  		goto error;
  	    }
--- 256,261 ----
  	case(Z_OK):
  	    /* need more memory */
! 	    if (_PyString_Resize(&result_str, r_strlen << 1) < 0) {
  		inflateEnd(&zst);
  		goto error;
  	    }
***************
*** 415,422 ****
         so extend the output buffer and try again */
      while (err == Z_OK && self->zst.avail_out == 0) {
! 	if (_PyString_Resize(&RetVal, length << 1) == -1) {
! 	    RetVal = NULL;
  	    goto error;
- 	}
  	self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
  	    + length;
--- 414,419 ----
         so extend the output buffer and try again */
      while (err == Z_OK && self->zst.avail_out == 0) {
! 	if (_PyString_Resize(&RetVal, length << 1) < 0)
  	    goto error;
  	self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
  	    + length;
***************
*** 439,445 ****
  	goto error;
      }
!     if (_PyString_Resize(&RetVal,
! 			 self->zst.total_out - start_total_out) < 0)
! 	RetVal = NULL;
  
   error:
--- 436,440 ----
  	goto error;
      }
!     _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
  
   error:
***************
*** 511,518 ****
  	    length = max_length;
  
! 	if (_PyString_Resize(&RetVal, length) == -1) {
! 	    RetVal = NULL;
  	    goto error;
- 	}
  	self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
  	    + old_length;
--- 506,511 ----
  	    length = max_length;
  
! 	if (_PyString_Resize(&RetVal, length) < 0)
  	    goto error;
  	self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
  	    + old_length;
***************
*** 562,567 ****
      }
  
!     if (_PyString_Resize(&RetVal, self->zst.total_out - start_total_out) < 0)
! 	RetVal = NULL;
  
   error:
--- 555,559 ----
      }
  
!     _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
  
   error:
***************
*** 613,620 ****
         so extend the output buffer and try again */
      while (err == Z_OK && self->zst.avail_out == 0) {
! 	if (_PyString_Resize(&RetVal, length << 1) == -1)  {
! 	    RetVal = NULL;
  	    goto error;
- 	}
  	self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
  	    + length;
--- 605,610 ----
         so extend the output buffer and try again */
      while (err == Z_OK && self->zst.avail_out == 0) {
! 	if (_PyString_Resize(&RetVal, length << 1) < 0)
  	    goto error;
  	self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
  	    + length;
***************
*** 652,657 ****
      }
  
!     if (_PyString_Resize(&RetVal, self->zst.total_out - start_total_out) < 0)
! 	RetVal = NULL;
  
   error:
--- 642,646 ----
      }
  
!     _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
  
   error: