summaryrefslogtreecommitdiffstats
path: root/bin/sh/memalloc.h
diff options
context:
space:
mode:
authorobrien <obrien@FreeBSD.org>2010-10-13 23:29:09 +0000
committerobrien <obrien@FreeBSD.org>2010-10-13 23:29:09 +0000
commit04029327665ca3c97ae07cae698d5df760f2b68e (patch)
tree21a2e1390b3cbd0e8029830cf4195a75dba0d1e7 /bin/sh/memalloc.h
parent256615c9b37c8059eef0e130fbf813607a5396a8 (diff)
downloadFreeBSD-src-04029327665ca3c97ae07cae698d5df760f2b68e.zip
FreeBSD-src-04029327665ca3c97ae07cae698d5df760f2b68e.tar.gz
Do not assume in growstackstr() that a "precious" character will be
immediately written into the stack after the call. Instead let the caller manage the "space left". Previously, growstackstr()'s assumption causes problems with STACKSTRNUL() where we want to be able to turn a stack into a C string, and later pretend the NUL is not there. This fixes a bug in STACKSTRNUL() (that grew the stack) where: 1. STADJUST() called after a STACKSTRNUL() results in an improper adjust. This can be seen in ${var%pattern} and ${var%%pattern} evaluation. 2. Memory leak in STPUTC() called after a STACKSTRNUL(). Reviewed by: jilles
Diffstat (limited to 'bin/sh/memalloc.h')
-rw-r--r--bin/sh/memalloc.h9
1 files changed, 8 insertions, 1 deletions
diff --git a/bin/sh/memalloc.h b/bin/sh/memalloc.h
index 187d4c8..563927a 100644
--- a/bin/sh/memalloc.h
+++ b/bin/sh/memalloc.h
@@ -67,9 +67,16 @@ void ungrabstackstr(char *, char *);
#define stackblock() stacknxt
#define stackblocksize() stacknleft
#define STARTSTACKSTR(p) p = stackblock(), sstrnleft = stackblocksize()
-#define STPUTC(c, p) (--sstrnleft >= 0? (*p++ = (c)) : (p = growstackstr(), *p++ = (c)))
+#define STPUTC(c, p) (--sstrnleft >= 0? (*p++ = (c)) : (p = growstackstr(), --sstrnleft, *p++ = (c)))
#define CHECKSTRSPACE(n, p) { if (sstrnleft < n) p = makestrspace(); }
#define USTPUTC(c, p) (--sstrnleft, *p++ = (c))
+/*
+ * STACKSTRNUL's use is where we want to be able to turn a stack
+ * (non-sentinel, character counting string) into a C string,
+ * and later pretend the NUL is not there.
+ * Note: Because of STACKSTRNUL's semantics, STACKSTRNUL cannot be used
+ * on a stack that will grabstackstr()ed.
+ */
#define STACKSTRNUL(p) (sstrnleft == 0? (p = growstackstr(), *p = '\0') : (*p = '\0'))
#define STUNPUTC(p) (++sstrnleft, --p)
#define STTOPC(p) p[-1]
OpenPOWER on IntegriCloud