]> git.hungrycats.org Git - bees/commitdiff
fd: delete Fd::operator bool so "if (fd)" fails to compile
authorZygo Blaxell <bees@furryterror.org>
Sat, 10 Oct 2026 18:50:13 +0000 (14:50 -0400)
committerZygo Blaxell <bees@furryterror.org>
Sat, 10 Oct 2026 18:55:23 +0000 (14:55 -0400)
Fd has operator int() and operator!(), but no operator bool.  In a
boolean context such as "if (fd)", "fd && x" or "fd ? a : b", the
compiler converts through operator int, and an empty Fd is -1, which
is true.  "!fd" works, which makes the positive form look safe when it
is not.  Several such tests have crept in.

Declare an explicit, deleted operator bool.  An explicit conversion is
only considered in boolean contexts, where it is an exact match and
wins over operator int, so those uses now fail to compile.  Conversions
to int are unaffected.  Test an Fd with "!!fd" or "!fd".

Assisted-by: Claude-Code:claude-opus-5-5
include/crucible/fd.h

index 6c3e22a4f5352019b784323b6ec561dcbae1f914..358beb78481ba288daf5e69bd26137d284ce1e52 100644 (file)
@@ -62,6 +62,9 @@ namespace crucible {
                Fd &operator=(const shared_ptr<IOHandle> &);
                operator int() const;
                bool operator!() const;
+               /// Without this, `if (fd)` converts through operator int, and
+               /// an empty Fd (-1) is true.  Test with `!!fd` or `!fd` instead.
+               explicit operator bool() const = delete;
                shared_ptr<IOHandle> operator->() const;
                string name() const;
                string leaf_name() const;