aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBojan Novković <bnovkov@FreeBSD.org>2025-07-24 15:07:34 +0000
committerBojan Novković <bnovkov@FreeBSD.org>2025-07-25 09:22:44 +0000
commit108e2d1137aabc65db3cff545615e9e8927dbe23 (patch)
tree8bf5c167c15fdcad2f0bed0932a077fc3a3257ea
parentfdefa79abef25dac6c058059c8dc2b85cef1c8f7 (diff)
db/hash.c: Do not return an error when opening a missing database with O_CREAT
dbm_open currently returns an error when opening a missing database with O_CREAT but creates the request database file. This is caused by a buggy check in __hash_open which attempts to detect a new (or empty) database file, but fails to take the O_CREAT flag into account. Fix this by expanding the check to include O_CREAT. Fixes: edcdc752ecdd Sponsored by: Klara, Inc. Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D51491
-rw-r--r--lib/libc/db/hash/hash.c3
1 files changed, 2 insertions, 1 deletions
diff --git a/lib/libc/db/hash/hash.c b/lib/libc/db/hash/hash.c
index 7a66f5443d94..cc96fb5ce326 100644
--- a/lib/libc/db/hash/hash.c
+++ b/lib/libc/db/hash/hash.c
@@ -120,7 +120,8 @@ __hash_open(const char *file, int flags, int mode,
if ((hashp->fp = _open(file, flags | O_CLOEXEC, mode)) == -1)
RETURN_ERROR(errno, error0);
new_table = _fstat(hashp->fp, &statbuf) == 0 &&
- statbuf.st_size == 0 && (flags & O_ACCMODE) != O_RDONLY;
+ statbuf.st_size == 0 &&
+ ((flags & O_ACCMODE) != O_RDONLY || (flags & O_CREAT) != 0);
} else
new_table = 1;