Page MenuHomeFreeBSD

db/hash.c: Allow O_WRONLY in dbm_open
ClosedPublic

Authored by bnovkov on Jul 25 2025, 9:29 AM.
Tags
None
Referenced Files
Unknown Object (File)
Tue, Nov 4, 5:34 AM
Unknown Object (File)
Mon, Nov 3, 8:09 AM
Unknown Object (File)
Sat, Nov 1, 1:50 PM
Unknown Object (File)
Wed, Oct 22, 10:51 PM
Unknown Object (File)
Wed, Oct 22, 10:51 PM
Unknown Object (File)
Wed, Oct 22, 10:51 PM
Unknown Object (File)
Oct 12 2025, 11:50 PM
Unknown Object (File)
Oct 12 2025, 11:50 PM
Subscribers

Details

Summary

The dbm(3) manpage explicitly states that O_WRONLY is not allowed in
dbm_open, but a more recent comment in __hash_open suggests otherwise.
Furthermore, POSIX.1 allows O_WRONLY in dbm_open and states
that the underlying file must be opened for both reading and writing.

Fix this by correcting the O_WRONLY check and moving it further into
the function to make sure that the original flags are stored in hashp.

Sponsored by: Klara, Inc.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

markj added inline comments.
lib/libc/db/hash/hash.c
182

Should this be checking flags instead?

lib/libc/db/man/dbm.3
104

The modified code is also used by dbopen(3), which has the same comment in its man page.

bnovkov marked 2 inline comments as done.

Address @markj 's comments.

lib/libc/db/hash/hash.c
182

hm, looks like it should.
The comment next to save_file member implies so since it refers to the underlying database file:

int             save_file;      /* Indicates whether we need to flush
                                         * file at
                                         * exit */

I've changed the check to use flags.

lib/libc/db/hash/hash.c
517

It looks like this function should have an explicit check for a readable handle, just like hash_put() below checks for a writeable handle.

Add O_WRONLY check in hash_get and add corresponding tests.

bnovkov added inline comments.
lib/libc/db/hash/hash.c
517

done, thanks!

lib/libc/tests/db/dbm_open_test.c
31–41

I think you shouldn't need this? ATF tests should always be run in a sandbox dir.

lib/libc/tests/db/dbm_perm_test.c
19
29

Or just assert that the unlink() above succeeds?

This revision is now accepted and ready to land.Jul 29 2025, 7:29 PM
bnovkov added inline comments.
lib/libc/tests/db/dbm_open_test.c
31–41

ah right, will fix while pushing, thanks

This revision was automatically updated to reflect the committed changes.