[Python-checkins] CVS: python/dist/src/Objects unicodeobject.c,2.122,2.123

Barry Warsaw bwarsaw@users.sourceforge.net
Wed, 28 Nov 2001 13:00:43 -0800


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

Modified Files:
	unicodeobject.c 
Log Message:
formatfloat(), formatint(): Conversion of sprintf() to PyOS_snprintf()
for buffer overrun avoidance.


Index: unicodeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v
retrieving revision 2.122
retrieving revision 2.123
diff -C2 -d -r2.122 -r2.123
*** unicodeobject.c	2001/11/28 12:56:20	2.122
--- unicodeobject.c	2001/11/28 21:00:41	2.123
***************
*** 5081,5085 ****
      if (type == 'f' && (fabs(x) / 1e25) >= 1e25)
  	type = 'g';
!     sprintf(fmt, "%%%s.%d%c", (flags & F_ALT) ? "#" : "", prec, type);
      /* worst case length calc to ensure no buffer overrun:
           fmt = %#.<prec>g
--- 5081,5086 ----
      if (type == 'f' && (fabs(x) / 1e25) >= 1e25)
  	type = 'g';
!     PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c",
! 		  (flags & F_ALT) ? "#" : "", prec, type);
      /* worst case length calc to ensure no buffer overrun:
           fmt = %#.<prec>g
***************
*** 5152,5164 ****
      if (x == 0 && (flags & F_ALT) && (type == 'x' || type == 'X')) {
          /* Only way to know what the platform does is to try it. */
!         sprintf(fmt, type == 'x' ? "%#x" : "%#X", 0);
          if (fmt[1] != (char)type) {
              /* Supply our own leading 0x/0X -- needed under std C */
              use_native_c_format = 0;
!             sprintf(fmt, "0%c%%#.%dl%c", type, prec, type);
          }
      }
      if (use_native_c_format)
!          sprintf(fmt, "%%%s.%dl%c", (flags & F_ALT) ? "#" : "", prec, type);
      return usprintf(buf, fmt, x);
  }
--- 5153,5166 ----
      if (x == 0 && (flags & F_ALT) && (type == 'x' || type == 'X')) {
          /* Only way to know what the platform does is to try it. */
!         PyOS_snprintf(fmt, sizeof(fmt), type == 'x' ? "%#x" : "%#X", 0);
          if (fmt[1] != (char)type) {
              /* Supply our own leading 0x/0X -- needed under std C */
              use_native_c_format = 0;
!             PyOS_snprintf(fmt, sizeof(fmt), "0%c%%#.%dl%c", type, prec, type);
          }
      }
      if (use_native_c_format)
!          PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%dl%c",
! 		       (flags & F_ALT) ? "#" : "", prec, type);
      return usprintf(buf, fmt, x);
  }