diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-01-06 03:26:53 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-01-06 03:26:53 +0000 |
commit | 56ea65ca5f30778f05c8882b04e3a94c869bbca4 (patch) | |
tree | 85a5d24ca1a86b504779b034fd9e7dbe4dfffa38 /libbb | |
parent | 5fee2e1a79dc6fc05658821a86b0e7b5678a90dd (diff) | |
download | busybox-w32-56ea65ca5f30778f05c8882b04e3a94c869bbca4.tar.gz busybox-w32-56ea65ca5f30778f05c8882b04e3a94c869bbca4.tar.bz2 busybox-w32-56ea65ca5f30778f05c8882b04e3a94c869bbca4.zip |
ps: fix overflow in USER and VSZ columns
function old new delta
smart_ulltoa4 - 280 +280
smart_ulltoa5 283 408 +125
ulltoa6_and_space - 25 +25
scale 28 38 +10
bbunpack 358 366 +8
ps_main 259 261 +2
glob3 35 37 +2
fill_bounds 172 174 +2
process_stdin 456 446 -10
smart_ulltoa6 406 - -406
------------------------------------------------------------------------------
(add/remove: 2/1 grow/shrink: 6/1 up/down: 454/-416) Total: 38 bytes
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/xfuncs.c | 69 |
1 files changed, 61 insertions, 8 deletions
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index 6d8eac4b6..6d50bf9bc 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c | |||
@@ -282,10 +282,10 @@ void xsetenv(const char *key, const char *value) | |||
282 | bb_error_msg_and_die(bb_msg_memory_exhausted); | 282 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
283 | } | 283 | } |
284 | 284 | ||
285 | // Converts unsigned long long value into compact 4-char | 285 | /* Converts unsigned long long value into compact 4-char |
286 | // representation. Examples: "1234", "1.2k", " 27M", "123T" | 286 | * representation. Examples: "1234", "1.2k", " 27M", "123T" |
287 | // Fifth char is always '\0' | 287 | * String is not terminated (buf[4] is untouched) */ |
288 | void smart_ulltoa5(unsigned long long ul, char buf[5]) | 288 | void smart_ulltoa4(unsigned long long ul, char buf[5], const char *scale) |
289 | { | 289 | { |
290 | const char *fmt; | 290 | const char *fmt; |
291 | char c; | 291 | char c; |
@@ -327,13 +327,66 @@ void smart_ulltoa5(unsigned long long ul, char buf[5]) | |||
327 | buf[1] = '.'; | 327 | buf[1] = '.'; |
328 | } | 328 | } |
329 | buf[2] = "0123456789"[v]; | 329 | buf[2] = "0123456789"[v]; |
330 | // see http://en.wikipedia.org/wiki/Tera | 330 | buf[3] = scale[idx]; /* typically scale = " kmgt..." */ |
331 | // (small letters stand out better versus numbers) | ||
332 | buf[3] = " kmgtpezy"[idx]; | ||
333 | } | 331 | } |
334 | buf[4] = '\0'; | ||
335 | } | 332 | } |
336 | 333 | ||
334 | /* Converts unsigned long long value into compact 5-char representation. | ||
335 | * String is not terminated (buf[5] is untouched) */ | ||
336 | void smart_ulltoa5(unsigned long long ul, char buf[6], const char *scale) | ||
337 | { | ||
338 | const char *fmt; | ||
339 | char c; | ||
340 | unsigned v, u, idx = 0; | ||
341 | |||
342 | if (ul > 99999) { // do not scale if 99999 or less | ||
343 | ul *= 10; | ||
344 | do { | ||
345 | ul /= 1024; | ||
346 | idx++; | ||
347 | } while (ul >= 100000); | ||
348 | } | ||
349 | v = ul; // ullong divisions are expensive, avoid them | ||
350 | |||
351 | fmt = " 123456789"; | ||
352 | u = v / 10; | ||
353 | v = v % 10; | ||
354 | if (!idx) { | ||
355 | // 99999 or less: use "12345" format | ||
356 | // u is value/10, v is last digit | ||
357 | c = buf[0] = " 123456789"[u/1000]; | ||
358 | if (c != ' ') fmt = "0123456789"; | ||
359 | c = buf[1] = fmt[u/100%10]; | ||
360 | if (c != ' ') fmt = "0123456789"; | ||
361 | c = buf[2] = fmt[u/10%10]; | ||
362 | if (c != ' ') fmt = "0123456789"; | ||
363 | buf[3] = fmt[u%10]; | ||
364 | buf[4] = "0123456789"[v]; | ||
365 | } else { | ||
366 | // value has been scaled into 0..9999.9 range | ||
367 | // u is value, v is 1/10ths (allows for 92.1M format) | ||
368 | if (u >= 100) { | ||
369 | // value is >= 100: use "1234M', " 123M" formats | ||
370 | c = buf[0] = " 123456789"[u/1000]; | ||
371 | if (c != ' ') fmt = "0123456789"; | ||
372 | c = buf[1] = fmt[u/100%10]; | ||
373 | if (c != ' ') fmt = "0123456789"; | ||
374 | v = u % 10; | ||
375 | u = u / 10; | ||
376 | buf[2] = fmt[u%10]; | ||
377 | } else { | ||
378 | // value is < 100: use "92.1M" format | ||
379 | c = buf[0] = " 123456789"[u/10]; | ||
380 | if (c != ' ') fmt = "0123456789"; | ||
381 | buf[1] = fmt[u%10]; | ||
382 | buf[2] = '.'; | ||
383 | } | ||
384 | buf[3] = "0123456789"[v]; | ||
385 | buf[4] = scale[idx]; /* typically scale = " kmgt..." */ | ||
386 | } | ||
387 | } | ||
388 | |||
389 | |||
337 | // Convert unsigned integer to ascii, writing into supplied buffer. | 390 | // Convert unsigned integer to ascii, writing into supplied buffer. |
338 | // A truncated result contains the first few digits of the result ala strncpy. | 391 | // A truncated result contains the first few digits of the result ala strncpy. |
339 | // Returns a pointer past last generated digit, does _not_ store NUL. | 392 | // Returns a pointer past last generated digit, does _not_ store NUL. |