Mon Nov 22 05:21:54 2021 UTC ()
PR bin/53550

/bin/sh's processing of here doc expansions has changed.   Now it happens
in the context of the parent shell, so side effects are visible there,
just like all other redirection expansions.

We need to stop testing that that doesn't happen, and instead test
that it does.   This is that change.

Add another test case which is testing exactly the example from the PR
(well, with a different exit status, 1 is too generic and could happen
by accident) to make sure we don't reintroduce that bug sometime.


(kre)
diff -r1.8 -r1.9 src/tests/bin/sh/t_here.sh

cvs diff -r1.8 -r1.9 src/tests/bin/sh/t_here.sh (expand / switch to unified diff)

--- src/tests/bin/sh/t_here.sh 2021/09/09 00:04:51 1.8
+++ src/tests/bin/sh/t_here.sh 2021/11/22 05:21:54 1.9
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1# $NetBSD: t_here.sh,v 1.8 2021/09/09 00:04:51 kre Exp $ 1# $NetBSD: t_here.sh,v 1.9 2021/11/22 05:21:54 kre Exp $
2# 2#
3# Copyright (c) 2007 The NetBSD Foundation, Inc. 3# Copyright (c) 2007 The NetBSD Foundation, Inc.
4# All rights reserved. 4# All rights reserved.
5# 5#
6# Redistribution and use in source and binary forms, with or without 6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions 7# modification, are permitted provided that the following conditions
8# are met: 8# are met:
9# 1. Redistributions of source code must retain the above copyright 9# 1. Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer. 10# notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright 11# 2. Redistributions in binary form must reproduce the above copyright
12# notice, this list of conditions and the following disclaimer in the 12# notice, this list of conditions and the following disclaimer in the
13# documentation and/or other materials provided with the distribution. 13# documentation and/or other materials provided with the distribution.
14# 14#
@@ -505,41 +505,81 @@ quoting_body() { @@ -505,41 +505,81 @@ quoting_body() {
505 check 'p=AB??; cat <<-EOF 505 check 'p=AB??; cat <<-EOF
506 ${p%B?D} 506 ${p%B?D}
507 ${p%B\??} 507 ${p%B\??}
508 ${p%"B??"} 508 ${p%"B??"}
509 "${p%??}" 509 "${p%??}"
510 ${p#"${p%??}"} 510 ${p#"${p%??}"}
511 "${p#"${p%?"?"}"}" 511 "${p#"${p%?"?"}"}"
512 EOF 512 EOF
513 ' 'AB?? A A "AB" ?? "??"' 0 513 ' 'AB?? A A "AB" ?? "??"' 0
514 514
515 results 515 results
516} 516}
517 517
 518#
 519# This next test is really just testing what our shell happens to do.
 520# There doesn't seem to be any spec on in which context expansions
 521# in redirects are processed. Most shells do them in the parent
 522# shell context, meaning that side effects of the expansion become
 523# visible to the shell - a couple process redirect expansions in
 524# a subshell, meaning that side effects are lost.
 525#
 526# Before PR bin/53550 was fixed, the NetBSD sh evaluated all redirect
 527# expansions, except here documents, in the context of the shell, and
 528# here document redirects in a subshell context. That distinction
 529# makes no real sense (and only an old, and maybe still current, FreeBSD
 530# shell shares that pecadillo.) Afer that fix, the NetBSD shell joins
 531# almost all others in expanding redirects (all of them) in the shell
 532# context, meaning that side effects of here documenty expansions become
 533# visible in the shell.
 534#
 535# Before the fix, we used to get "2\n1\n" as the output from this
 536# test, now the variable assignment in the here document persists
 537# and we get "2\n2\n" as do most other shells. (bash is a notable
 538# exception, but it does all redirect expansions in a subshell context)
 539#
 540
518atf_test_case side_effects 541atf_test_case side_effects
519side_effects_head() { 542side_effects_head() {
520 atf_set "descr" "Tests how side effects in here documents are handled" 543 atf_set "descr" "Tests how side effects in here documents are handled"
521} 544}
522side_effects_body() { 545side_effects_body() {
523 546
524 atf_check -s exit:0 -o inline:'2\n1\n' -e empty ${TEST_SH} -c ' 547 atf_check -s exit:0 -o inline:'2\n2\n' -e empty ${TEST_SH} -c '
525 unset X 548 unset X
526 cat <<-EOF 549 cat <<-EOF
527 ${X=2} 550 ${X=2}
528 EOF 551 EOF
529 echo "${X-1}" 552 echo "${X-1}"
530 ' 553 '
531} 554}
532 555
 556# This is a test for the specific bug reported in PR bin/53550
 557# This should work in any shell.
 558
 559atf_test_case exit_status
 560exit_status_head() {
 561 atf_set descr "Tests exit status of a command substitution in a heredoc"
 562}
 563exit_status_body() {
 564
 565 # PR bin/53550 test
 566 atf_check -s exit:7 -o empty -e empty ${TEST_SH} -c '
 567 <<-EOF
 568 $(exit 7)
 569 EOF
 570 '
 571}
 572
533# The following tests a problem reported on the austin-list 2021-09-08 573# The following tests a problem reported on the austin-list 2021-09-08
534# by oguzismailuysal@gmail.com ... it affected all ash derived shells 574# by oguzismailuysal@gmail.com ... it affected all ash derived shells
535atf_test_case hard_cases 575atf_test_case hard_cases
536hard_cases_head() { 576hard_cases_head() {
537 atf_set "descr" \ 577 atf_set "descr" \
538 "Tests here docs in positions that have confised our parser" 578 "Tests here docs in positions that have confised our parser"
539} 579}
540hard_cases_body() { 580hard_cases_body() {
541 581
542 atf_check -s exit:0 -o inline:'xxx\n' -e empty ${TEST_SH} -c ' 582 atf_check -s exit:0 -o inline:'xxx\n' -e empty ${TEST_SH} -c '
543 : <<- do | for x in xxx 583 : <<- do | for x in xxx
544 do 584 do
545 do echo $x 585 do echo $x
@@ -613,22 +653,23 @@ vicious_body() { @@ -613,22 +653,23 @@ vicious_body() {
613 echo line 5 653 echo line 5
614 DASH_CODE 654 DASH_CODE
615 END_SCRIPT 655 END_SCRIPT
616 656
617 # we will just verify that the shell can parse the 657 # we will just verify that the shell can parse the
618 # script somehow, and doesn't fall over completely... 658 # script somehow, and doesn't fall over completely...
619 659
620 atf_check -s exit:0 -o ignore -e empty ${TEST_SH} script 660 atf_check -s exit:0 -o ignore -e empty ${TEST_SH} script
621} 661}
622 662
623atf_init_test_cases() { 663atf_init_test_cases() {
624 atf_add_test_case do_simple # not worthy of a comment 664 atf_add_test_case do_simple # not worthy of a comment
625 atf_add_test_case end_markers # the mundane, the weird, the bizarre 665 atf_add_test_case end_markers # the mundane, the weird, the bizarre
 666 atf_add_test_case exit_status # PR bin/53550, cmdsub in heredoc
626 atf_add_test_case incomplete # where the end marker isn't... 667 atf_add_test_case incomplete # where the end marker isn't...
627 atf_add_test_case lineends # test weird line endings in heredocs 668 atf_add_test_case lineends # test weird line endings in heredocs
628 atf_add_test_case multiple # multiple << operators on one cmd 669 atf_add_test_case multiple # multiple << operators on one cmd
629 atf_add_test_case nested # here docs inside here docs 670 atf_add_test_case nested # here docs inside here docs
630 atf_add_test_case quoting # stuff quoted inside 671 atf_add_test_case quoting # stuff quoted inside
631 atf_add_test_case side_effects # here docs that modify environment 672 atf_add_test_case side_effects # here docs that modify environment
632 atf_add_test_case hard_cases # here doc bodies appearing mid command 673 atf_add_test_case hard_cases # here doc bodies appearing mid command
633 atf_add_test_case vicious # evil test from the austin-l list... 674 atf_add_test_case vicious # evil test from the austin-l list...
634} 675}