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 | |
| 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
| -rw-r--r-- | include/libbb.h | 4 | ||||
| -rw-r--r-- | libbb/xfuncs.c | 69 | ||||
| -rw-r--r-- | procps/nmeter.c | 5 | ||||
| -rw-r--r-- | procps/ps.c | 32 | ||||
| -rw-r--r-- | procps/top.c | 72 |
5 files changed, 99 insertions, 83 deletions
diff --git a/include/libbb.h b/include/libbb.h index 33e73b27b..a07606287 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
| @@ -503,7 +503,9 @@ char *itoa(int n); | |||
| 503 | /* Returns a pointer past the formatted number, does NOT null-terminate */ | 503 | /* Returns a pointer past the formatted number, does NOT null-terminate */ |
| 504 | char *utoa_to_buf(unsigned n, char *buf, unsigned buflen); | 504 | char *utoa_to_buf(unsigned n, char *buf, unsigned buflen); |
| 505 | char *itoa_to_buf(int n, char *buf, unsigned buflen); | 505 | char *itoa_to_buf(int n, char *buf, unsigned buflen); |
| 506 | void smart_ulltoa5(unsigned long long ul, char buf[5]); | 506 | /* Intelligent formatters of bignums */ |
| 507 | void smart_ulltoa4(unsigned long long ul, char buf[5], const char *scale); | ||
| 508 | void smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale); | ||
| 507 | //TODO: provide pointer to buf (avoid statics)? | 509 | //TODO: provide pointer to buf (avoid statics)? |
| 508 | const char *make_human_readable_str(unsigned long long size, | 510 | const char *make_human_readable_str(unsigned long long size, |
| 509 | unsigned long block_size, unsigned long display_unit); | 511 | unsigned long block_size, unsigned long display_unit); |
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. |
diff --git a/procps/nmeter.c b/procps/nmeter.c index b846ee5b6..b8ba3facb 100644 --- a/procps/nmeter.c +++ b/procps/nmeter.c | |||
| @@ -257,7 +257,10 @@ static int rdval_diskstats(const char* p, ullong *vec) | |||
| 257 | static void scale(ullong ul) | 257 | static void scale(ullong ul) |
| 258 | { | 258 | { |
| 259 | char buf[5]; | 259 | char buf[5]; |
| 260 | smart_ulltoa5(ul, buf); | 260 | |
| 261 | /* see http://en.wikipedia.org/wiki/Tera */ | ||
| 262 | smart_ulltoa4(ul, buf, " kmgtpezy"); | ||
| 263 | buf[4] = '\0'; | ||
| 261 | put(buf); | 264 | put(buf); |
| 262 | } | 265 | } |
| 263 | 266 | ||
diff --git a/procps/ps.c b/procps/ps.c index a6c35f101..6a6a9e6d6 100644 --- a/procps/ps.c +++ b/procps/ps.c | |||
| @@ -25,9 +25,9 @@ enum { MAX_WIDTH = 2*1024 }; | |||
| 25 | 25 | ||
| 26 | #if ENABLE_SELINUX | 26 | #if ENABLE_SELINUX |
| 27 | #define SELINIX_O_PREFIX "label," | 27 | #define SELINIX_O_PREFIX "label," |
| 28 | #define DEFAULT_O_STR (SELINIX_O_PREFIX "pid,user" USE_FEATURE_PS_TIME(",time")) | 28 | #define DEFAULT_O_STR (SELINIX_O_PREFIX "pid,user" USE_FEATURE_PS_TIME(",time") ",args") |
| 29 | #else | 29 | #else |
| 30 | #define DEFAULT_O_STR ("pid,user" USE_FEATURE_PS_TIME(",time")) | 30 | #define DEFAULT_O_STR ("pid,user" USE_FEATURE_PS_TIME(",time") ",args") |
| 31 | #endif | 31 | #endif |
| 32 | 32 | ||
| 33 | typedef struct { | 33 | typedef struct { |
| @@ -188,7 +188,10 @@ static void func_pgid(char *buf, int size, const procps_status_t *ps) | |||
| 188 | static void put_lu(char *buf, int size, unsigned long u) | 188 | static void put_lu(char *buf, int size, unsigned long u) |
| 189 | { | 189 | { |
| 190 | char buf5[5]; | 190 | char buf5[5]; |
| 191 | smart_ulltoa5( ((unsigned long long)u) << 10, buf5); | 191 | |
| 192 | /* see http://en.wikipedia.org/wiki/Tera */ | ||
| 193 | smart_ulltoa4( (u, buf5, " mgtpezy"); | ||
| 194 | buf5[5] = '\0'; | ||
| 192 | sprintf(buf, "%.*s", size, buf5); | 195 | sprintf(buf, "%.*s", size, buf5); |
| 193 | } | 196 | } |
| 194 | 197 | ||
| @@ -505,9 +508,9 @@ int ps_main(int argc, char **argv) | |||
| 505 | #endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */ | 508 | #endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */ |
| 506 | 509 | ||
| 507 | if (use_selinux) | 510 | if (use_selinux) |
| 508 | puts(" PID Context Stat Command"); | 511 | puts(" PID CONTEXT STAT COMMAND"); |
| 509 | else | 512 | else |
| 510 | puts(" PID Uid VSZ Stat Command"); | 513 | puts(" PID USER VSZ STAT COMMAND"); |
| 511 | 514 | ||
| 512 | while ((p = procps_scan(p, 0 | 515 | while ((p = procps_scan(p, 0 |
| 513 | | PSSCAN_PID | 516 | | PSSCAN_PID |
| @@ -519,7 +522,7 @@ int ps_main(int argc, char **argv) | |||
| 519 | ))) { | 522 | ))) { |
| 520 | #if ENABLE_SELINUX | 523 | #if ENABLE_SELINUX |
| 521 | if (use_selinux) { | 524 | if (use_selinux) { |
| 522 | len = printf("%5u %-32s %s ", | 525 | len = printf("%5u %-32.32s %s ", |
| 523 | p->pid, | 526 | p->pid, |
| 524 | p->context ? p->context : "unknown", | 527 | p->context ? p->context : "unknown", |
| 525 | p->state); | 528 | p->state); |
| @@ -527,12 +530,17 @@ int ps_main(int argc, char **argv) | |||
| 527 | #endif | 530 | #endif |
| 528 | { | 531 | { |
| 529 | const char *user = get_cached_username(p->uid); | 532 | const char *user = get_cached_username(p->uid); |
| 530 | if (p->vsz == 0) | 533 | //if (p->vsz == 0) |
| 531 | len = printf("%5u %-8s %s ", | 534 | // len = printf("%5u %-8.8s %s ", |
| 532 | p->pid, user, p->state); | 535 | // p->pid, user, p->state); |
| 533 | else | 536 | //else |
| 534 | len = printf("%5u %-8s %6lu %s ", | 537 | { |
| 535 | p->pid, user, p->vsz, p->state); | 538 | char buf6[6]; |
| 539 | smart_ulltoa5(p->vsz, buf6, " mgtpezy"); | ||
| 540 | buf6[5] = '\0'; | ||
| 541 | len = printf("%5u %-8.8s %s %s ", | ||
| 542 | p->pid, user, buf6, p->state); | ||
| 543 | } | ||
| 536 | } | 544 | } |
| 537 | 545 | ||
| 538 | { | 546 | { |
diff --git a/procps/top.c b/procps/top.c index 200519162..fc393d43b 100644 --- a/procps/top.c +++ b/procps/top.c | |||
| @@ -669,60 +669,10 @@ static void display_topmem_header(int scr_width) | |||
| 669 | #undef str | 669 | #undef str |
| 670 | } | 670 | } |
| 671 | 671 | ||
| 672 | // Converts unsigned long long value into compact 5-char | 672 | static void ulltoa6_and_space(unsigned long long ul, char buf[6]) |
| 673 | // representation. Sixth char is always ' ' | ||
| 674 | static void smart_ulltoa6(unsigned long long ul, char buf[6]) | ||
| 675 | { | 673 | { |
| 676 | const char *fmt; | 674 | /* see http://en.wikipedia.org/wiki/Tera */ |
| 677 | char c; | 675 | smart_ulltoa5(ul, buf, " mgtpezy"); |
| 678 | unsigned v, u, idx = 0; | ||
| 679 | |||
| 680 | if (ul > 99999) { // do not scale if 99999 or less | ||
| 681 | ul *= 10; | ||
| 682 | do { | ||
| 683 | ul /= 1024; | ||
| 684 | idx++; | ||
| 685 | } while (ul >= 100000); | ||
| 686 | } | ||
| 687 | v = ul; // ullong divisions are expensive, avoid them | ||
| 688 | |||
| 689 | fmt = " 123456789"; | ||
| 690 | u = v / 10; | ||
| 691 | v = v % 10; | ||
| 692 | if (!idx) { | ||
| 693 | // 99999 or less: use "12345" format | ||
| 694 | // u is value/10, v is last digit | ||
| 695 | c = buf[0] = " 123456789"[u/1000]; | ||
| 696 | if (c != ' ') fmt = "0123456789"; | ||
| 697 | c = buf[1] = fmt[u/100%10]; | ||
| 698 | if (c != ' ') fmt = "0123456789"; | ||
| 699 | c = buf[2] = fmt[u/10%10]; | ||
| 700 | if (c != ' ') fmt = "0123456789"; | ||
| 701 | buf[3] = fmt[u%10]; | ||
| 702 | buf[4] = "0123456789"[v]; | ||
| 703 | } else { | ||
| 704 | // value has been scaled into 0..9999.9 range | ||
| 705 | // u is value, v is 1/10ths (allows for 92.1M format) | ||
| 706 | if (u >= 100) { | ||
| 707 | // value is >= 100: use "1234M', " 123M" formats | ||
| 708 | c = buf[0] = " 123456789"[u/1000]; | ||
| 709 | if (c != ' ') fmt = "0123456789"; | ||
| 710 | c = buf[1] = fmt[u/100%10]; | ||
| 711 | if (c != ' ') fmt = "0123456789"; | ||
| 712 | v = u % 10; | ||
| 713 | u = u / 10; | ||
| 714 | buf[2] = fmt[u%10]; | ||
| 715 | } else { | ||
| 716 | // value is < 100: use "92.1M" format | ||
| 717 | c = buf[0] = " 123456789"[u/10]; | ||
| 718 | if (c != ' ') fmt = "0123456789"; | ||
| 719 | buf[1] = fmt[u%10]; | ||
| 720 | buf[2] = '.'; | ||
| 721 | } | ||
| 722 | buf[3] = "0123456789"[v]; | ||
| 723 | // see http://en.wikipedia.org/wiki/Tera | ||
| 724 | buf[4] = " mgtpezy"[idx]; | ||
| 725 | } | ||
| 726 | buf[5] = ' '; | 676 | buf[5] = ' '; |
| 727 | } | 677 | } |
| 728 | 678 | ||
| @@ -739,14 +689,14 @@ static NOINLINE void display_topmem_process_list(int count, int scr_width) | |||
| 739 | 689 | ||
| 740 | while (--count >= 0) { | 690 | while (--count >= 0) { |
| 741 | // PID VSZ VSZRW RSS (SHR) DIRTY (SHR) COMMAND | 691 | // PID VSZ VSZRW RSS (SHR) DIRTY (SHR) COMMAND |
| 742 | smart_ulltoa6(s->pid , &line_buf[0*6]); | 692 | ulltoa6_and_space(s->pid , &line_buf[0*6]); |
| 743 | smart_ulltoa6(s->vsz , &line_buf[1*6]); | 693 | ulltoa6_and_space(s->vsz , &line_buf[1*6]); |
| 744 | smart_ulltoa6(s->vszrw , &line_buf[2*6]); | 694 | ulltoa6_and_space(s->vszrw , &line_buf[2*6]); |
| 745 | smart_ulltoa6(s->rss , &line_buf[3*6]); | 695 | ulltoa6_and_space(s->rss , &line_buf[3*6]); |
| 746 | smart_ulltoa6(s->rss_sh , &line_buf[4*6]); | 696 | ulltoa6_and_space(s->rss_sh , &line_buf[4*6]); |
| 747 | smart_ulltoa6(s->dirty , &line_buf[5*6]); | 697 | ulltoa6_and_space(s->dirty , &line_buf[5*6]); |
| 748 | smart_ulltoa6(s->dirty_sh, &line_buf[6*6]); | 698 | ulltoa6_and_space(s->dirty_sh, &line_buf[6*6]); |
| 749 | smart_ulltoa6(s->stack , &line_buf[7*6]); | 699 | ulltoa6_and_space(s->stack , &line_buf[7*6]); |
| 750 | line_buf[8*6] = '\0'; | 700 | line_buf[8*6] = '\0'; |
| 751 | if (scr_width > MIN_WIDTH) { | 701 | if (scr_width > MIN_WIDTH) { |
| 752 | read_cmdline(&line_buf[8*6], scr_width - MIN_WIDTH, s->pid, s->comm); | 702 | read_cmdline(&line_buf[8*6], scr_width - MIN_WIDTH, s->pid, s->comm); |
