From 7abf933fda68040b1ed240efc635698f13fbb1d5 Mon Sep 17 00:00:00 2001 From: markj Date: Wed, 5 Sep 2018 00:30:34 +0000 Subject: [PATCH] MFC r338375: sed: Fix -i option behavior with 'q' command. PR: 230507 --- sys/kern/imgact_elf.c | 8 +++++++- usr.bin/sed/extern.h | 2 ++ usr.bin/sed/main.c | 5 +++-- usr.bin/sed/process.c | 12 ++++++++---- usr.bin/sed/tests/sed2_test.sh | 18 ++++++++++++++++++ 5 files changed, 38 insertions(+), 7 deletions(-) diff --git a/sys/kern/imgact_elf.c b/sys/kern/imgact_elf.c index 3eed4f53ee5..ab9447c903c 100644 --- a/sys/kern/imgact_elf.c +++ b/sys/kern/imgact_elf.c @@ -839,7 +839,8 @@ __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp) break; case PT_INTERP: /* Path to interpreter */ - if (phdr[i].p_filesz > MAXPATHLEN) { + if (phdr[i].p_filesz < 2 || + phdr[i].p_filesz > MAXPATHLEN) { uprintf("Invalid PT_INTERP\n"); error = ENOEXEC; goto ret; @@ -870,6 +871,11 @@ __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp) } else { interp = __DECONST(char *, imgp->image_header) + phdr[i].p_offset; + if (interp[interp_name_len - 1] != '\0') { + uprintf("Invalid PT_INTERP\n"); + error = ENOEXEC; + goto ret; + } } break; case PT_GNU_STACK: diff --git a/usr.bin/sed/extern.h b/usr.bin/sed/extern.h index 6aca9d9efe6..8ba8e62d1ff 100644 --- a/usr.bin/sed/extern.h +++ b/usr.bin/sed/extern.h @@ -44,6 +44,8 @@ extern int aflag, eflag, nflag; extern const char *fname, *outfname; extern FILE *infile, *outfile; extern int rflags; /* regex flags to use */ +extern const char *inplace; +extern int quit; void cfclose(struct s_command *, struct s_command *); void compile(void); diff --git a/usr.bin/sed/main.c b/usr.bin/sed/main.c index 23072053803..7d700f6d3e9 100644 --- a/usr.bin/sed/main.c +++ b/usr.bin/sed/main.c @@ -101,6 +101,7 @@ FILE *outfile; /* Current output file */ int aflag, eflag, nflag; int rflags = 0; +int quit = 0; static int rval; /* Exit status */ static int ispan; /* Whether inplace editing spans across files */ @@ -114,7 +115,7 @@ const char *fname; /* File name. */ const char *outfname; /* Output file name */ static char oldfname[PATH_MAX]; /* Old file name (for in-place editing) */ static char tmpfname[PATH_MAX]; /* Temporary file name (for in-place editing) */ -static const char *inplace; /* Inplace edit file extension. */ +const char *inplace; /* Inplace edit file extension. */ u_long linenum; static void add_compunit(enum e_cut, char *); @@ -334,7 +335,7 @@ mf_fgets(SPACE *sp, enum e_spflag spflag) } for (;;) { - if (infile != NULL && (c = getc(infile)) != EOF) { + if (infile != NULL && (c = getc(infile)) != EOF && !quit) { (void)ungetc(c, infile); break; } diff --git a/usr.bin/sed/process.c b/usr.bin/sed/process.c index 8607ff5fb86..5da7bd741e1 100644 --- a/usr.bin/sed/process.c +++ b/usr.bin/sed/process.c @@ -207,10 +207,14 @@ process(void) } break; case 'q': - if (!nflag && !pd) - OUT(); - flush_appends(); - exit(0); + if (inplace == NULL) { + if (!nflag && !pd) + OUT(); + flush_appends(); + exit(0); + } + quit = 1; + break; case 'r': if (appendx >= appendnum) if ((appends = realloc(appends, diff --git a/usr.bin/sed/tests/sed2_test.sh b/usr.bin/sed/tests/sed2_test.sh index a32f09d4bba..9acd628a8f1 100755 --- a/usr.bin/sed/tests/sed2_test.sh +++ b/usr.bin/sed/tests/sed2_test.sh @@ -38,6 +38,7 @@ inplace_hardlink_src_body() atf_check ln a b atf_check sed -i '' -e 's,foo,bar,g' b atf_check -o 'inline:bar\n' -s exit:0 cat b + atf_check -s not-exit:0 stat -q '.!'* } atf_test_case inplace_symlink_src @@ -50,10 +51,27 @@ inplace_symlink_src_body() echo foo > a atf_check ln -s a b atf_check -e not-empty -s not-exit:0 sed -i '' -e 's,foo,bar,g' b + atf_check -s not-exit:0 stat -q '.!'* +} + +atf_test_case inplace_command_q +inplace_command_q_head() +{ + atf_set "descr" "Verify -i works correctly with the 'q' command" +} +inplace_command_q_body() +{ + printf '1\n2\n3\n' > a + atf_check -o 'inline:1\n2\n' sed '2q' a + atf_check sed -i.bak '2q' a + atf_check -o 'inline:1\n2\n' cat a + atf_check -o 'inline:1\n2\n3\n' cat a.bak + atf_check -s not-exit:0 stat -q '.!'* } atf_init_test_cases() { + atf_add_test_case inplace_command_q atf_add_test_case inplace_hardlink_src atf_add_test_case inplace_symlink_src } -- 2.45.0