]> git.armaanb.net Git - sic.git/blobdiff - util.c
fix an out-of-bounds read if the input is ""
[sic.git] / util.c
diff --git a/util.c b/util.c
index 5412efc52eff19b697fdb2bbef23dbf70bfbd8a2..25678120422eea5ec0782e9040fdd5ad24b9ff45 100644 (file)
--- a/util.c
+++ b/util.c
@@ -40,42 +40,28 @@ dial(char *host, char *port) {
        return srv;
 }
 
-#define strlcpy _strlcpy
-static void
-strlcpy(char *to, const char *from, int l) {
-       memccpy(to, from, '\0', l);
-       to[l-1] = '\0';
-}
-
-static void
-eat(char **s, int (*p)(int), int r) {
-       char *q;
-
-       for(q = *s; *q && p(*q) == r; q++)
-               ;
-       *s = q;
+static char *
+eat(char *s, int (*p)(int), int r) {
+       while(*s != '\0' && p(*s) == r)
+               s++;
+       return s;
 }
 
 static char*
-tok(char **s) {
-       char *p;
-
-       eat(s, isspace, 1);
-       p = *s;
-       eat(s, isspace, 0);
-       if(**s)
-               *(*s)++ = '\0';
-       return p;
+skip(char *s, char c) {
+       while(*s != c && *s != '\0')
+               s++;
+       if(*s != '\0')
+               *s++ = '\0';
+       return s;
 }
 
-static char*
-ctok(char **s, int c) {
-       char *p, *q;
+static void
+trim(char *s) {
+       char *e;
 
-       q = *s;
-       for(p = q; *p && *p != c; p++)
-               ;
-       if(*p) *p++ = '\0';
-       *s = p;
-       return q;
+       e = s + strlen(s) - 1;
+       while(e > s && isspace(*e))
+               e--;
+       *(e + 1) = '\0';
 }