From 8626a6ac7dff93bf97f754c93c3e426ee5c6b1e0 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Fri, 14 Apr 2023 22:50:39 -0500 Subject: [PATCH] Treat space and newline the same, and don't theoretically overrun toybuf by 5 bytes if kernel returns something weird. --- toys/other/oneit.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/toys/other/oneit.c b/toys/other/oneit.c index 13f58078..c0b0c09d 100644 --- a/toys/other/oneit.c +++ b/toys/other/oneit.c @@ -72,15 +72,15 @@ void oneit_main(void) // Autodetect console from sysfs if no -c memcpy(toybuf, "/dev/", 5); - if (!TT.c && (TT.c = readfile("/sys/class/tty/console/active", ss, 4096))) { + i = sizeof(toybuf)-6; + if (!TT.c && (TT.c = readfile("/sys/class/tty/console/active", ss, i))) { // Take last entry, remove newline terminator - for (;;) { - if (!(ss = strchr(TT.c, ' '))) break; - if (!ss[1]) *ss = 0; - else TT.c = ++ss; + while (TT.c[i = strcspn(TT.c, " \n")]) { + TT.c[i++] = 0; + if (TT.c[i]) TT.c += i; + else break; } - if (ss = strchr(TT.c, '\n')) ss[1] = 0; - // Ensure /dev prefix + // Ensure exactly one /dev prefix strstart(&TT.c, "/dev/"); memmove(toybuf+5, TT.c, strlen(TT.c)); TT.c = toybuf; -- 2.39.2