←back to thread

79 points Deeg9rie9usi | 1 comments | | HN request time: 0.246s | source
Show context
chubot ◴[] No.44381542[source]
Wow great bug!

> Bash forgot to reset errno before the call. For about 30 years, no one noticed

I have to say, this part of the POSIX API is maddening!

99% of the time, you don't need to set errno = 0 before making a call. You check for a non-zero return, and only then look at errno.

But SOMETIMES you need to set errno = 0, because in this case readdir() returns NULL on both error and EOF.

I actually didn't realize this before working on https://oils.pub/

---

And it should go without saying: Oils simply uses libc - we don't need to support system with a broken getcwd()!

Although a funny thing is that I just fixed a bug related to $PWD that AT&T ksh (the original shell, that bash is based on) hasn't fixed for 30+ years too!

(and I didn't realize it was still maintained)

https://www.illumos.org/issues/17442

https://github.com/oils-for-unix/oils/issues/2058

There is a subtle issue with respect to:

1) "trusting" the $PWD value you inherit from another process

2) Respecting symlinks - this is the reason the shell can't just call getcwd() !

    if (*p != '/' || stat(p, &st1) || stat(".", &st2) ||
        st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
        p = 0;
Basically, the shell considers BOTH the inherited $PWD and the value of getcwd() to determine its $PWD. It can't just use one or the other!
replies(2): >>44385123 #>>44387855 #
JdeBP ◴[] No.44385123[source]
The Bourne Again shell is not based upon the Korn shell. There's a very big clue in its name which prior shell the Bourne Again shell started out to clone. (-:
replies(1): >>44387041 #
1. chubot ◴[] No.44387041[source]
I'm saying the behavior of bash is largely based on ksh. See

https://pages.oils.pub/spec-compat/2025-06-19/renamed-tmp/sp...

(which I created)

Even the upcoming bash 5.4 implement ksh command sub ${ echo hi; }, which is more evidence that bash is based on ksh.

They're still implementing ksh 35 years later ...