Skip to content

Allow io_memory_resize to truncate to 0 bytes ##io#25706

Open
trufae wants to merge 3 commits intomasterfrom
claude/implement-todo-item-KBuTr
Open

Allow io_memory_resize to truncate to 0 bytes ##io#25706
trufae wants to merge 3 commits intomasterfrom
claude/implement-todo-item-KBuTr

Conversation

@trufae
Copy link
Copy Markdown
Collaborator

@trufae trufae commented Apr 2, 2026

The TODO asked why truncation to 0 bytes was not allowed. There was no
fundamental reason - we just need to free the buffer and reset size/offset
instead of calling realloc(ptr, 0) which has implementation-defined behavior.

Also add a NULL guard for the RIOMalloc pointer and protect the read path
from calling memcpy with a NULL source when the buffer is empty.

https://claude.ai/code/session_01H9JNeHBKZngDR8pf47GeH4

claude added 3 commits April 2, 2026 03:49
The TODO asked why truncation to 0 bytes was not allowed. There was no
fundamental reason - we just need to free the buffer and reset size/offset
instead of calling realloc(ptr, 0) which has implementation-defined behavior.

Also add a NULL guard for the RIOMalloc pointer and protect the read path
from calling memcpy with a NULL source when the buffer is empty.

https://claude.ai/code/session_01H9JNeHBKZngDR8pf47GeH4
Follow-up to the io_memory_resize fix: audit and fix all IO plugins
for safe behavior when buffer size is zero.

- io_gzip: allow resize to 0 (free buf, reset size/offset) for
  consistency with io_memory_resize, and guard __read memcpy when
  count adjusts to 0 to avoid memcpy with NULL source
- io_null: fix unsigned underflow in __lseek where size-1 wraps to
  UT64_MAX when size is 0 (the __resize handler already supported
  count=0 but __lseek did not)
- io_xattr: guard __close to skip write_xattr when buf is NULL
  after a resize to 0, avoiding passing NULL to setxattr

https://claude.ai/code/session_01H9JNeHBKZngDR8pf47GeH4
Fix io_malloc __resize to guard against calling r_hex_bin2strdup with
NULL buf/zero size after resize to 0 on hex:// URIs. The assertion in
r_hex_bin2strdup would crash the process. Now the URI is set to
"hex://" for empty buffers.

Add test/db/cmd/cmd_or with 8 tests covering:
- resize malloc:// to zero (reads return 0xff)
- resize to zero and grow back (buffer is zeroed)
- write after resize-to-zero and grow back
- shrink preserves partial data
- shrink + grow preserves partial data, new area zeroed
- map removal after resize to zero
- seek behavior after resize to zero
- hex:// resize to zero and back

https://claude.ai/code/session_01H9JNeHBKZngDR8pf47GeH4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants