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
This commit is contained in:
Eric Andersen 2001-04-26 15:56:47 +00:00
parent 3c3277f0bd
commit c1bdffe99b
12 changed files with 46 additions and 11 deletions

View File

@ -248,7 +248,7 @@ process_escape_sequence.c read_package_field.c read_text_file_to_buffer.c \
recursive_action.c safe_read.c safe_strncpy.c seek_ared_file.c syscalls.c \ recursive_action.c safe_read.c safe_strncpy.c seek_ared_file.c syscalls.c \
syslog_msg_with_name.c time_string.c trim.c untar.c unzip.c vdprintf.c \ syslog_msg_with_name.c time_string.c trim.c untar.c unzip.c vdprintf.c \
verror_msg.c vperror_msg.c wfopen.c xfuncs.c xgetcwd.c xregcomp.c interface.c \ verror_msg.c vperror_msg.c wfopen.c xfuncs.c xgetcwd.c xregcomp.c interface.c \
remove_file.c remove_file.c last_char_is.c
LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC)) LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC))
LIBBB_CFLAGS = -I$(LIBBB) LIBBB_CFLAGS = -I$(LIBBB)
ifneq ($(strip $(BB_SRC_DIR)),) ifneq ($(strip $(BB_SRC_DIR)),)

View File

@ -583,7 +583,7 @@ static int status_merge(void *status, package_t *pkgs)
*/ */
if ((fin = fopen(statusfile, "r")) != NULL) { if ((fin = fopen(statusfile, "r")) != NULL) {
while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) {
line[strlen(line) - 1] = '\0'; /* trim newline */ chomp(line); /* trim newline */
/* If we see a package header, find out if it's a package /* If we see a package header, find out if it's a package
* that we have processed. if so, we skip that block for * that we have processed. if so, we skip that block for
* now (write it at the end). * now (write it at the end).

View File

@ -706,7 +706,7 @@ static int readTarFile(int tarFd, int extractFlag, int listFlag,
case REGTYPE0: case REGTYPE0:
/* If the name ends in a '/' then assume it is /* If the name ends in a '/' then assume it is
* supposed to be a directory, and fall through */ * supposed to be a directory, and fall through */
if (header.name[strlen(header.name)-1] != '/') { if (last_char_is(header.name,'/')) {
if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE) if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE)
errorFlag=TRUE; errorFlag=TRUE;
break; break;

View File

@ -75,7 +75,7 @@ static void decompose_list(const char *list)
/* handle multi-value cases */ /* handle multi-value cases */
else if (nminus == 1) { else if (nminus == 1) {
/* handle 'N-' case */ /* handle 'N-' case */
if (list[strlen(list) - 1] == '-') { if (last_char_is(list,'-')) {
startpos = strtol(list, &ptr, 10); startpos = strtol(list, &ptr, 10);
} }
/* handle '-M' case */ /* handle '-M' case */

2
cut.c
View File

@ -75,7 +75,7 @@ static void decompose_list(const char *list)
/* handle multi-value cases */ /* handle multi-value cases */
else if (nminus == 1) { else if (nminus == 1) {
/* handle 'N-' case */ /* handle 'N-' case */
if (list[strlen(list) - 1] == '-') { if (last_char_is(list,'-')) {
startpos = strtol(list, &ptr, 10); startpos = strtol(list, &ptr, 10);
} }
/* handle '-M' case */ /* handle '-M' case */

2
dpkg.c
View File

@ -583,7 +583,7 @@ static int status_merge(void *status, package_t *pkgs)
*/ */
if ((fin = fopen(statusfile, "r")) != NULL) { if ((fin = fopen(statusfile, "r")) != NULL) {
while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) {
line[strlen(line) - 1] = '\0'; /* trim newline */ chomp(line); /* trim newline */
/* If we see a package header, find out if it's a package /* If we see a package header, find out if it's a package
* that we have processed. if so, we skip that block for * that we have processed. if so, we skip that block for
* now (write it at the end). * now (write it at the end).

View File

@ -19,7 +19,7 @@
*/ */
char *vi_Version = char *vi_Version =
"$Id: vi.c,v 1.4 2001/04/16 15:46:44 andersen Exp $"; "$Id: vi.c,v 1.5 2001/04/26 15:56:47 andersen Exp $";
/* /*
* To compile for standalone use: * To compile for standalone use:
@ -1745,7 +1745,7 @@ static void colon(Byte * buf)
while (isblnk(*buf)) while (isblnk(*buf))
buf++; buf++;
strcpy((char *) args, (char *) buf); strcpy((char *) args, (char *) buf);
if (cmd[strlen((char *) cmd) - 1] == '!') { if (last_char_is((char *)cmd,'!')) {
useforce = TRUE; useforce = TRUE;
cmd[strlen((char *) cmd) - 1] = '\0'; // get rid of ! cmd[strlen((char *) cmd) - 1] = '\0'; // get rid of !
} }

View File

@ -218,6 +218,7 @@ int klogctl(int type, char * b, int len);
char *xgetcwd(char *cwd); char *xgetcwd(char *cwd);
char *concat_path_file(const char *path, const char *filename); char *concat_path_file(const char *path, const char *filename);
int last_char_is(const char *s, const int c);
typedef struct ar_headers_s { typedef struct ar_headers_s {
char *name; char *name;

33
libbb/last_char_is.c Normal file
View File

@ -0,0 +1,33 @@
/*
* busybox library eXtended function
*
* Copyright (C) 2001 Larry Doolittle, <ldoolitt@recycle.lbl.gov>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "libbb.h"
/* Find out if the last character of a string matches the one given Don't
* underrun the buffer if the string length is 0. Also avoids a possible
* space-hogging inline of strlen() per usage.
*/
int last_char_is(const char *s, const int c)
{
int l = strlen(s);
if (l==0) return 0;
return (s[l-1] == c);
}

View File

@ -218,6 +218,7 @@ int klogctl(int type, char * b, int len);
char *xgetcwd(char *cwd); char *xgetcwd(char *cwd);
char *concat_path_file(const char *path, const char *filename); char *concat_path_file(const char *path, const char *filename);
int last_char_is(const char *s, const int c);
typedef struct ar_headers_s { typedef struct ar_headers_s {
char *name; char *name;

2
tar.c
View File

@ -706,7 +706,7 @@ static int readTarFile(int tarFd, int extractFlag, int listFlag,
case REGTYPE0: case REGTYPE0:
/* If the name ends in a '/' then assume it is /* If the name ends in a '/' then assume it is
* supposed to be a directory, and fall through */ * supposed to be a directory, and fall through */
if (header.name[strlen(header.name)-1] != '/') { if (last_char_is(header.name,'/')) {
if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE) if (tarExtractRegularFile(&header, extractFlag, tostdoutFlag)==FALSE)
errorFlag=TRUE; errorFlag=TRUE;
break; break;

4
vi.c
View File

@ -19,7 +19,7 @@
*/ */
char *vi_Version = char *vi_Version =
"$Id: vi.c,v 1.4 2001/04/16 15:46:44 andersen Exp $"; "$Id: vi.c,v 1.5 2001/04/26 15:56:47 andersen Exp $";
/* /*
* To compile for standalone use: * To compile for standalone use:
@ -1745,7 +1745,7 @@ static void colon(Byte * buf)
while (isblnk(*buf)) while (isblnk(*buf))
buf++; buf++;
strcpy((char *) args, (char *) buf); strcpy((char *) args, (char *) buf);
if (cmd[strlen((char *) cmd) - 1] == '!') { if (last_char_is((char *)cmd,'!')) {
useforce = TRUE; useforce = TRUE;
cmd[strlen((char *) cmd) - 1] = '\0'; // get rid of ! cmd[strlen((char *) cmd) - 1] = '\0'; // get rid of !
} }