Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.sbin/sushi XXX: this code needs a lot more cleanup... Th...
details: https://anonhg.NetBSD.org/src/rev/0fe856b58b20
branches: trunk
changeset: 524939:0fe856b58b20
user: christos <christos%NetBSD.org@localhost>
date: Tue Apr 02 18:59:54 2002 +0000
description:
XXX: this code needs a lot more cleanup... There is no clear malloc/free
policy and the data dependent initialization is very fragile. I.e.
the code assumes because the data entry is of type foo, it has
initialized certain fields and others not.
- make sure list is not NULL before you use it.
- don't realloc on every loop iteration!
- don't increment i when there are short lines; leads to uninitialized
list[i] entries [fixes bills' core-dump, hi bill!]
diffstat:
usr.sbin/sushi/scanform.c | 51 +++++++++++++++++++++++++++++++---------------
1 files changed, 34 insertions(+), 17 deletions(-)
diffs (107 lines):
diff -r bdc3e5cdbaf5 -r 0fe856b58b20 usr.sbin/sushi/scanform.c
--- a/usr.sbin/sushi/scanform.c Tue Apr 02 18:57:01 2002 +0000
+++ b/usr.sbin/sushi/scanform.c Tue Apr 02 18:59:54 2002 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: scanform.c,v 1.20 2001/08/03 09:18:58 itojun Exp $ */
+/* $NetBSD: scanform.c,v 1.21 2002/04/02 18:59:54 christos Exp $ */
/*
* Copyright (c) 2000 The NetBSD Foundation, Inc.
@@ -995,9 +995,13 @@
case DATAT_MLIST:
case DATAT_MFUNC:
case DATAT_MSCRIPT:
- for (i = 0, j = 0; ftp->list[i] != NULL; i++)
- if (strlen(ftp->list[i]) > j)
- j = strlen(ftp->list[i]);
+ if (ftp->list == NULL)
+ return 0;
+ for (i = 0, j = 0; ftp->list[i] != NULL; i++) {
+ size_t k;
+ if ((k = strlen(ftp->list[i])) > j)
+ j = k;
+ }
return(j);
/* NOTREACHED */
break;
@@ -1021,10 +1025,11 @@
gen_list(FTREE_ENTRY *ftp, int max, char **args)
{
int i=0;
+ int lmax = 10;
int cur;
char *p, *q;
- ftp->list = malloc(sizeof(char*));
+ ftp->list = malloc(sizeof(char*) * lmax);
if (ftp->list == NULL)
bailout("malloc: %s", strerror(errno));
for (p = ftp->data; p != NULL;) {
@@ -1036,7 +1041,10 @@
else
ftp->list[i++] = strdup(q);
}
- ftp->list = realloc(ftp->list, (i+1)*sizeof(char*));
+ if (i == lmax - 2) {
+ lmax += 10;
+ ftp->list = realloc(ftp->list, sizeof(char*) * lmax);
+ }
if (ftp->list == NULL)
bailout("realloc: %s", strerror(errno));
}
@@ -1072,9 +1080,12 @@
char *p, *q, *qo, *po, *comm, *test;
FILE *file;
char buf[PATH_MAX+30];
-/* struct stat sb; */
+#if 0
+ struct stat sb;
+#endif
size_t len;
int i, cur;
+ int lmax = 10;
qo = q = strdup(ftp->data);
comm = malloc(sizeof(char) * strlen(q) + 2);
@@ -1123,25 +1134,31 @@
if (file == NULL)
bailout("popen: %s", strerror(errno));
- ftp->list = malloc(sizeof(char *)*2);
+ ftp->list = malloc(sizeof(char *) * lmax);
if (ftp->list == NULL)
bailout("malloc: %s", strerror(errno));
- for (i = 0; (p = fgetln(file, &len)) != NULL; i++) {
- if (len == 1)
+ for (i = 0; (p = fgetln(file, &len)) != NULL;) {
+ if (len <= 1)
continue;
- p[len - 1] = '\0'; /* NUL terminate */
- ftp->list[i] = strdup(p);
- ftp->list[i][len -1] = '\0';
- ftp->list = realloc(ftp->list, sizeof(char *) * (i+2));
- if (ftp->list == NULL)
- bailout("realloc: %s", strerror(errno));
+ ftp->list[i] = malloc(len);
+ if (ftp->list[i] == NULL)
+ bailout("malloc: %s", strerror(errno));
+ memcpy(ftp->list[i], p, len);
+ ftp->list[i][len - 1] = '\0';
+ if (++i == lmax - 2) {
+ lmax += 10;
+ ftp->list = realloc(ftp->list, sizeof(char *) * lmax);
+ if (ftp->list == NULL)
+ bailout("realloc: %s", strerror(errno));
+ }
}
- ftp->list[i] = NULL;
pclose(file);
if (i == 0) {
ftp->list[0] = "";
ftp->list[1] = NULL;
+ } else {
+ ftp->list[i] = NULL;
}
free(comm);
}
Home |
Main Index |
Thread Index |
Old Index