diff options
author | kris <kris@FreeBSD.org> | 2000-07-31 10:49:08 +0000 |
---|---|---|
committer | kris <kris@FreeBSD.org> | 2000-07-31 10:49:08 +0000 |
commit | 8fb74c8657ddf57d3fb2628272cd1e07117ea21f (patch) | |
tree | f55f950d969e6e9d502164471d92c9654f09cc99 | |
parent | 157ad2beb52bfbd347b54f4a232d7c3e2377cfdd (diff) | |
download | FreeBSD-src-8fb74c8657ddf57d3fb2628272cd1e07117ea21f.zip FreeBSD-src-8fb74c8657ddf57d3fb2628272cd1e07117ea21f.tar.gz |
Don't segv when trying to add a 0-length unit name.
Some string-related cleanups inspired by OpenBSD.
Reviewed by: asmodai
-rw-r--r-- | usr.bin/units/units.c | 41 |
1 files changed, 20 insertions, 21 deletions
diff --git a/usr.bin/units/units.c b/usr.bin/units/units.c index 7d1fd78..c0ba09a 100644 --- a/usr.bin/units/units.c +++ b/usr.bin/units/units.c @@ -63,6 +63,12 @@ struct { char *NULLUNIT = ""; +#ifdef MSDOS +#define SEPERATOR ";" +#else +#define SEPARATOR ":" +#endif + int unitcount; int prefixcount; @@ -107,26 +113,17 @@ readunits(char *userfile) if (!unitfile) { char *direc, *env; char filename[1000]; - char separator[2]; env = getenv("PATH"); if (env) { - if (strchr(env, ';')) - strcpy(separator, ";"); - else - strcpy(separator, ":"); - direc = strtok(env, separator); + direc = strtok(env, SEPARATOR); while (direc) { - strcpy(filename, ""); - strncat(filename, direc, 999); - strncat(filename, "/", - 999 - strlen(filename)); - strncat(filename, UNITSFILE, - 999 - strlen(filename)); + snprintf(filename, sizeof(filename), + "%s/%s", direc, UNITSFILE); unitfile = fopen(filename, "rt"); if (unitfile) break; - direc = strtok(NULL, separator); + direc = strtok(NULL, SEPARATOR); } } if (!unitfile) @@ -286,6 +283,9 @@ addunit(struct unittype * theunit, char *toadd, int flip) char *divider, *slash; int doingtop; + if (!strlen(toadd)) + return 1; + savescr = scratch = dupstr(toadd); for (slash = scratch + 1; *slash; slash++) if (*slash == '-' && @@ -435,7 +435,7 @@ lookupunit(char *unit) copy[strlen(copy) - 1] = 0; for (i = 0; i < unitcount; i++) { if (!strcmp(unittable[i].uname, copy)) { - strcpy(buffer, copy); + strlcpy(buffer, copy, sizeof(buffer)); free(copy); return buffer; } @@ -447,7 +447,7 @@ lookupunit(char *unit) copy[strlen(copy) - 1] = 0; for (i = 0; i < unitcount; i++) { if (!strcmp(unittable[i].uname, copy)) { - strcpy(buffer, copy); + strlcpy(buffer, copy, sizeof(buffer)); free(copy); return buffer; } @@ -456,7 +456,7 @@ lookupunit(char *unit) copy[strlen(copy) - 1] = 0; for (i = 0; i < unitcount; i++) { if (!strcmp(unittable[i].uname, copy)) { - strcpy(buffer, copy); + strlcpy(buffer, copy, sizeof(buffer)); free(copy); return buffer; } @@ -469,9 +469,8 @@ lookupunit(char *unit) strlen(prefixtable[i].prefixname))) { unit += strlen(prefixtable[i].prefixname); if (!strlen(unit) || lookupunit(unit)) { - strcpy(buffer, prefixtable[i].prefixval); - strcat(buffer, " "); - strcat(buffer, unit); + snprintf(buffer, sizeof(buffer), "%s %s", + prefixtable[i].prefixval, unit); return buffer; } } @@ -652,8 +651,8 @@ main(int argc, char **argv) readunits(userfile); if (optind == argc - 2) { - strcpy(havestr, argv[optind]); - strcpy(wantstr, argv[optind + 1]); + strlcpy(havestr, argv[optind], sizeof(havestr)); + strlcpy(wantstr, argv[optind + 1], sizeof(wantstr)); initializeunit(&have); addunit(&have, havestr, 0); completereduce(&have); |