[hackers] [sbase] grep: use len returned from getline and check fmemopen() || Hiltjo Posthuma

From: <git_AT_suckless.org>
Date: Sat, 28 Mar 2015 22:03:52 +0100 (CET)

commit fea0a34e130dd2cc0f67d9ce35be31d6516a9a58
Author: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Fri Mar 27 15:54:41 2015 +0100

    grep: use len returned from getline and check fmemopen()

diff --git a/grep.c b/grep.c
index db7539c..8decd67 100644
--- a/grep.c
+++ b/grep.c
_AT_@ -10,7 +10,7 @@
 
 enum { Match = 0, NoMatch = 1, Error = 2 };
 
-static void addpattern(const char *);
+static void addpattern(const char *, size_t);
 static void addpatternfile(FILE *);
 static int grep(FILE *, const char *);
 
_AT_@ -37,38 +37,41 @@ struct pattern {
 static SLIST_HEAD(phead, pattern) phead;
 
 static void
-addpattern(const char *pattern)
+addpattern(const char *pattern, size_t patlen)
 {
         struct pattern *pnode;
         char *tmp;
         int bol, eol;
         size_t len;
 
+ if (!patlen)
+ return;
+
         /* a null BRE/ERE matches every line */
         if (!Fflag)
                 if (pattern[0] == '\0')
                         pattern = ".";
 
         if (!Fflag && xflag) {
- tmp = enmalloc(Error, strlen(pattern) + 3);
- snprintf(tmp, strlen(pattern) + 3, "%s%s%s",
+ tmp = enmalloc(Error, patlen + 3);
+ snprintf(tmp, patlen + 3, "%s%s%s",
                          pattern[0] == '^' ? "" : "^",
                          pattern,
- pattern[strlen(pattern) - 1] == '$' ? "" : "$");
+ pattern[patlen - 1] == '$' ? "" : "$");
         } else if (!Fflag && wflag) {
- len = strlen(pattern) + 5 + (Eflag ? 2 : 4);
+ len = patlen + 5 + (Eflag ? 2 : 4);
                 tmp = enmalloc(Error, len);
 
                 bol = eol = 0;
                 if (pattern[0] == '^')
                         bol = 1;
- if (pattern[strlen(pattern) - 1] == '$')
+ if (pattern[patlen - 1] == '$')
                         eol = 1;
 
                 snprintf(tmp, len, "%s\\<%s%.*s%s\\>%s",
                          bol ? "^" : "",
                          Eflag ? "(" : "\\(",
- (int)strlen(pattern) - bol - eol, pattern + bol,
+ (int)patlen - bol - eol, pattern + bol,
                          Eflag ? ")" : "\\)",
                          eol ? "$" : "");
         } else {
_AT_@ -90,7 +93,7 @@ addpatternfile(FILE *fp)
         while ((len = getline(&buf, &size, fp)) > 0) {
                 if (len > 0 && buf[len - 1] == '\n')
                         buf[len - 1] = '\0';
- addpattern(buf);
+ addpattern(buf, (size_t)len);
         }
         if (ferror(fp))
                 enprintf(Error, "read error:");
_AT_@ -190,7 +193,8 @@ main(int argc, char *argv[])
                 break;
         case 'e':
                 arg = EARGF(usage());
- fp = fmemopen(arg, strlen(arg) + 1, "r");
+ if(!(fp = fmemopen(arg, strlen(arg) + 1, "r")))
+ eprintf("fmemopen:");
                 addpatternfile(fp);
                 fclose(fp);
                 eflag = 1;
_AT_@ -239,7 +243,8 @@ main(int argc, char *argv[])
 
         /* just add literal pattern to list */
         if (!eflag && !fflag) {
- fp = fmemopen(argv[0], strlen(argv[0]) + 1, "r");
+ if(!(fp = fmemopen(argv[0], strlen(argv[0]) + 1, "r")))
+ eprintf("fmemopen:");
                 addpatternfile(fp);
                 fclose(fp);
                 argc--;
Received on Sat Mar 28 2015 - 22:03:52 CET

This archive was generated by hypermail 2.3.0 : Sat Mar 28 2015 - 22:12:23 CET