Am 28.02.2012 13:27, schrieb Ralf Friedl: > walter harms wrote: >> Enabling the fast proc scan code for linux2.4 causes top >> to display strange numbers [see:possible bug in proc fast scan code] >> >> the reason for this is that the last char in proc/$$/stat is "\n". >> This is no problem for > 2.4 since the stats line contains additional >> chars. >> >> Please be aware that the current code does NOT stop at \0 ! adding more >> tokens to read will cause the code to read beyond \0 ! >> >> i also killed the variable c what saves 1 byte. >> > This variable would live on the stack if it would ever leave the > register (which it doesn't, even on a x86 with very few registers). > > Independent of this patch, you do realize that initializing n to > *str-'0' is some kind of manual loop unrolling? Both of the following > versions are smaller, at the cost of one more pass through the loop. I > don't know whether it is called often enough for this to matter. > The first version is smaller with -O9 -m32, -Os -m32 and -Os -m64 while > it is larger with -O9 -m64 with GCC 4.6.2. In each case both versions > are smaller than the original. > > unsigned long fast_strtoul_10(char **endptr) > { > char c; > char *str = *endptr; > unsigned long n = 0; > > while ((c = *str++) > ' ') > n = n*10 + (c - '0'); > > *endptr = str; /* We skip trailing space! */ > return n; > } > > unsigned long fast_strtoul_10(char **endptr) > { > char c; > char *str = *endptr; > unsigned long n = 0; > > while (*str > ' ') > n = n*10 + (*str++ - '0'); > > *endptr = str + 1; /* We skip trailing space! */ > return n; > } > >
Hello Ralf, the current version has a major problem when you consider this: "0\0" the patch ( change != into > ) will only catch the current bug (reading the last 0 wrong what actually happens on 2.4) but when the main code reads again (perhaps to get additional information) it will read beyond \0. char *cp="0\n"; fast_strtoul_10(&cp); <-- will work fine fast_strtoul_10(&cp); <-- will break re wh _______________________________________________ busybox mailing list busybox@busybox.net http://lists.busybox.net/mailman/listinfo/busybox