←back to thread

79 points Deeg9rie9usi | 1 comments | | HN request time: 0.412s | 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 #
1. alganet ◴[] No.44387855[source]
> and I didn't realize it was still maintained

ksh _was_ unmaintained for ages. It stopped effectively in 2012, with some very small attempts at reviving it in 2016, 2018 and 2020.

Then it was picked up for active development in 2021, and it lives here now:

https://github.com/ksh93/ksh

If you didn't already, you should open an issue there with your findings.

--

Shameless plug: I keep some docker images with all those versions for testing, and many other shells too both historical and active (including osh!)

https://github.com/alganet/shell-versions/blob/main/.github/...