Source-Changes-HG archive

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

[src/trunk]: src/lib/libc/gen Use malloc(3) instead of the non-standard alloc...



details:   https://anonhg.NetBSD.org/src/rev/e12c30ba590d
branches:  trunk
changeset: 581145:e12c30ba590d
user:      lukem <lukem%NetBSD.org@localhost>
date:      Sat May 28 12:57:48 2005 +0000

description:
Use malloc(3) instead of the non-standard alloca(3) in MAKEEXTRALIST(),
check the return value of malloc(3) and return "" (or equiv) upon failure,
and free the result of the malloc(3).

diffstat:

 lib/libc/gen/vis.c |  59 +++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 47 insertions(+), 12 deletions(-)

diffs (170 lines):

diff -r 0f1d869af793 -r e12c30ba590d lib/libc/gen/vis.c
--- a/lib/libc/gen/vis.c        Sat May 28 12:30:39 2005 +0000
+++ b/lib/libc/gen/vis.c        Sat May 28 12:57:48 2005 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: vis.c,v 1.30 2005/05/20 01:22:48 lukem Exp $   */
+/*     $NetBSD: vis.c,v 1.31 2005/05/28 12:57:48 lukem Exp $   */
 
 /*-
  * Copyright (c) 1989, 1993
@@ -30,7 +30,7 @@
  */
 
 /*-
- * Copyright (c) 1999 The NetBSD Foundation, Inc.
+ * Copyright (c) 1999, 2005 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -64,7 +64,7 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: vis.c,v 1.30 2005/05/20 01:22:48 lukem Exp $");
+__RCSID("$NetBSD: vis.c,v 1.31 2005/05/28 12:57:48 lukem Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #include "namespace.h"
@@ -106,7 +106,8 @@
        char *e;                                                              \
        while (*o++)                                                          \
                continue;                                                     \
-       extra = alloca((size_t)((o - orig) + MAXEXTRAS));                     \
+       extra = malloc((size_t)((o - orig) + MAXEXTRAS));                     \
+       if (!extra) break;                                                    \
        for (o = orig, e = extra; (*e++ = *o++) != '\0';)                     \
                continue;                                                     \
        e--;                                                                  \
@@ -223,14 +224,20 @@
        int c, flag, nextc;
        const char *extra;
 {
-       char *nextra;
+       char *nextra = NULL;
+
        _DIAGASSERT(dst != NULL);
        _DIAGASSERT(extra != NULL);
        MAKEEXTRALIST(flag, nextra, extra);
+       if (!nextra) {
+               *dst = '\0';            /* can't create nextra, return "" */
+               return (dst);
+       }
        if (flag & VIS_HTTPSTYLE)
                HVIS(dst, c, flag, nextc, nextra);
        else
                SVIS(dst, c, flag, nextc, nextra);
+       free(nextra);
        *dst = '\0';
        return(dst);
 }
@@ -260,13 +267,17 @@
 {
        int c;
        char *start;
-       char *nextra;
+       char *nextra = NULL;
        const unsigned char *src = (const unsigned char *)csrc;
 
        _DIAGASSERT(dst != NULL);
        _DIAGASSERT(src != NULL);
        _DIAGASSERT(extra != NULL);
        MAKEEXTRALIST(flag, nextra, extra);
+       if (!nextra) {
+               *dst = '\0';            /* can't create nextra, return "" */
+               return 0;
+       }
        if (flag & VIS_HTTPSTYLE) {
                for (start = dst; (c = *src++) != '\0'; /* empty */)
                        HVIS(dst, c, flag, *src, nextra);
@@ -274,6 +285,7 @@
                for (start = dst; (c = *src++) != '\0'; /* empty */)
                        SVIS(dst, c, flag, *src, nextra);
        }
+       free(nextra);
        *dst = '\0';
        return (dst - start);
 }
@@ -289,13 +301,17 @@
 {
        unsigned char c;
        char *start;
-       char *nextra;
+       char *nextra = NULL;
        const unsigned char *src = (const unsigned char *)csrc;
 
        _DIAGASSERT(dst != NULL);
        _DIAGASSERT(src != NULL);
        _DIAGASSERT(extra != NULL);
        MAKEEXTRALIST(flag, nextra, extra);
+       if (! nextra) {
+               *dst = '\0';            /* can't create nextra, return "" */
+               return 0;
+       }
 
        if (flag & VIS_HTTPSTYLE) {
                for (start = dst; len > 0; len--) {
@@ -308,6 +324,7 @@
                        SVIS(dst, c, flag, len ? *src : '\0', nextra);
                }
        }
+       free(nextra);
        *dst = '\0';
        return (dst - start);
 }
@@ -323,12 +340,16 @@
        int c, flag, nextc;
 
 {
-       char *extra;
+       char *extra = NULL;
        unsigned char uc = (unsigned char)c;
 
        _DIAGASSERT(dst != NULL);
 
        MAKEEXTRALIST(flag, extra, "");
+       if (! extra) {
+               *dst = '\0';            /* can't create extra, return "" */
+               return (dst);
+       }
        if (flag & VIS_HTTPSTYLE)
                HVIS(dst, uc, flag, nextc, extra);
        else
@@ -354,10 +375,17 @@
        const char *src;
        int flag;
 {
-       char *extra;
+       char *extra = NULL;
+       int rv;
 
        MAKEEXTRALIST(flag, extra, "");
-       return (strsvis(dst, src, flag, extra));
+       if (!extra) {
+               *dst = '\0';            /* can't create extra, return "" */
+               return 0;
+       }
+       rv = strsvis(dst, src, flag, extra);
+       free(extra);
+       return (rv);
 }
 
 
@@ -368,9 +396,16 @@
        size_t len;
        int flag;
 {
-       char *extra;
+       char *extra = NULL;
+       int rv;
 
        MAKEEXTRALIST(flag, extra, "");
-       return (strsvisx(dst, src, len, flag, extra));
+       if (!extra) {
+               *dst = '\0';            /* can't create extra, return "" */
+               return 0;
+       }
+       rv = strsvisx(dst, src, len, flag, extra);
+       free(extra);
+       return (rv);
 }
 #endif



Home | Main Index | Thread Index | Old Index