about summary refs log tree commit diff stats
path: root/src/local/container.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-09-23 11:50:36 +0200
committerbptato <nincsnevem662@gmail.com>2023-09-23 13:16:26 +0200
commitd7c2c30c924c051c36dd30290f4853e960325886 (patch)
treec7f71efdc56fac11918a229f1a6028ba694acd10 /src/local/container.nim
parent860a1e77e40469753dbef080ee6fcc24f82dc847 (diff)
downloadchawan-d7c2c30c924c051c36dd30290f4853e960325886.tar.gz
fix devnull dup in place of stdin
The previous solution was ok, but it could leak an fd...
Diffstat (limited to 'src/local/container.nim')
-rw-r--r--src/local/container.nim11
1 files changed, 7 insertions, 4 deletions
diff --git a/src/local/container.nim b/src/local/container.nim
index 2f4d0044..8b961e03 100644
--- a/src/local/container.nim
+++ b/src/local/container.nim
@@ -983,13 +983,16 @@ proc setStream*(container: Container, stream: Stream) =
     container.iface = newBufferInterface(stream)
     if container.source.t == LOAD_PIPE:
       container.iface.passFd(container.source.fd).then(proc() =
-        discard close(container.source.fd)
         if container.source.fd == 0:
-          # We have just closed stdin.
+          # We are closing stdin.
           # Leaving the stdin fileno open to grab is a bad idea.
-          # (Yes, I've just got bitten by this: dup2(0, 0). Ouch.)
           let devnull = open("/dev/null", O_RDONLY)
-          discard dup2(devnull, 0)
+          doAssert devnull != -1
+          if devnull != 0:
+            discard dup2(devnull, 0)
+            discard close(devnull)
+        else:
+          discard close(container.source.fd)
       )
       stream.flush()
     container.load()