[BusyBox] 14 bugs on the wall

Larry Doolittle ldoolitt at recycle.lbl.gov
Mon Apr 23 21:53:01 UTC 2001


Squash another two of the "14 bugs on the wall".  Weed out five
space-hogging strlen calls,  and add one paranoia check to avoid
buffer underrun (looking for the second ":" in a long argument
specifier).  Saves 120 text bytes in my build environment.

I'm not a getopt user (nor do I play one on TV), so somebody should
test this patch before committing.  I did check that it compiles
and runs, but I'm not sure I would recognize a getopt(1) bug if
it bit me in the ass.

The code formatting in getopt.c does not conform to the style guide.
I made no attempt to fix that, or even stay consistent with what
was there.  The result is even messier than before.

     - Larry

diff -urN cvs/busybox/getopt.c busybox-trial/getopt.c
--- cvs/busybox/getopt.c	Fri Mar  9 14:09:18 2001
+++ busybox-trial/getopt.c	Mon Apr 23 14:39:33 2001
@@ -243,20 +243,23 @@
  */
 void add_long_options(char *options)
 {
-        int arg_opt;
+        int arg_opt, tlen;
         char *tokptr=strtok(options,", \t\n");
         while (tokptr) {
                 arg_opt=no_argument;
-                if (strlen(tokptr) > 0) {
-                        if (tokptr[strlen(tokptr)-1] == ':') {
-                                if (tokptr[strlen(tokptr)-2] == ':') {
-                                        tokptr[strlen(tokptr)-2]='\0';
+		tlen=strlen(tokptr);
+                if (tlen > 0) {
+                        if (tokptr[tlen-1] == ':') {
+                                if (tlen > 1 && tokptr[tlen-2] == ':') {
+                                        tokptr[tlen-2]='\0';
+					tlen -= 2;
                                         arg_opt=optional_argument;
                                 } else {
-                                        tokptr[strlen(tokptr)-1]='\0';
+                                        tokptr[tlen-1]='\0';
+					tlen -= 1;
                                         arg_opt=required_argument;
                                 }
-                                if (strlen(tokptr) == 0)
+                                if (tlen == 0)
                                         error_msg("empty long option after -l or --long argument");
                         }
                         add_longopt(tokptr,arg_opt);





More information about the busybox mailing list