summary refs log tree commit diff
path: root/src/bltin/test.1 (unfollow)
Commit message (Collapse)Author
2014-12-26man: Clarify two redirection mechanismsStéphane Aulery
Close Debian Bug #501566 Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-11-17[BUILTIN] Fix "test -x" as root on FreeBSD 8Jonathan Nieder
POSIX.1-2008 §4.4 "File Access Permission" sayeth: If execute permission is requested, access shall be granted if execute permission is granted to at least one user by the file permission bits or by an alternate access control mechanism; otherwise, access shall be denied. For historical reasons, POSIX unfortunately also allows access() and faccessat() to return success for X_OK if the current process is privileged, even when the above condition is not fulfilled and actual execution would fail. On the affected platforms, "test -x <path>" as root started returning true on nonexecutable files when dash switched from its own emulation to the true faccessat in v0.5.7~54 (2010-04-02). Work around this by checking the permissions bits when mode == X_OK and geteuid() == 0 on such platforms. Unfortunately the behavior seems to vary from one kernel version to another, so we cannot just check the behavior at compile time and rely on that. A survey of some affected kernels: - NetBSD's kernel moved to the sane semantics in 1997 - OpenBSD's kernel made the same change in version 4.4, three years ago - FreeBSD 9's kernel fixes this but hasn't been released yet It seems safe to only apply the workaround on systems using the FreeBSD kernel for now, and to push for standardization on the expected access()/faccessat() semantics so we can drop the workaround altogether in a few years. To try it on other platforms, use "./configure --enable-test-workaround". Reported-by: Christoph Egger <christoph@debian.org> Analysis-by: Petr Salinger <Petr.Salinger@seznam.cz> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-11-17[MAN] Document redirection file descriptor limitationStéphane Aulery
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-11-17[MAN] Correct typo in manual pageStéphane Aulery
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-30[PARSER] Catch variable length expansions on non-existant specialsHerbert Xu
Currently we only check special variable names that follow directly after $ or ${. So errors such as ${#&} are not caught. This patch fixes that by moving the is_special check to just before we print out the special variable name. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-28[PARSER] Simplify EOF/newline handling in list parserHerbert Xu
This patch simplifies the EOF and new handling in the list parser. In particular, it eliminates a case where we may leave here-documents unfinished upon EOF. It also removes special EOF/newline handling from parsecmd. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-28[PARSER] Removed unnecessary pungetc on EOF from parserHerbert Xu
Doing a pungetc on an EOF is a noop and is only useful when we don't know what character we're putting back. This patch removes an unnecessary pungetc when we know it's EOF. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[BUILTIN] Handle -- in dotcmdHerbert Xu
This patch adds a nextopt call in dotcmd in order to handle --. Reported-by: Stephane Chazelas <stephane_chazelas@yahoo.fr> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[BUILTIN] Simplify echo commandHerbert Xu
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[BUILTIN] Optimise handling of backslash octals in printfHerbert Xu
This patch removes the duplicate octal handling for %b by reusing the existing code in conv_escape. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[BUILTIN] Use error instead of warnx for fatal errors in printfHerbert Xu
This patch replaces uses of warnx where we abort with error since the effect is the same. The exit status however changes from 1 to 2. Non-fatal errors where we continue are unchanged. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[BUILTIN] Remove getintmax in printfHerbert Xu
This patch removes getintmax and moves its functionality into getuintmax in order to reduce code duplication. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[BUILTIN] Remove unnecessary restoration of format string in printfHerbert Xu
Currently we try to preserve the format string which is stored in argv after temporarily modifying it. This is unnecessary as it's only ever used once. This patch gets rid of it. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[OUTPUT] Add ifdefs around MEM_OUT handling in outmemHerbert Xu
MEM_OUT is only used by forkless backtick processing which we do not currently support. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[OUTPUT] Add likely tag in outmemHerbert Xu
The branch in outmem where the string fits in the buffer is the common case and is now marked as likely. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[INPUT] Replace open-coded flushall in preadbufferHerbert Xu
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[BUILTIN] Handle embedded NULs correctly in printfHerbert Xu
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=379227 On Sat, Jul 22, 2006 at 12:48:38PM +0200, A Mennucc wrote: > Package: dash > Version: 0.5.3-3 > Severity: normal > > hi > > here are the examples > > $ bash -c 'echo -n -e "A\0102C\00D\0E" | hexdump -c' > 0000000 A B C \0 D \0 E > 0000007 > > $ /bin/echo -n -e "A\0102C\00D\0E" | hexdump -c > 0000000 A B C \0 D \0 E > 0000007 > > $ zsh -c 'echo -n -e "A\0102C\00D\0E" | hexdump -c' > 0000000 A B C \0 D \0 E > 0000007 > > $ dash -c 'echo -n "A\0102C\00D\0E" | hexdump -c' > 0000000 A B C > 0000003 > > and also > > $ dash -c 'echo -n "ABC\0DEFGH" | hexdump -c' > 0000000 A B C > 0000003 > > As you see, dash 's builtin echo truncates the output at the first \0 > > a. > > -- System Information: > Debian Release: testing/unstable > APT prefers unstable > APT policy: (500, 'unstable'), (500, 'testing') > Architecture: i386 (i686) > Shell: /bin/sh linked to /bin/bash > Kernel: Linux 2.6.16-1-k7 > Locale: LANG=it_IT.UTF-8, LC_CTYPE=it_IT.UTF-8 (charmap=UTF-8) > > Versions of packages dash depends on: > ii libc6 2.3.6-15 GNU C Library: Shared libraries > > dash recommends no packages. > > -- debconf information: > * dash/sh: false > > -- > Andrea Mennucc > "E' un mondo difficile. Che vita intensa!" (Tonino Carotone) This patch fixes handling of embedded NULs using an approach similar to the one taken by NetBSD. In particular, we first determine the length of the output string, and then use a sequence of Xs of the same length as input to the underlying C printf to determine the amount of leading and trailing padding. Finally we replace the Xs with the actual string before writing it out. In order to print out the temporary string containing Xs and padding, a new helper xasprintf is added. Unlike asprintf though, our xasprintf prints to the ash stack rather than using straight malloc memory. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[BUILTIN] Add printf support for format string a, A, and FHerbert Xu
This patch adds the format string characters a, A and F to the supported set of the built-in printf command. They're already supported by the underlying printf function. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[CD] support drive letters on CygwinEric Blake
The Cygwin platform supports DOS style drive-letter paths such as "C:\\dir", even though the preferred form is a POSIX-style "/cygdrive/c/dir". This can be seen by doing things such as chdir("c:") (which succeeds) followed by getcwd(NULL, 0) (which returns the normalized "/cygdrive/c"). However, dash was trying to perform local manipulations on the argument to 'cd' prior to calling into libc, in order to update the state of $PWD and friends; these manipulations were assuming that the user meant to change to a relative subdirectory of the current location, as in './c:', instead of honoring the drive letter. None of the other dash builtins take a filename and manipulate it to affect shell state (some, like 'test', take a file name, but as stat("c:") works just fine, there is no need to normalize). This patch has no impact outside of cygwin; on cygwin, it takes advantage of a native function call to canonicalize any incoming name into preferred form before updating shell state. Pre-patch: $ dash -c 'cd c: && echo $PWD' dash: 1: cd: can't cd to c: Post-patch: $ dash -c 'cd c: && echo $PWD' /cygdrive/c Signed-off-by: Eric Blake <eblake@redhat.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-08[EXPAND] Optimise nulonly away and just use quoted as beforeHerbert Xu
This patch makes a small optimisation by using the same value for quoted between evalvar and varvalue by eliminating nulonly and passing along quoted instead. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-08[EXPAND] Do not split quoted VSLENGTH and VSTRIMHerbert Xu
Currently VSLENGTH and VSTRIM* are field-split even within quotes. This is obviously wrong. This patch fixes that. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-08[EXPAND] Split unquoted $@/$* correctly when IFS is set but emptyHerbert Xu
Currently we do not field-split $@/$* when it isn't quoted and IFS is set but empty. This is obviously wrong. This patch fixes this. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-07[VAR] Use setvareq to set OPTIND initiallyHerbert Xu
There is no need to setvarint to set the initial value of OPTIND of one. This patch switchs to setvareq which also lets us avoid an unnecessary memory allocation. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-06[BUILTIN] Return without arguments in a trap should use status outside trapsHerbert Xu
POSIX now requires that return without arguments in a trap should return the last command status prior to executing traps. This patch implements this behaviour. Incidentally this also changes the behaviour of return without arguments in a loop conditional to use the last exit status in the body as opposed to the last command in the conditional when there is one. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-06[BUILTIN] Allow return in loop conditional to set exit statusHerbert Xu
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=332954 When return is used in a loop conditional the exit status will be lost because we always set the exit status at the end of the loop to that of the last command executed in the body. This is counterintuitive and contrary to what most other shells do. This patch fixes this by always preserving the exit status of return when it is used in a loop conditional. The patch was originally written by Gerrit Pape <pape@smarden.org>. Reported-by: Stephane Chazelas <stephane_chazelas@yahoo.fr> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-06[EVAL] Move common skipcount logic into skiploopHerbert Xu
The functions evalloop and evalfor share the logic on checking and updating skipcount. This patch moves that into the helper function skiploop. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-06[BUILTIN] Do not allow break to break across function callsHerbert Xu
As it is if you do a multi-level break inside a function it'll actually include loops outside of the function call. This is counterintuitive. This patch changes this by saving and resetting loopnest when entering a function. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-06[BUILTIN] Exit without arguments in a trap should use status outside trapsHerbert Xu
POSIX now requires that exit without arguments in a trap should return the last command status prior to executing traps. This patch implements this behaviour. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-03[EVAL] Do not clobber exitstatus in evalcommandHerbert Xu
All originators of EXERROR have been setting the exitstatus for a while now. So it is no longer appropriate to set it explicitly in evalcommand. In fact doing so may cause the original exitstatus to be lost. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-02[ERROR] Set exitstatus in onintHerbert Xu
Currently the exit status when we receive SIGINT is set in evalcommand which means that it doesn't always get set. For example, if you press CTRL-C at the prompt of an interactive dash, the exit status is not set to 130 as it is in many other Bourne shells. This patch fixes this by moving the setting of the exit status into onint which also simplifies evalcommand. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-02[TRAP] Make sure evalskip is zero before running trapsHerbert Xu
As it is if dotrap is called with evalskip set to a nonzero value, it'll try to execute any set traps. The result is that the first command in the first set trap will be executed while the rest of the trap will be silently ignored due to evalskip. This is highly counterintuitive, even though both bash and ksh exhibit a similar behaviour. This patch fixes it by skipping trap processing if evalskip is set on entry. It also adds a dotrap call to the top of evaltree to ensure that while continue; do continue; done has a chance of running traps. Finally the pendingsigs check is moved into dotrap for compactness. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-02[EVAL] Fix use-after-free in dotrap/evalstringHerbert Xu
The function dotrap calls evalstring using the stored trap string. If evalstring then unsets that exact trap string then we will end up using freed memory. This patch fixes it by making evalstring always duplicate the string before using it. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-09-29[PARSER] Add nlprompt/nlnoprompt helpersHerbert Xu
This patch adds the nlprompt/nlnoprompt helpers to isolate code dealing with newlines and prompting. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-09-29[PARSER] Handle backslash newlines properly after dollar signHerbert Xu
On Tue, Aug 26, 2014 at 12:34:42PM +0000, Eric Blake wrote: > On 08/26/2014 06:15 AM, Oleg Bulatov wrote: > > Hi! > > > > While playing with sh generators I found that dash and bash have different > > interpretations for <slash><newline> sequence. > > > > $ dash -c 'EDIT=xxx; echo $EDIT\ > >> OR' > > xxxOR > > Buggy. > > > $ bash -c 'EDIT=xxx; echo $EDIT\ > > OR' > > /usr/bin/vim > > Correct behavior. > > > > > $ dash -c 'echo "$\ > > (pwd)"' > > $(pwd) > > > > Is it undefined behaviour in POSIX? > > No, it's well-defined, and dash is buggy. POSIX says: > > http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_03 > > "the shell shall break its input into tokens by applying the first > applicable rule below to the next character in its input" > > Rule 4 covers backslash handling, while rule 5 covers locating the end > of a word to be subject to $ expansion. Therefore, rule 4 should happen > first. Rule 4 defers to the section on quoting, with the caveat that > <newline> joining is the only substitution that happens immediately as > part of the parsing: > > http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_02 > > "If a <newline> follows the <backslash>, the shell shall interpret this > as line continuation. The <backslash> and <newline> shall be removed > before splitting the input into tokens. Since the escaped <newline> is > removed entirely from the input and is not replaced by any white space, > it cannot serve as a token separator." > > So the fact that dash is treating the elided backslash-newline as a > token separator, and parsing your input as if ${EDIT}OR instead of > ${EDITOR} is a bug in dash. I agree. This patch should resolve this problem and similar ones affecting blackslash newlines after we encounter a dollar sign. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-09-29[INPUT] Kill pgetc_macroHerbert Xu
pgetc_macro is identical to pgetc except that it's a macro and pgetc isn't. Since there is very little performance difference on modern systems it's time to kill pgetc_macro. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-09-28[BUILTIN] Correctly handle test ! ! = !Herbert Xu
This patch adds a special case in testcmd for the 4-argument expression beginning with a !. Without this ! ! = ! is deemed a syntax error, which breaks POSIX. Note that this special case does not extend down into subexpressions so if ! ! = ! is used inside parentheses then a syntax error will still occur as before. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-09-26Release 0.5.8.Herbert Xu
2014-09-26[MAN] Clarify "trap '' signals" syntax in manual pageJonathan Nieder
When the manpage states | <action> may be null, which cause the specified signals to be ignored. it is not immediately obvious what it means for an action to be null. Clarify by explicitly referring to an empty string, as opposed to a NULL pointer or the string "null". Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-09-26[MAN] 0 and EXIT both work for exit trapsAdam Buchbinder
I saw a discussion in which there was some confusion over whether or not you can use a symbolic name, since the manpage doesn't specifically say so. Signed-off-by: Adam Buchbinder <adam.buchbinder@gmail.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-09-26[MAN] Change characters for printf precision to bytesHerbert Xu
On Sun, Mar 09, 2014 at 11:11:43AM +0000, Jeroen van Dijke wrote: > > There seems to be a bug in the dash man page, at least in 0.5.7. It reads: > > Precision: > An optional period, `.', followed by an optional digit string giving a precision which specifies the number of digits to appear after the decimal point, for e and f formats, or the maximum number of *characters* to be printed from a string (b and s for- > mats); if the digit string is missing, the precision is treated as zero; > > dash behaves cuts to the number of bytes > > $ length=10; printf "%.${length}s\n" "eeeeeeeeeeeeeeeeeeeeeeeee" > eeeeeeeeee > $ length=10; printf "%.${length}s\n" "ëëëëëëëëëëëëëëëëëëëëëëëëë” > ëëëëë > > > The POSIX specification (2008) says: > > precision Gives the minimum number of digits to appear for the d, o, i, u, x, or X conversion specifiers (the field is padded with leading zeros), the number of digits to appear after the radix character for the e and f conversion specifiers, the maximum number of significant digits for the g conversion specifier; or the maximum number of *bytes* to be written from a string in the s conversion specifier. The precision shall take the form of a ( '.' ) followed by a decimal digit string; a null digit string is treated as zero. > > So it seems to me that “characters” should be changed to “bytes”. Indeed and this patch makes that change. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-09-26[JOBS] Fix off-by-one error for multiple of four job numbersHarald van Dijk
On 29/07/13 23:44, Luigi Tarenga wrote: > hi list, > while writing a script to execute parallel ssh command on many host I found > a strange behavior of dash. I can replicate it with a very simple script but > didn't find any documentation about dash or POSIX that can explain it. > > tested on centos 6.4 (dash 0.5.5.1) and wih dash compiled from source (0.5.7) > the following script reports error: > > #!/bin/dash > > sleep 3 & > sleep 3 & > sleep 3 & > sleep 3 & > > #/bin/true > jobs -l > > wait %1 > wait %2 > wait %3 > wait %4 > > [vortex@lizard ~]$ ./dash-0.5.7/src/dash test.sh > [4] + 4569 Running > [3] - 4568 Running > [2] 4567 Running > [1] 4566 Running > prova: 14: wait: No such job: %4 > [vortex@lizard ~]$ echo $? > 2 Yes, this looks like a bug to me. The number of allocated jobs is always kept as a multiple of four, and the first check in considering whether the job number is valid is "if it's greater than or equal to the number of allocated job, it's invalid". That doesn't look right. That would only be right if jobs were zero-based, but they aren't. If it's exactly equal to the number of available jobs, it can still be valid. It works when adding /bin/true, because four more more jobs end up allocated internally. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-09-26[BUILTIN] Set command -p path to /usr/sbin:/usr/bin:/sbin:/binHerbert Xu
Exclude /usr/local from command -p PATH. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-09-26[BUILTIN] Small optimisation of command -pv changeHerbert Xu
This patch moves the pathval call into the describe_command function and also eliminates an unnecessary branch when DEBUG is off. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-09-26[BUILTIN] command: allow combining -p with -vHarald van Dijk
On 10/07/13 20:18, Craig Loomis wrote: > Dash (0.5.7 and git master) does not implement 'command -p' > according to the standard, and opens an intriguing security hole to > anyone trying this scheme. > > When using 'command -v' to simply print the path to an executable, > '-p' has no effect: You're right. dash has never supported combining -p with -v, but back in 2005 this was seemingly accidentally changed from reporting a syntax error to silently ignoring the -p option, only about a month after dash moved to git. Making sure that -p is respected even when -v is used is easy enough, see attached patch. Tested even with explicit PATH overrides: PATH=/path/to/some/other/dash command -pv dash correctly outputs /bin/dash on my system. > the path that 'command -p cmd' uses is a compiled-in constant > from dash's src/var.c:defpathvar, which starts with > "/usr/local/sbin:/usr/local/bin". To me, that is both completely > unexpected and pretty scary -- /usr/local/bin is (very) often less > well secured or checked than, say, /bin: Agreed. However, IMO, it does make sense for defpathvar to start with /usr/local/*: it has two separate functions, it also serves as the default path (hence the name) when dash is started with no PATH set at all. I think fixing this should be done in a way so that command -p does not use defpathvar, not by changing defpathvar. bash uses the same confstr function for this that getconf uses, and it shouldn't be too much work to make dash use that too. If no one else comes up with a working patch or a better approach, I'll try to get that working. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2013-08-23[BUILTIN] Fixed argument parsing crash in testHerbert Xu
When nexpr gets an unexpected EOI, this may cause crashes further up the call chain because we've advanced t_wp too far. Fix it by checking for EOI in nexpr and only advancing t_wp if we've got more arguments. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2013-08-23[VAR] Initialise OPTIND after importing environmentHerbert Xu
On Sat, Mar 23, 2013 at 01:46:20AM +0000, Chris F.A. Johnson wrote: > > According to both the dash man page and the POSIX spec, "When the > shell is invoked, OPTIND is initialized to 1." > > However, it actually takes the value of the environment variable > if it exists: > > $ OPTIND=4 dash -c 'echo "$OPTIND"' > 4 > $ OPTIND=4 bash -c 'echo "$OPTIND"' > 1 > $ OPTIND=4 ksh -c 'echo "$OPTIND"' > 1 > $ OPTIND=4 ksh93 -c 'echo "$OPTIND"' > 1 This patch fixes this by initialising OPTIND after importing the environment. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2013-08-23[VAR] Add newline when tracing in poplocalvarsPeter Rosin
Signed-off-by: Peter Rosin <peda@lysator.liu.se> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2013-08-23[MAN] Markup fixes in manual for mandoc 1.12.1Jérémie Courrèges-Anglas
with Aleksey we briefly discussed the mdoc compatibility of the manpage, here's a patch that makes mandoc 1.12.1 happier and behaves correctly against groff 1.21. I want to include it in the staging dash-0.5.7 OpenBSD port. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2013-08-23[BUILTIN] Use PRIdMAX instead of %j in printfHarald van Dijk
On 12/03/2012 05:59 PM, Harald van Dijk wrote: > On 12/03/2012 08:42 AM, Roy wrote: >> MSYS libc does not support %j[dXx] format, only %ll[dXx] is supported. >> >> diff --git a/src/bltin/printf.c b/src/bltin/printf.c >> index 893295c..12ce660 100644 >> --- a/src/bltin/printf.c >> +++ b/src/bltin/printf.c >> @@ -319,11 +319,12 @@ mklong(const char *str, const char *ch) >> char *copy; >> size_t len; >> >> - len = ch - str + 3; >> + len = ch - str + 4; >> STARTSTACKSTR(copy); >> copy = makestrspace(len, copy); >> - memcpy(copy, str, len - 3); >> - copy[len - 3] = 'j'; >> + memcpy(copy, str, len - 4); >> + copy[len - 4] = 'l'; >> + copy[len - 3] = 'l'; >> copy[len - 2] = *ch; >> copy[len - 1] = '\0'; >> return (copy); > > The calling code uses the result to print intmax_t and uintmax_t values. > Printing intmax_t values with %lld is wrong, this will only work if > intmax_t is really a typedef for long long (which may be true on your > system, but is not required by the standard). > > The other patch that Jonathan linked to should work just fine. Here's a slightly tweaked version of that patch. Regardless of whether PRIdMAX is defined as "jd" or as "lld", the use of memcpy here, first copying "jd"/"lld" and the null byte, and only changing the 'd' after that, surprisingly results in slightly shorter object code than the original byte-by-byte approach, even though memcpy is fully inlined. Perhaps that could be a reason for applying this, even if the original reason for it, making the code work on not-quite-conforming systems, isn't good enough to get it in dash. Tested with normal glibc, and with glibc hacked to not provide PRIdMAX. Reviewed-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2013-08-23[EXPAND] Propagate EXP_QPAT in subevalvarHerbert Xu
On Tue, Aug 28, 2012 at 01:27:24PM +0000, Todor Vlaev wrote: > > While playing around with parameter expansion I noticed that the > following didn't work in dash (dash 0.5.5.1-7.4ubuntu1) as compared > to bash even though I believe it should be POSIX-compliant: > > my_str=swan; last_char="${my_str#${my_str%?}}"; echo ${last_char} > > If the double quotes are removed, the last character is printed correctly. > > At a quick glance through the commits after the 0.5.5.1 release I saw > the following bug fix. Could it be related? > > 0d7d66039b614b642c775432fd64aa8c11f9a64d > [EXPAND] Fix quoted pattern patch breakage We need to propagate EXP_QPAT in subevalvar as otherwise a nested parameter expansion within subevalvar may be expanded incorrectly. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>