Bug 274254 - The /bin/sh $LINENO value just after a nested function is misleading.
Summary: The /bin/sh $LINENO value just after a nested function is misleading.
Status: Open
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 13.2-RELEASE
Hardware: Any Any
: --- Affects Some People
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-10-03 21:34 UTC by white-pack
Modified: 2024-02-12 14:28 UTC (History)
5 users (show)

See Also:


Attachments
A sample script for inspiring a fix. (1.86 KB, text/plain)
2023-10-03 21:34 UTC, white-pack
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description white-pack 2023-10-03 21:34:28 UTC
Created attachment 245417 [details]
A sample script for inspiring a fix.

The following script reveals the issue

 1  #!/bin/sh
 2
 3  check()
 4  {
 5      echo $1 - \
 6      $(
 7          [ $2 -eq $3 ] \
 8              && echo OK \
 9              || echo FAIL "($2 != $3)"
10      )
11  }
12
13  main()
14  {
15      check 2 $LINENO 3
16
17      inner()
18      {
19          check 4 $LINENO 3
20      }
21
22      check 3 $LINENO 10
23      inner
24  }
25
26  check 1 $LINENO 26
27  main

Here's its output:

1 - OK
2 - OK
3 - FAIL (22 != 10)
4 - OK

Every line of the output should be "OK", but the third one fails.

The internal logic controlling LINENO seems to ignore that inner() was nested.

I attach an ad-hoc "work-around" which could inspire a possible fix.
Comment 1 Mark Johnston freebsd_committer freebsd_triage 2023-10-13 13:44:04 UTC
dash seems to handle this properly.
Comment 2 Collin Funk 2024-02-10 03:40:20 UTC
Was bored and looking for interesting bugs to work on. I tried the script in the body of your message with a few shells.
The following tests are all on FreeBSD 14.0.

1. /bin/sh
-----------
1 - OK
2 - OK
3 - FAIL (22 != 10)
4 - OK

2. dash
-----------
1 - OK
2 - OK
3 - OK
4 - OK

3. bash, mksh, ksh93, oksh
-----------
1 - OK
2 - FAIL (15 != 3)
3 - FAIL (22 != 10)
4 - FAIL (19 != 3)

What do we think the proper solution to this is?
Comment 3 white-pack 2024-02-12 14:28:21 UTC
The interest on having this working on sh is because it's the default root shell in FreeBSD 14.0-RELEASE onwards. And according to its man page and to my best understanding of it the thing should work (on the original post I've attached an ad-hoc "work-around/idea" which could[?] possibly inspire a fix).

I know nothing about dash and the other shells. As commented above it seems that dash does not have the issue; great. For the other shells I would suspect they're are all even more flawed WRT $LINENO.