At $WORK we hit a bug where ${var%/*} was not producing the correct expansion. There are two patches to fix this. I prefer the first as I feel it is cleaner from an API perspective. I've also added a regression testcase that shows the problem. Thoughts? -- -- David (obrien_at_FreeBSD.org) Commit log: 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". Currently 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(). ----------%<----------%<----------%<----------%<----------%<---------- Index: bin/sh/memalloc.h =================================================================== --- bin/sh/memalloc.h (revision 213714) +++ bin/sh/memalloc.h (working copy) _at__at_ -67,9 +67,16 _at__at_ 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] Index: bin/sh/histedit.c =================================================================== --- bin/sh/histedit.c (revision 213714) +++ bin/sh/histedit.c (working copy) _at__at_ -418,7 +418,7 _at__at_ fc_replace(const char *s, char *p, char } else STPUTC(*s++, dest); } - STACKSTRNUL(dest); + STPUTC('\0', dest); dest = grabstackstr(dest); return (dest); Index: bin/sh/memalloc.c =================================================================== --- bin/sh/memalloc.c (revision 213714) +++ bin/sh/memalloc.c (working copy) _at__at_ -295,6 +295,12 _at__at_ grabstackblock(int len) * is space for at least one character. */ +static char * +growstrstackblock(int n) { + growstackblock(); + sstrnleft = stackblocksize() - n; + return stackblock() + n; +} char * growstackstr(void) _at__at_ -304,12 +310,10 _at__at_ growstackstr(void) len = stackblocksize(); if (herefd >= 0 && len >= 1024) { xwrite(herefd, stackblock(), len); - sstrnleft = len - 1; + sstrnleft = len; return stackblock(); } - growstackblock(); - sstrnleft = stackblocksize() - len - 1; - return stackblock() + len; + return growstrstackblock(len); } _at__at_ -323,9 +327,7 _at__at_ makestrspace(void) int len; len = stackblocksize() - sstrnleft; - growstackblock(); - sstrnleft = stackblocksize() - len; - return stackblock() + len; + return growstrstackblock(len); } ----------%<----------%<----------%<----------%<----------%<---------- ALTERNATE PATCH: Index: bin/sh-1/memalloc.h =================================================================== --- bin/sh-1/memalloc.h (revision 213696) +++ bin/sh-1/memalloc.h (working copy) _at__at_ -70,7 +70,17 _at__at_ void ungrabstackstr(char *, char *); #define STPUTC(c, p) (--sstrnleft >= 0? (*p++ = (c)) : (p = growstackstr(), *p++ = (c))) #define CHECKSTRSPACE(n, p) { if (sstrnleft < n) p = makestrspace(); } #define USTPUTC(c, p) (--sstrnleft, *p++ = (c)) -#define STACKSTRNUL(p) (sstrnleft == 0? (p = growstackstr(), *p = '\0') : (*p = '\0')) +/* + * growstackstr() has the assumption that a "precious" character will be + * immediately written into it, so it sets up 'sstrnleft' appropriately. + * But that is not the case for STACKSTRNUL, 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 (without adjusting 'sstrnleft'). + * 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', ++sstrnleft) : (*p = '\0')) +//#define STACKSTRNUL(p) (sstrnleft == 0? (p = growstackstr(), *p = '\0') : (*p = '\0')) #define STUNPUTC(p) (++sstrnleft, --p) #define STTOPC(p) p[-1] #define STADJUST(amount, p) (p += (amount), sstrnleft -= (amount)) Index: bin/sh-1/histedit.c =================================================================== --- bin/sh-1/histedit.c (revision 213696) +++ bin/sh-1/histedit.c (working copy) _at__at_ -418,7 +418,8 _at__at_ fc_replace(const char *s, char *p, char } else STPUTC(*s++, dest); } - STACKSTRNUL(dest); + //STACKSTRNUL(dest); + STPUTC('\0', dest); dest = grabstackstr(dest); return (dest); ----------%<----------%<----------%<----------%<----------%<---------- Index: tools/regression/bin/sh/expansion/trim4.0 =================================================================== --- tools/regression/bin/sh/expansion/trim4.0 (revision 0) +++ tools/regression/bin/sh/expansion/trim4.0 (revision 0) _at__at_ -0,0 +1,12 _at__at_ +# $FreeBSD$ + +J1="/homes/SOME_USER" + +J2="A567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567891123456789112345678911234567891123456789112345678911234567891123456789112345678911234567891234567890123456789012345678901234567A567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567891123456789112345678911234567891123456789112345678911234567891123456789112345678911234567890123" + +J3="C1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 HI" + +# Trigger bug in VSTRIMRIGHT processing STADJUST() call in expand.c:subevalvar() +J4="${J2} ${J1%/*} $J3" + +echo $J4 Property changes on: tools/regression/bin/sh/expansion/trim4.0 ___________________________________________________________________ Added: svn:keywords + FreeBSD=%H Index: tools/regression/bin/sh/expansion/trim4.0.stdout =================================================================== --- tools/regression/bin/sh/expansion/trim4.0.stdout (revision 0) +++ tools/regression/bin/sh/expansion/trim4.0.stdout (revision 0) _at__at_ -0,0 +1 _at__at_ +A567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567891123456789112345678911234567891123456789112345678911234567891123456789112345678911234567891234567890123456789012345678901234567A567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567891123456789112345678911234567891123456789112345678911234567891123456789112345678911234567890123 /homes C1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 HIReceived on Tue Oct 12 2010 - 00:51:30 UTC
This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:40:08 UTC