diff options
author | Eric Andersen <andersen@codepoet.org> | 2001-04-26 15:56:47 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2001-04-26 15:56:47 +0000 |
commit | c1bdffe99b6fa6fdb0f43e0dec85d8bbfcab1572 (patch) | |
tree | 3cf5b7c0c69bd8c802c4b080b6a5e94a4a401831 | |
parent | 3c3277f0bda79b66b35e5e965e5340624d29a459 (diff) | |
download | busybox-w32-c1bdffe99b6fa6fdb0f43e0dec85d8bbfcab1572.tar.gz busybox-w32-c1bdffe99b6fa6fdb0f43e0dec85d8bbfcab1572.tar.bz2 busybox-w32-c1bdffe99b6fa6fdb0f43e0dec85d8bbfcab1572.zip |
Another nice cleanup from Larry. This adds a new last_char_is() function and
uses it to avoid possible buffer underruns whn strlen is zero, and avoid the
possible space-hogging inline of strlen() in several cases.
-Erik
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | archival/dpkg.c | 2 | ||||
-rw-r--r-- | archival/tar.c | 2 | ||||
-rw-r--r-- | coreutils/cut.c | 2 | ||||
-rw-r--r-- | cut.c | 2 | ||||
-rw-r--r-- | dpkg.c | 2 | ||||
-rw-r--r-- | editors/vi.c | 4 | ||||
-rw-r--r-- | include/libbb.h | 1 | ||||
-rw-r--r-- | libbb/last_char_is.c | 33 | ||||
-rw-r--r-- | libbb/libbb.h | 1 | ||||
-rw-r--r-- | tar.c | 2 | ||||
-rw-r--r-- | vi.c | 4 |
12 files changed, 46 insertions, 11 deletions
@@ -248,7 +248,7 @@ process_escape_sequence.c read_package_field.c read_text_file_to_buffer.c \ | |||
248 | recursive_action.c safe_read.c safe_strncpy.c seek_ared_file.c syscalls.c \ | 248 | recursive_action.c safe_read.c safe_strncpy.c seek_ared_file.c syscalls.c \ |
249 | syslog_msg_with_name.c time_string.c trim.c untar.c unzip.c vdprintf.c \ | 249 | syslog_msg_with_name.c time_string.c trim.c untar.c unzip.c vdprintf.c \ |
250 | verror_msg.c vperror_msg.c wfopen.c xfuncs.c xgetcwd.c xregcomp.c interface.c \ | 250 | verror_msg.c vperror_msg.c wfopen.c xfuncs.c xgetcwd.c xregcomp.c interface.c \ |
251 | remove_file.c | 251 | remove_file.c last_char_is.c |
252 | LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC)) | 252 | LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC)) |
253 | LIBBB_CFLAGS = -I$(LIBBB) | 253 | LIBBB_CFLAGS = -I$(LIBBB) |
254 | ifneq ($(strip $(BB_SRC_DIR)),) | 254 | ifneq ($(strip $(BB_SRC_DIR)),) |
diff --git a/archival/dpkg.c b/archival/dpkg.c index e5ed95cb8..996809a6f 100644 --- a/archival/dpkg.c +++ b/archival/dpkg.c | |||
@@ -583,7 +583,7 @@ static int status_merge(void *status, package_t *pkgs) | |||
583 | */ | 583 | */ |
584 | if ((fin = fopen(statusfile, "r")) != NULL) { | 584 | if ((fin = fopen(statusfile, "r")) != NULL) { |
585 | while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { | 585 | while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { |
586 | line[strlen(line) - 1] = '\0'; /* trim newline */ | 586 | chomp(line); /* trim newline */ |
587 | /* If we see a package header, find out if it's a package | 587 | /* If we see a package header, find out if it's a package |
588 | * that we have processed. if so, we skip that block for | 588 | * that we have processed. if so, we skip that block for |
589 | * now (write it at the end). | 589 | * now (write it at the end). |
diff --git a/archival/tar.c b/archival/tar.c index 48284c00a..716f4ac30 100644 --- a/archival/tar.c +++ b/archival/tar.c | |||
@@ -706,7 +706,7 @@ static int readTarFile(int tarFd, int extractFlag, int listFlag, | |||
706 | case REGTYPE0: | 706 | case REGTYPE0: |
707 | /* If the name ends in a '/' then assume it is | 707 | /* If the name ends in a '/' then assume it is |
708 | * supposed to be a directory, and fall through */ | 708 | * supposed to be a directory, and fall through */ |
709 | if (header.name[strlen(header.name)-1] != '/') { | 709 | if (last_char_is(header.name,'/')) { |
710 | if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE) | 710 | if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE) |
711 | errorFlag=TRUE; | 711 | errorFlag=TRUE; |
712 | break; | 712 | break; |
diff --git a/coreutils/cut.c b/coreutils/cut.c index 7e9a72e3f..d852ab3be 100644 --- a/coreutils/cut.c +++ b/coreutils/cut.c | |||
@@ -75,7 +75,7 @@ static void decompose_list(const char *list) | |||
75 | /* handle multi-value cases */ | 75 | /* handle multi-value cases */ |
76 | else if (nminus == 1) { | 76 | else if (nminus == 1) { |
77 | /* handle 'N-' case */ | 77 | /* handle 'N-' case */ |
78 | if (list[strlen(list) - 1] == '-') { | 78 | if (last_char_is(list,'-')) { |
79 | startpos = strtol(list, &ptr, 10); | 79 | startpos = strtol(list, &ptr, 10); |
80 | } | 80 | } |
81 | /* handle '-M' case */ | 81 | /* handle '-M' case */ |
@@ -75,7 +75,7 @@ static void decompose_list(const char *list) | |||
75 | /* handle multi-value cases */ | 75 | /* handle multi-value cases */ |
76 | else if (nminus == 1) { | 76 | else if (nminus == 1) { |
77 | /* handle 'N-' case */ | 77 | /* handle 'N-' case */ |
78 | if (list[strlen(list) - 1] == '-') { | 78 | if (last_char_is(list,'-')) { |
79 | startpos = strtol(list, &ptr, 10); | 79 | startpos = strtol(list, &ptr, 10); |
80 | } | 80 | } |
81 | /* handle '-M' case */ | 81 | /* handle '-M' case */ |
@@ -583,7 +583,7 @@ static int status_merge(void *status, package_t *pkgs) | |||
583 | */ | 583 | */ |
584 | if ((fin = fopen(statusfile, "r")) != NULL) { | 584 | if ((fin = fopen(statusfile, "r")) != NULL) { |
585 | while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { | 585 | while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { |
586 | line[strlen(line) - 1] = '\0'; /* trim newline */ | 586 | chomp(line); /* trim newline */ |
587 | /* If we see a package header, find out if it's a package | 587 | /* If we see a package header, find out if it's a package |
588 | * that we have processed. if so, we skip that block for | 588 | * that we have processed. if so, we skip that block for |
589 | * now (write it at the end). | 589 | * now (write it at the end). |
diff --git a/editors/vi.c b/editors/vi.c index 6a93fc1fe..96fc96559 100644 --- a/editors/vi.c +++ b/editors/vi.c | |||
@@ -19,7 +19,7 @@ | |||
19 | */ | 19 | */ |
20 | 20 | ||
21 | char *vi_Version = | 21 | char *vi_Version = |
22 | "$Id: vi.c,v 1.4 2001/04/16 15:46:44 andersen Exp $"; | 22 | "$Id: vi.c,v 1.5 2001/04/26 15:56:47 andersen Exp $"; |
23 | 23 | ||
24 | /* | 24 | /* |
25 | * To compile for standalone use: | 25 | * To compile for standalone use: |
@@ -1745,7 +1745,7 @@ static void colon(Byte * buf) | |||
1745 | while (isblnk(*buf)) | 1745 | while (isblnk(*buf)) |
1746 | buf++; | 1746 | buf++; |
1747 | strcpy((char *) args, (char *) buf); | 1747 | strcpy((char *) args, (char *) buf); |
1748 | if (cmd[strlen((char *) cmd) - 1] == '!') { | 1748 | if (last_char_is((char *)cmd,'!')) { |
1749 | useforce = TRUE; | 1749 | useforce = TRUE; |
1750 | cmd[strlen((char *) cmd) - 1] = '\0'; // get rid of ! | 1750 | cmd[strlen((char *) cmd) - 1] = '\0'; // get rid of ! |
1751 | } | 1751 | } |
diff --git a/include/libbb.h b/include/libbb.h index 2937b4863..0ceb983cd 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -218,6 +218,7 @@ int klogctl(int type, char * b, int len); | |||
218 | 218 | ||
219 | char *xgetcwd(char *cwd); | 219 | char *xgetcwd(char *cwd); |
220 | char *concat_path_file(const char *path, const char *filename); | 220 | char *concat_path_file(const char *path, const char *filename); |
221 | int last_char_is(const char *s, const int c); | ||
221 | 222 | ||
222 | typedef struct ar_headers_s { | 223 | typedef struct ar_headers_s { |
223 | char *name; | 224 | char *name; |
diff --git a/libbb/last_char_is.c b/libbb/last_char_is.c new file mode 100644 index 000000000..b4bb7ec32 --- /dev/null +++ b/libbb/last_char_is.c | |||
@@ -0,0 +1,33 @@ | |||
1 | /* | ||
2 | * busybox library eXtended function | ||
3 | * | ||
4 | * Copyright (C) 2001 Larry Doolittle, <ldoolitt@recycle.lbl.gov> | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | * General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License | ||
17 | * along with this program; if not, write to the Free Software | ||
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
19 | * | ||
20 | */ | ||
21 | |||
22 | #include "libbb.h" | ||
23 | |||
24 | /* Find out if the last character of a string matches the one given Don't | ||
25 | * underrun the buffer if the string length is 0. Also avoids a possible | ||
26 | * space-hogging inline of strlen() per usage. | ||
27 | */ | ||
28 | int last_char_is(const char *s, const int c) | ||
29 | { | ||
30 | int l = strlen(s); | ||
31 | if (l==0) return 0; | ||
32 | return (s[l-1] == c); | ||
33 | } | ||
diff --git a/libbb/libbb.h b/libbb/libbb.h index 2937b4863..0ceb983cd 100644 --- a/libbb/libbb.h +++ b/libbb/libbb.h | |||
@@ -218,6 +218,7 @@ int klogctl(int type, char * b, int len); | |||
218 | 218 | ||
219 | char *xgetcwd(char *cwd); | 219 | char *xgetcwd(char *cwd); |
220 | char *concat_path_file(const char *path, const char *filename); | 220 | char *concat_path_file(const char *path, const char *filename); |
221 | int last_char_is(const char *s, const int c); | ||
221 | 222 | ||
222 | typedef struct ar_headers_s { | 223 | typedef struct ar_headers_s { |
223 | char *name; | 224 | char *name; |
@@ -706,7 +706,7 @@ static int readTarFile(int tarFd, int extractFlag, int listFlag, | |||
706 | case REGTYPE0: | 706 | case REGTYPE0: |
707 | /* If the name ends in a '/' then assume it is | 707 | /* If the name ends in a '/' then assume it is |
708 | * supposed to be a directory, and fall through */ | 708 | * supposed to be a directory, and fall through */ |
709 | if (header.name[strlen(header.name)-1] != '/') { | 709 | if (last_char_is(header.name,'/')) { |
710 | if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE) | 710 | if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE) |
711 | errorFlag=TRUE; | 711 | errorFlag=TRUE; |
712 | break; | 712 | break; |
@@ -19,7 +19,7 @@ | |||
19 | */ | 19 | */ |
20 | 20 | ||
21 | char *vi_Version = | 21 | char *vi_Version = |
22 | "$Id: vi.c,v 1.4 2001/04/16 15:46:44 andersen Exp $"; | 22 | "$Id: vi.c,v 1.5 2001/04/26 15:56:47 andersen Exp $"; |
23 | 23 | ||
24 | /* | 24 | /* |
25 | * To compile for standalone use: | 25 | * To compile for standalone use: |
@@ -1745,7 +1745,7 @@ static void colon(Byte * buf) | |||
1745 | while (isblnk(*buf)) | 1745 | while (isblnk(*buf)) |
1746 | buf++; | 1746 | buf++; |
1747 | strcpy((char *) args, (char *) buf); | 1747 | strcpy((char *) args, (char *) buf); |
1748 | if (cmd[strlen((char *) cmd) - 1] == '!') { | 1748 | if (last_char_is((char *)cmd,'!')) { |
1749 | useforce = TRUE; | 1749 | useforce = TRUE; |
1750 | cmd[strlen((char *) cmd) - 1] = '\0'; // get rid of ! | 1750 | cmd[strlen((char *) cmd) - 1] = '\0'; // get rid of ! |
1751 | } | 1751 | } |