Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make make(1): clean up memory handling in CondParser...



details:   https://anonhg.NetBSD.org/src/rev/f38fe170653e
branches:  trunk
changeset: 948190:f38fe170653e
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Dec 20 13:03:48 2020 +0000

description:
make(1): clean up memory handling in CondParser_String

diffstat:

 usr.bin/make/cond.c |  74 ++++++++++++++++++++--------------------------------
 1 files changed, 29 insertions(+), 45 deletions(-)

diffs (185 lines):

diff -r cef0aa0f9a12 -r f38fe170653e usr.bin/make/cond.c
--- a/usr.bin/make/cond.c       Sun Dec 20 12:53:34 2020 +0000
+++ b/usr.bin/make/cond.c       Sun Dec 20 13:03:48 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cond.c,v 1.227 2020/12/20 12:46:49 rillig Exp $        */
+/*     $NetBSD: cond.c,v 1.228 2020/12/20 13:03:48 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -94,7 +94,7 @@
 #include "dir.h"
 
 /*     "@(#)cond.c     8.2 (Berkeley) 1/2/94"  */
-MAKE_RCSID("$NetBSD: cond.c,v 1.227 2020/12/20 12:46:49 rillig Exp $");
+MAKE_RCSID("$NetBSD: cond.c,v 1.228 2020/12/20 13:03:48 rillig Exp $");
 
 /*
  * The parsing of conditional expressions is based on this grammar:
@@ -394,12 +394,10 @@
 /* coverity:[+alloc : arg-*4] */
 static void
 CondParser_String(CondParser *par, Boolean doEval, Boolean strictLHS,
-                 const char **out_str, void **out_str_freeIt,
-                 Boolean *out_quoted)
+                 FStr *out_str, Boolean *out_quoted)
 {
        Buffer buf;
-       const char *str;
-       void *str_freeIt;
+       FStr str;
        Boolean atStart;
        const char *nested_p;
        Boolean quoted;
@@ -408,14 +406,13 @@
        VarParseResult parseResult;
 
        Buf_Init(&buf);
-       str = NULL;
-       str_freeIt = NULL;
+       str = FStr_InitRefer(NULL);
        *out_quoted = quoted = par->p[0] == '"';
        start = par->p;
        if (quoted)
                par->p++;
 
-       while (par->p[0] != '\0' && str == NULL) {
+       while (par->p[0] != '\0' && str.str == NULL) {
                switch (par->p[0]) {
                case '\\':
                        par->p++;
@@ -454,26 +451,22 @@
                        nested_p = par->p;
                        atStart = nested_p == start;
                        parseResult = Var_Parse(&nested_p, VAR_CMDLINE, eflags,
-                                               &str, &str_freeIt);
+                                               &str.str, &str.freeIt);
                        /* TODO: handle errors */
-                       if (str == var_Error) {
+                       if (str.str == var_Error) {
                                if (parseResult & VPR_ANY_MSG)
                                        par->printedError = TRUE;
-                               if (str_freeIt != NULL) {
-                                       /*
-                                        * XXX: Can there be any situation
-                                        * in which a returned var_Error
-                                        * requires freeIt?
-                                        */
-                                       free(str_freeIt);
-                                       str_freeIt = NULL;
-                               }
+                               /*
+                                * XXX: Can there be any situation in which
+                                * a returned var_Error requires freeIt?
+                                */
+                               FStr_Done(&str);
                                /*
                                 * Even if !doEval, we still report syntax
                                 * errors, which is what getting var_Error
                                 * back with !doEval means.
                                 */
-                               str = NULL;
+                               str = FStr_InitRefer(NULL);
                                goto cleanup;
                        }
                        par->p = nested_p;
@@ -487,12 +480,9 @@
                        if (atStart && is_separator(par->p[0]))
                                goto cleanup;
 
-                       Buf_AddStr(&buf, str);
-                       if (str_freeIt != NULL) {
-                               free(str_freeIt);
-                               str_freeIt = NULL;
-                       }
-                       str = NULL;     /* not finished yet */
+                       Buf_AddStr(&buf, str.str);
+                       FStr_Done(&str);
+                       str = FStr_InitRefer(NULL); /* not finished yet */
                        continue;
                default:
                        if (strictLHS && !quoted && *start != '$' &&
@@ -501,7 +491,7 @@
                                 * The left-hand side must be quoted,
                                 * a variable reference or a number.
                                 */
-                               str = NULL;
+                               str = FStr_InitRefer(NULL);
                                goto cleanup;
                        }
                        Buf_AddByte(&buf, par->p[0]);
@@ -510,15 +500,10 @@
                }
        }
 got_str:
-       {
-               char *bufstr = Buf_GetAll(&buf, NULL);
-               str = bufstr;
-               str_freeIt = bufstr;
-       }
+       str = FStr_InitOwn(Buf_GetAll(&buf, NULL));
 cleanup:
        Buf_Destroy(&buf, FALSE);
        *out_str = str;
-       *out_str_freeIt = str_freeIt;
 }
 
 struct If {
@@ -644,17 +629,16 @@
 CondParser_Comparison(CondParser *par, Boolean doEval)
 {
        Token t = TOK_ERROR;
-       const char *lhs, *op, *rhs;
-       void *lhs_freeIt, *rhs_freeIt;
+       FStr lhs, rhs;
+       const char *op;
        Boolean lhsQuoted, rhsQuoted;
 
        /*
         * Parse the variable spec and skip over it, saving its
         * value in lhs.
         */
-       CondParser_String(par, doEval, lhsStrict,
-           &lhs, &lhs_freeIt, &lhsQuoted);
-       if (lhs == NULL)
+       CondParser_String(par, doEval, lhsStrict, &lhs, &lhsQuoted);
+       if (lhs.str == NULL)
                goto done_lhs;
 
        CondParser_SkipWhitespace(par);
@@ -672,7 +656,7 @@
                break;
        default:
                /* Unknown operator, compare against an empty string or 0. */
-               t = ToToken(doEval && EvalNotEmpty(par, lhs, lhsQuoted));
+               t = ToToken(doEval && EvalNotEmpty(par, lhs.str, lhsQuoted));
                goto done_lhs;
        }
 
@@ -685,8 +669,8 @@
                goto done_lhs;
        }
 
-       CondParser_String(par, doEval, FALSE, &rhs, &rhs_freeIt, &rhsQuoted);
-       if (rhs == NULL)
+       CondParser_String(par, doEval, FALSE, &rhs, &rhsQuoted);
+       if (rhs.str == NULL)
                goto done_rhs;
 
        if (!doEval) {
@@ -694,12 +678,12 @@
                goto done_rhs;
        }
 
-       t = EvalCompare(lhs, lhsQuoted, op, rhs, rhsQuoted);
+       t = EvalCompare(lhs.str, lhsQuoted, op, rhs.str, rhsQuoted);
 
 done_rhs:
-       free(rhs_freeIt);
+       FStr_Done(&rhs);
 done_lhs:
-       free(lhs_freeIt);
+       FStr_Done(&lhs);
        return t;
 }
 



Home | Main Index | Thread Index | Old Index