diff options
| author | Andreas Schwab | 2007-02-16 13:20:39 +0000 |
|---|---|---|
| committer | Andreas Schwab | 2007-02-16 13:20:39 +0000 |
| commit | 01769a7373458a391cab62c03974112926c3be1a (patch) | |
| tree | 797498e1b79758f071f9ff033d30277ce6cc21e1 /src | |
| parent | cb06e57015e38dbaff0904762a8cd728a6cd1275 (diff) | |
| download | emacs-01769a7373458a391cab62c03974112926c3be1a.tar.gz emacs-01769a7373458a391cab62c03974112926c3be1a.zip | |
(doprnt1): Add support for '+' flag. Fix overflow checking.
Diffstat (limited to 'src')
| -rw-r--r-- | src/doprnt.c | 27 |
1 files changed, 13 insertions, 14 deletions
diff --git a/src/doprnt.c b/src/doprnt.c index 326fb5743e2..799f38d8f5d 100644 --- a/src/doprnt.c +++ b/src/doprnt.c | |||
| @@ -106,7 +106,7 @@ doprnt1 (lispstrings, buffer, bufsize, format, format_end, nargs, args) | |||
| 106 | char tembuf[DBL_MAX_10_EXP + 100]; | 106 | char tembuf[DBL_MAX_10_EXP + 100]; |
| 107 | 107 | ||
| 108 | /* Size of sprintf_buffer. */ | 108 | /* Size of sprintf_buffer. */ |
| 109 | int size_allocated = sizeof (tembuf); | 109 | unsigned size_allocated = sizeof (tembuf); |
| 110 | 110 | ||
| 111 | /* Buffer to use for sprintf. Either tembuf or same as BIG_BUFFER. */ | 111 | /* Buffer to use for sprintf. Either tembuf or same as BIG_BUFFER. */ |
| 112 | char *sprintf_buffer = tembuf; | 112 | char *sprintf_buffer = tembuf; |
| @@ -136,12 +136,12 @@ doprnt1 (lispstrings, buffer, bufsize, format, format_end, nargs, args) | |||
| 136 | { | 136 | { |
| 137 | if (*fmt == '%') /* Check for a '%' character */ | 137 | if (*fmt == '%') /* Check for a '%' character */ |
| 138 | { | 138 | { |
| 139 | int size_bound = 0; | 139 | unsigned size_bound = 0; |
| 140 | int width; /* Columns occupied by STRING. */ | 140 | int width; /* Columns occupied by STRING. */ |
| 141 | 141 | ||
| 142 | fmt++; | 142 | fmt++; |
| 143 | /* Copy this one %-spec into fmtcpy. */ | 143 | /* Copy this one %-spec into fmtcpy. */ |
| 144 | string = (unsigned char *)fmtcpy; | 144 | string = (unsigned char *) fmtcpy; |
| 145 | *string++ = '%'; | 145 | *string++ = '%'; |
| 146 | while (1) | 146 | while (1) |
| 147 | { | 147 | { |
| @@ -152,11 +152,11 @@ doprnt1 (lispstrings, buffer, bufsize, format, format_end, nargs, args) | |||
| 152 | This might be a field width or a precision; e.g. | 152 | This might be a field width or a precision; e.g. |
| 153 | %1.1000f and %1000.1f both might need 1000+ bytes. | 153 | %1.1000f and %1000.1f both might need 1000+ bytes. |
| 154 | Parse the width or precision, checking for overflow. */ | 154 | Parse the width or precision, checking for overflow. */ |
| 155 | int n = *fmt - '0'; | 155 | unsigned n = *fmt - '0'; |
| 156 | while ('0' <= fmt[1] && fmt[1] <= '9') | 156 | while ('0' <= fmt[1] && fmt[1] <= '9') |
| 157 | { | 157 | { |
| 158 | if (n * 10 / 10 != n | 158 | if (n * 10 / 10 != n |
| 159 | || (n = n * 10 + (fmt[1] - '0')) < 0) | 159 | || (n = n * 10 + (fmt[1] - '0')) < n) |
| 160 | error ("Format width or precision too large"); | 160 | error ("Format width or precision too large"); |
| 161 | *string++ = *++fmt; | 161 | *string++ = *++fmt; |
| 162 | } | 162 | } |
| @@ -164,7 +164,7 @@ doprnt1 (lispstrings, buffer, bufsize, format, format_end, nargs, args) | |||
| 164 | if (size_bound < n) | 164 | if (size_bound < n) |
| 165 | size_bound = n; | 165 | size_bound = n; |
| 166 | } | 166 | } |
| 167 | else if (*fmt == '-' || *fmt == ' ' || *fmt == '.') | 167 | else if (*fmt == '-' || *fmt == ' ' || *fmt == '.' || *fmt == '+') |
| 168 | ; | 168 | ; |
| 169 | else | 169 | else |
| 170 | break; | 170 | break; |
| @@ -174,10 +174,9 @@ doprnt1 (lispstrings, buffer, bufsize, format, format_end, nargs, args) | |||
| 174 | 174 | ||
| 175 | /* Make the size bound large enough to handle floating point formats | 175 | /* Make the size bound large enough to handle floating point formats |
| 176 | with large numbers. */ | 176 | with large numbers. */ |
| 177 | size_bound += DBL_MAX_10_EXP + 50; | 177 | if (size_bound + DBL_MAX_10_EXP + 50 < size_bound) |
| 178 | |||
| 179 | if (size_bound < 0) | ||
| 180 | error ("Format width or precision too large"); | 178 | error ("Format width or precision too large"); |
| 179 | size_bound += DBL_MAX_10_EXP + 50; | ||
| 181 | 180 | ||
| 182 | /* Make sure we have that much. */ | 181 | /* Make sure we have that much. */ |
| 183 | if (size_bound > size_allocated) | 182 | if (size_bound > size_allocated) |
| @@ -213,7 +212,7 @@ doprnt1 (lispstrings, buffer, bufsize, format, format_end, nargs, args) | |||
| 213 | abort (); | 212 | abort (); |
| 214 | sprintf (sprintf_buffer, fmtcpy, args[cnt++]); | 213 | sprintf (sprintf_buffer, fmtcpy, args[cnt++]); |
| 215 | /* Now copy into final output, truncating as nec. */ | 214 | /* Now copy into final output, truncating as nec. */ |
| 216 | string = (unsigned char *)sprintf_buffer; | 215 | string = (unsigned char *) sprintf_buffer; |
| 217 | goto doit; | 216 | goto doit; |
| 218 | 217 | ||
| 219 | case 'f': | 218 | case 'f': |
| @@ -227,7 +226,7 @@ doprnt1 (lispstrings, buffer, bufsize, format, format_end, nargs, args) | |||
| 227 | u.half[1] = args[cnt++]; | 226 | u.half[1] = args[cnt++]; |
| 228 | sprintf (sprintf_buffer, fmtcpy, u.d); | 227 | sprintf (sprintf_buffer, fmtcpy, u.d); |
| 229 | /* Now copy into final output, truncating as nec. */ | 228 | /* Now copy into final output, truncating as nec. */ |
| 230 | string = (unsigned char *)sprintf_buffer; | 229 | string = (unsigned char *) sprintf_buffer; |
| 231 | goto doit; | 230 | goto doit; |
| 232 | } | 231 | } |
| 233 | 232 | ||
| @@ -240,13 +239,13 @@ doprnt1 (lispstrings, buffer, bufsize, format, format_end, nargs, args) | |||
| 240 | minlen = atoi (&fmtcpy[1]); | 239 | minlen = atoi (&fmtcpy[1]); |
| 241 | if (lispstrings) | 240 | if (lispstrings) |
| 242 | { | 241 | { |
| 243 | string = ((struct Lisp_String *)args[cnt])->data; | 242 | string = ((struct Lisp_String *) args[cnt])->data; |
| 244 | tem = STRING_BYTES ((struct Lisp_String *)args[cnt]); | 243 | tem = STRING_BYTES ((struct Lisp_String *) args[cnt]); |
| 245 | cnt++; | 244 | cnt++; |
| 246 | } | 245 | } |
| 247 | else | 246 | else |
| 248 | { | 247 | { |
| 249 | string = (unsigned char *)args[cnt++]; | 248 | string = (unsigned char *) args[cnt++]; |
| 250 | tem = strlen (string); | 249 | tem = strlen (string); |
| 251 | } | 250 | } |
| 252 | width = strwidth (string, tem); | 251 | width = strwidth (string, tem); |