From: Mark Whitley Date: Mon, 23 Apr 2001 23:16:20 +0000 (-0000) Subject: Applied patch from Larry Doolittle to remove some strlen calls, and add one X-Git-Tag: 0_52~308 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=af030496fae5c4ad24770be8a850b244286ba608;p=oweals%2Fbusybox.git Applied patch from Larry Doolittle to remove some strlen calls, and add one paranoia check to avoid buffer underrun. Saves 120 text bytes. --- diff --git a/getopt.c b/getopt.c index b74dd65a1..95ecba6e6 100644 --- a/getopt.c +++ b/getopt.c @@ -243,20 +243,23 @@ void add_longopt(const char *name,int has_arg) */ 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); diff --git a/util-linux/getopt.c b/util-linux/getopt.c index b74dd65a1..95ecba6e6 100644 --- a/util-linux/getopt.c +++ b/util-linux/getopt.c @@ -243,20 +243,23 @@ void add_longopt(const char *name,int has_arg) */ 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);