]> Trent Huber's Code - thus.git/commitdiff
Fix undefined behavior, more cleaning
authorTrent Huber <trentmhuber@gmail.com>
Thu, 20 Nov 2025 05:31:33 +0000 (00:31 -0500)
committerTrent Huber <trentmhuber@gmail.com>
Thu, 20 Nov 2025 05:31:33 +0000 (00:31 -0500)
15 files changed:
src/builtins/alias.c
src/builtins/build.c
src/builtins/builtin.c
src/builtins/builtin.h
src/builtins/cd.c
src/builtins/fg.c
src/builtins/fg.h
src/builtins/list.h [deleted file]
src/builtins/pwd.c
src/input.c
src/parse.c
src/signals.c
src/utils.c
tools/install.c
tools/uninstall.c

index 1f4a21b9e0c6ef0b7d29a6c890f2cd1091fb1bb1..42105a8aacda83554992817697c36f86254f0aff 100644 (file)
@@ -38,12 +38,11 @@ char **parsealias(char *value) {
 
    if (!value) return NULL;
 
+   c = (struct context){.b = c.buffer, .alias = 1};
    strcpy(c.buffer, value);
-   l = strlen(value);
-   c.buffer[l + 1] = '\0';
-   c.buffer[l] = ';';
-   c.b = c.buffer;
-   c.alias = 1;
+   l = strlen(c.buffer);
+   c.buffer[l++] = ';';
+   c.buffer[l] = '\0';
 
    if (!parse(&c)) return NULL;
 
index a115a3971cb3ec51a9363a7ff0292f5689a99a55..30ad34440aba556ba717d1e78ca498c90a727ab2 100644 (file)
@@ -21,8 +21,7 @@ int main(void) {
    if (!(dir = opendir("./")))
        err(EXIT_FAILURE, "Unable to open current directory");
 
-   dprintf(listfd, "#include <stddef.h>\n\n#include \"builtin.h\"\n"
-           "#include \"list.h\"\n\n");
+   dprintf(listfd, "#include <stddef.h>\n\n#include \"builtin.h\"\n\n");
 
    src = srcs;
    errno = 0;
index cf2d9148f9645bc6be7ab52d0f370a536c463268..1e005bfebff2180c46c050d05d2b55859ad1fe7f 100644 (file)
@@ -3,7 +3,6 @@
 #include <string.h>
 
 #include "builtin.h"
-#include "list.h"
 #include "utils.h"
 
 int (*getbuiltin(char *name))(char **args, size_t numargs) {
index 768ba7e1b6f74aeef253ba725d3e9540d851e4b4..0aeaf3ae5c4ce03d4394bd74fdf22a383293601e 100644 (file)
@@ -1,2 +1,7 @@
+extern struct builtin {
+   char *name;
+   int (*func)(char **args, size_t numargs);
+} builtins[];
+
 int (*getbuiltin(char *name))(char **args, size_t numargs);
 int usage(char *program, char *options);
index 12451af612d9e6b1c7bcfb7cb1f8db26ecf3a1b7..acd9c4f0ea71d28d6dd27b05180abc19dc31d5b6 100644 (file)
@@ -20,8 +20,8 @@ int cd(char **args, size_t numargs) {
            return EXIT_FAILURE;
        }
        l = strlen(buffer);
-       buffer[l + 1] = '\0';
-       buffer[l] = '/';
+       buffer[l++] = '/';
+       buffer[l] = '\0';
        break;
    default:
        return usage(args[0], "[directory]");
index 0812bee9b1c03d0a7b2e9082ec8d0e7f921b000b..5b94fc865c10828f447ad35873b1c5e281c0861e 100644 (file)
@@ -96,6 +96,7 @@ int runfg(pid_t id) {
            exit(EXIT_SUCCESS);
        }
        if (sigint) sigint = 0;
+       if (sigwinch) sigwinch = 0;
    }
    setsig(SIGCHLD, &defaultaction);
    fgjob.done = errno = 0;
@@ -123,10 +124,6 @@ int runfg(pid_t id) {
    return 1;
 }
 
-void deinitfg(void) {
-   setconfig(&canonical);
-}
-
 int fg(char **args, size_t numargs) {
    struct bgjob job;
    long l;
index f1dab387c3e994c2b5d2134bf3e22aa88798016b..1308bccdb25fc49c9180c0d18d2fea0bc4a08d21 100644 (file)
@@ -3,4 +3,3 @@ extern struct termios canonical;
 int setconfig(struct termios *mode);
 void initfg(void);
 int runfg(pid_t id);
-void deinitfg(void);
diff --git a/src/builtins/list.h b/src/builtins/list.h
deleted file mode 100644 (file)
index 0c4a403..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-struct builtin {
-   char *name;
-   int (*func)(char **args, size_t numargs);
-};
-
-extern struct builtin builtins[];
index c6a6a28b70e4b524f6fa616b73366776e764faac..6c5b9db871cad3cb99fe5dfbd698a9a8ee1f13fe 100644 (file)
@@ -18,10 +18,9 @@ int pwd(char **args, size_t numargs) {
            note("Unable to get current working directory");
            return EXIT_FAILURE;
        }
-       l = strlen(buffer);
-       if (buffer[l - 1] != '/') {
-           buffer[l] = '/';
-           buffer[l + 1] = '\0';
+       if (buffer[(l = strlen(buffer)) - 1] != '/') {
+           buffer[l++] = '/';
+           buffer[l] = '\0';
        }
        if (setenv("PWD", buffer, 1) == -1) {
            note("Unable to set $PWD$");
index fbf60b4ff69a22e03148c0717eb9c8ff1acb0d59..e6b363d1c20dfcc9698132b39fcd2c3978bac7d2 100644 (file)
@@ -32,15 +32,15 @@ int stringinput(struct context *c) {
    end = c->string;
    while (*end && *end != '\n') ++end;
    l = end - c->string;
-   while (*end == '\n') ++end; /* `scriptinput()' uses `stringinput()' */
+   while (*end == '\n') ++end;
    if (l > MAXCHARS) {
        note("Line too long, exceeds %d character limit", MAXCHARS);
        return 0;
    }
 
    strncpy(c->buffer, c->string, l);
-   c->buffer[l] = ';';
-   c->buffer[l + 1] = '\0';
+   c->buffer[l++] = ';';
+   c->buffer[l] = '\0';
    c->string = end;
 
    return 1;
index b0904098dc09a9c52b72f49f464dbccbbf6b1e40..3ed02ca28ffbe61bf54067cba48f8100f19a7cb0 100644 (file)
@@ -129,16 +129,16 @@ int parse(struct context *c) {
    case '\\':
        if (!quote) break;
        switch (*(c->b + 1)) {
+       default:
+           memmove(c->b, c->b + 1, end-- - c->b);
+           --c->b;
+           *(c->b + 1) = *c->b;
        case '$':
        case '~':
        case '"':
        case '\\':
-           break;
-       default:
-           memmove(c->b, c->b + 1, end-- - c->b--);
-           *(c->b + 1) = *c->b;
+           memmove(c->b, c->b + 1, end-- - c->b);
        }
-       memmove(c->b, c->b + 1, end-- - c->b);
        break;
    case '*':
    case '?':
index 4d658722d82abadea85964e3a488da7499d98437..3f4318d06b2a3efd7b70d5c4dcaf49974902d622 100644 (file)
@@ -5,7 +5,7 @@
 
 #include "utils.h"
 
-int sigwinch, sigquit, sigint;
+int sigquit, sigint, sigwinch;
 sigset_t shellsigmask, childsigmask;
 struct sigaction defaultaction;
 
@@ -14,12 +14,6 @@ void setsig(int sig, struct sigaction *act) {
        fatal("Unable to install %s handler", strsignal(sig));
 }
 
-static void sigwinchhandler(int sig) {
-   (void)sig;
-
-   sigwinch = 1;
-}
-
 static void sigquithandler(int sig) {
    (void)sig;
 
@@ -32,6 +26,12 @@ static void siginthandler(int sig) {
    sigint = 1;
 }
 
+static void sigwinchhandler(int sig) {
+   (void)sig;
+
+   sigwinch = 1;
+}
+
 void initsignals(void) {
    struct sigaction action;
 
@@ -46,9 +46,6 @@ void initsignals(void) {
    setsig(SIGTTOU, &defaultaction);
    setsig(SIGTTIN, &defaultaction);
 
-   action = (struct sigaction){.sa_handler = sigwinchhandler};
-   setsig(SIGWINCH, &action);
-
    action = (struct sigaction){.sa_handler = sigquithandler};
    setsig(SIGHUP, &action);
    setsig(SIGQUIT, &action);
@@ -56,4 +53,7 @@ void initsignals(void) {
 
    action = (struct sigaction){.sa_handler = siginthandler};
    setsig(SIGINT, &action);
+
+   action = (struct sigaction){.sa_handler = sigwinchhandler};
+   setsig(SIGWINCH, &action);
 }
index f7de7142fa60dbd2870baa52b253b2a08221af6a..a59c0b7e73f2e5d95ad9d41275325a13c68e68c9 100644 (file)
@@ -57,19 +57,18 @@ void init(void) {
        note("Unable to update $SHLVL$ environment variable");
 
    if (!(home = getenv("HOME"))) fatal("Unable to find home directory");
-   l = strlen(home);
-   if (home[l - 1] != '/') {
+   if (home[(l = strlen(home)) - 1] != '/') {
        strcpy(buffer, home);
-       buffer[l] = '/';
-       buffer[l + 1] = '\0';
+       buffer[l++] = '/';
+       buffer[l] = '\0';
        if (setenv("HOME", buffer, 1) == -1 || !(home = getenv("HOME")))
            note("Unable to append trailing slash to $HOME$");
    }
 
    if (!getcwd(buffer, PATH_MAX)) fatal("Unable to find current directory");
    l = strlen(buffer);
-   buffer[l] = '/';
-   buffer[l + 1] = '\0';
+   buffer[l++] = '/';
+   buffer[l] = '\0';
    if (setenv("PWD", buffer, 1) == -1)
        note("Unable to append trailing slash to $PWD$");
 
@@ -90,8 +89,7 @@ char *catpath(char *dir, char *filename, char *buffer) {
    size_t l;
    int slash;
 
-   l = strlen(dir);
-   slash = dir[l - 1] == '/';
+   slash = dir[(l = strlen(dir)) - 1] == '/';
    if (l + slash + strlen(filename) + 1 > PATH_MAX) {
        note("Path name `%s%s%s' too long", dir, slash ? "/" : "", filename);
        return NULL;
@@ -105,7 +103,7 @@ char *catpath(char *dir, char *filename, char *buffer) {
 }
 
 char *quoted(char *token) {
-   char *p, *q, quote;
+   char *p, *end, quote;
    enum {
        NONE,
        DOUBLE,
@@ -119,8 +117,8 @@ char *quoted(char *token) {
    degree = NONE;
    for (p = token; *p; ++p) switch(*p) {
    case '[':
-       for (q = p; *q; ++q) if (*q == ']') break;
-       if (!*q) continue;
+       for (end = p; *end; ++end) if (*end == ']') break;
+       if (!*end) continue;
    case '>':
    case '<':
    case '*':
@@ -146,18 +144,18 @@ char *quoted(char *token) {
    p = buffer;
    *p++ = quote;
    strcpy(p, token);
-   for (q = p; *q; ++q);
+   for (end = p; *end; ++end);
    if (degree != SINGLE) for (; *p; ++p) switch (*p)
    case '$':
    case '~':
    case '"':
        if (degree == ESCAPEDOUBLE) {
        case '\\':
-           memmove(p + 1, p, ++q - p);
+           memmove(p + 1, p, ++end - p);
            *p++ = '\\';
        }
-   *q++ = quote;
-   *q++ = '\0';
+   *end++ = quote;
+   *end = '\0';
 
    return buffer;
 }
@@ -165,5 +163,5 @@ char *quoted(char *token) {
 void deinit(void) {
    deinithistory();
    deinitbg();
-   deinitfg();
+   setconfig(&canonical);
 }
index 883514c27b710684367ed8f1e6f7fa607ef4f619..6ceb77fb4c82ee1aa3a6d0d1c8ad83b48a1c2206 100644 (file)
@@ -23,13 +23,12 @@ int main(int argc, char **argv) {
 
    strcat(path, "bin/");
    if ((cpid = fork()) == -1) err(EXIT_FAILURE, "Unable to fork");
-   else if (cpid == 0)
-       run("/bin/mkdir", LIST("mkdir", "-p", path), "create", path);
+   if (!cpid) run("/bin/mkdir", LIST("mkdir", "-p", path), "create", path);
    await(cpid, "create", path);
 
    strcat(path, "thus");
    if ((cpid = fork()) == -1) err(EXIT_FAILURE, "Unable to fork");
-   else if (cpid == 0)
+   if (!cpid)
        run("/bin/cp", LIST("cp", "-f", "bin/thus", path), "copy", "bin/thus");
    await(cpid, "copy", "bin/thus");
 
index cca930fe6f996abc331246803459c711316a1d2d..a8b190f689d0e7a20488128840dc1cbbd62b11e7 100644 (file)
@@ -12,7 +12,7 @@ int main(int argc, char **argv) {
    if (argc != 1) err(EXIT_FAILURE, "usage: %s\n", argv[0]);
 
    if ((cpid = fork()) == -1) err(EXIT_FAILURE, "Unable to fork");
-   else if (cpid == 0)
+   if (!cpid)
        run("/bin/rm", LIST("rm", STR(PATH), "uninstall"), "remove", STR(PATH));
    await(cpid, "remove", STR(PATH));