From 5bce20565a2b921f434fa72d0b6647026e627716 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 27 Apr 2022 17:04:48 -0500 Subject: [PATCH] Slight improvement to "is binary" check. Freebsd's zlib/doc/txtvsbin.txt has an algorithm that's a little more complicated but does point out that ascii below BEL should not be in the first line of a shell script. --- toys/pending/sh.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/toys/pending/sh.c b/toys/pending/sh.c index 8f45c172..6b12dea5 100644 --- a/toys/pending/sh.c +++ b/toys/pending/sh.c @@ -3310,6 +3310,7 @@ static char *get_next_line(FILE *ff, int prompt) // TODO: ctrl-z during script read having already read partial line, // SIGSTOP and SIGTSTP need SA_RESTART, but child proc should stop // TODO if (!isspace(*new)) add_to_history(line); +// TODO: embedded nul bytes need signaling for the "tried to run binary" test. for (new = 0, len = 0;;) { errno = 0; @@ -3764,7 +3765,7 @@ int do_source(char *name, FILE *ff) // did we exec an ELF file or something? if (!TT.LINENO++ && name && new) { // A shell script's first line has no high bytes that aren't valid utf-8. - for (ii = 0; new[ii] && 0<(cc = utf8towc(&wc, new+ii, 4)); ii += cc); + for (ii = 0; new[ii]>6 && 0<(cc = utf8towc(&wc, new+ii, 4)); ii += cc); if (new[ii]) { is_binary: if (name) error_msg("'%s' is binary", name); // TODO syntax_err() exit? -- 2.39.2