[BusyBox] 14 bugs on the wall
Larry Doolittle
ldoolitt at recycle.lbl.gov
Fri Apr 20 20:38:53 UTC 2001
Cuts the number of strlen() operations per file from 4 to 1.
Better performance, and saves 47 bytes of text size, since
strcpy is inlined (at least in my build environment).
Add paranoia check to avoid buffer underrun. By my count,
the original 14 (potential) bugs on the wall are down to 10.
--- cvs/busybox/ls.c Fri Apr 6 10:09:54 2001
+++ ls.c Fri Apr 20 13:22:52 2001
@@ -530,6 +530,7 @@
struct dirent *entry;
DIR *dir;
int i, nfiles;
+ size_t path_len, d_name_len;
if (path==NULL) return(NULL);
@@ -541,18 +542,25 @@
status = EXIT_FAILURE;
return(NULL); /* could not open the dir */
}
+ path_len=strlen(path);
while ((entry = readdir(dir)) != NULL) {
/* are we going to list the file- it may be . or .. or a hidden file */
if ((strcmp(entry->d_name, ".")==0) && !(disp_opts & DISP_DOT)) continue;
if ((strcmp(entry->d_name, "..")==0) && !(disp_opts & DISP_DOT)) continue;
if ((entry->d_name[0] == '.') && !(disp_opts & DISP_HIDDEN)) continue;
cur= (struct dnode *)xmalloc(sizeof(struct dnode));
- cur->fullname = xmalloc(strlen(path)+1+strlen(entry->d_name)+1);
- strcpy(cur->fullname, path);
- if (cur->fullname[strlen(cur->fullname)-1] != '/')
- strcat(cur->fullname, "/");
- cur->name= cur->fullname + strlen(cur->fullname);
- strcat(cur->fullname, entry->d_name);
+ d_name_len=strlen(entry->d_name);
+ /* include space for '/' and trailing '\0' */
+ cur->fullname = xmalloc(path_len+d_name_len+2);
+ /* don't bother copying trailing first '\0', it would just get overwritten */
+ memcpy(cur->fullname, path, path_len);
+ cur->name = cur->fullname + path_len;
+ /* check on path_len > 0 might be useless */
+ if (path_len > 0 && cur->fullname[path_len-1] != '/') {
+ *cur->name++='/';
+ }
+ /* definitely copy trailing '\0'! */
+ memcpy(cur->name, entry->d_name, d_name_len+1);
if (my_stat(cur))
continue;
cur->next= dn;
More information about the busybox
mailing list