Bug 9186 - sha256sum in a script causes following commands to be executed twice
Summary: sha256sum in a script causes following commands to be executed twice
Status: RESOLVED FIXED
Alias: None
Product: Busybox
Classification: Unclassified
Component: Other (show other bugs)
Version: unspecified
Hardware: Other Linux
: P5 normal
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-08-18 16:18 UTC by David Jarvie
Modified: 2016-08-22 08:44 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:


Attachments
Config file used to build Busybox (31.89 KB, text/plain)
2016-08-18 16:18 UTC, David Jarvie
Details

Note You need to log in before you can comment on or make changes to this bug.
Description David Jarvie 2016-08-18 16:18:29 UTC
Created attachment 6611 [details]
Config file used to build Busybox

Busybox version 1.25, running in OpenWRT.

When the sha256sum command is executed in a script, all the following commands in the script are executed, and then they are all executed a second time. There is no loop in the script. A workaround is to put the sha256sum command into a little script by itself, and to call that script from the main script.

Example script:

#!/bin/sh
echo START
#tar tvJf /dev/mtd3
sha256sum -c app.sha256
echo MIDDLE
echo END

Executing this script produces the following output:

START
sha256sum: app.sha256: No such file or directory
MIDDLE
END
MIDDLE
END
Comment 1 David Jarvie 2016-08-18 16:37:07 UTC
The Busybox version is actually 1.26.0.git (2016-08-17 09:15:25 BST)
Comment 2 Denys Vlasenko 2016-08-19 16:43:46 UTC
Fixed in git, please try it:

commit 215b0ca6e4fe466c6942d21a1bba62d97f2d5e5d
Author: Denys Vlasenko <vda.linux@googlemail.com>
Date:   Fri Aug 19 18:23:56 2016 +0200

    hush: fix a bug in FEATURE_SH_STANDALONE=y config. Closes 9186

    Run this in a "sh SCRIPT":

    sha256sum /dev/null
    echo END

    sha256sum is a NOEXEC applet. It runs in a forked child. Then child exit()s.
    By this time, entire script is read, and buffered in a FILE object
    from fopen("SCRIPT"). But fgetc() did not consume entire input.
    exit() lseeks back by -9 bytes, from <eof> to 'e' in 'echo'.
    (this may be libc-specific).
    This change of fd position *is shared with the parent*!

    Now parent can read more, and it thinks there is another "echo END".
    End result: two "echo END"s are run.

    Fix this by _exit()ing instead.
Comment 3 David Jarvie 2016-08-22 08:44:32 UTC
Yes, that fixes it.

Thank you for your impressively fast response.