Skip to content

Fix: memory output drainage and add regression test for #2469 - #2472

Merged
ronaldtse merged 4 commits into
rnpgp:mainfrom
MakerYuichi:fix-empty-memory-buf-2469
Aug 28, 2026
Merged

Fix: memory output drainage and add regression test for #2469#2472
ronaldtse merged 4 commits into
rnpgp:mainfrom
MakerYuichi:fix-empty-memory-buf-2469

Conversation

@MakerYuichi

Copy link
Copy Markdown
Contributor

Description

Fixes an issue where rnp_output_memory_get_buf() unexpectedly fails with RNP_ERROR_BAD_PARAMETERS when draining a legitimately empty memory output (such as a zero-length literal packet or an operation that produced no data).

A memory destination that hasn't written any bytes does not allocate an internal buffer, causing mem_dest_get_memory() to return NULL. This PR introduces a check to safely return RNP_SUCCESS with *len = 0 and *buf = NULL if *len == 0 on a valid PGP_STREAM_MEMORY type stream, allowing callers to uniformly drain empty memory outputs.

Fixes #2469

PR Checklist

  • Branch is rebased on the latest main.
  • clang-format clean (Note: clang-format wasn't available locally, relying on CI check).
  • All tests pass on at least one backend (Verified 288/288 passing via Botan/macOS local build).
  • Added a dedicated regression test that fails without this change (src/tests/issues/2469.cpp).
  • Description explains the why and links the relevant issue.
  • No observable API changes to public headers in include/rnp/rnp.h.

@ronaldtse

Copy link
Copy Markdown
Contributor

Thank you @MakerYuichi ! We will be looking at this next.

Cover do_copy=true on an empty output (success without allocating) and
the non-memory-output rejection, drop unused includes, add the missing
EOF newline, remove trailing whitespace and stray blank-line hunks.
@ronaldtse

Copy link
Copy Markdown
Contributor

Validated end to end against a main-identical tree:

  • Bug confirmed: rnp_output_to_memory never preallocates (init_mem_dest(dst, NULL, …)param->memory = NULL), the first allocation happens in mem_dst_write, so draining a valid-but-empty memory output hit if (!*buf) return RNP_ERROR_BAD_PARAMETERS. The regression test fails on main exactly as reported in rnp_output_memory_get_buf() fails for a memory output with zero bytes written #2469 (verified locally: 0x10000002), passes with the fix.
  • Fix is correct: the !*buf && *len guard keeps the defensive error for the impossible NULL-with-bytes case, the explicit PGP_STREAM_MEMORY check makes the wrong-type rejection intentional instead of incidental, and do_copy && *len avoids a pointless malloc(0).
  • No overlap: no open PR modifies this function (checked all of them; dump: walk multiple armored messages in --list-packets (#2036) #2429/ffi: entropy encoding + backup archive building blocks #2435 only call it).
  • Pushed a small follow-up commit (via maintainer edits): extended the test with the do_copy=true-empty and non-memory-output-rejection cases, and cleaned up style (EOF newline, trailing whitespace, unused includes, stray blank-line hunks in ffi.cpp).

This unblocks uniform empty-output draining, which e.g. rnp_backup_archive_load's plaintext drain (#2435) also relies on.

@ronaldtse ronaldtse left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @MakerYuichi — clear bug report shape (issue + minimal fix + regression test), exactly how we like them.

Validated end to end against a main-identical tree: the regression test fails on current main with RNP_ERROR_BAD_PARAMETERS exactly as #2469 describes (lazy allocation means an empty memory output has no buffer), and passes with this fix. All three changed paths check out: the !*buf && *len guard keeps the defensive error for the impossible NULL-with-bytes case, the explicit PGP_STREAM_MEMORY check makes wrong-type rejection intentional, and do_copy && *len avoids a pointless malloc(0). No other open PR touches this function. I pushed a small follow-up commit extending the test to the do_copy=true-empty and non-memory-output cases and cleaning up style.

@ronaldtse

Copy link
Copy Markdown
Contributor

@ni4 this one needs your approval (1 of 2 — I've approved): #2472

Small, well-scoped fix by @MakerYuichi for #2469: rnp_output_memory_get_buf() returned BAD_PARAMETERS when draining a valid-but-empty memory output, because the buffer allocates lazily on first write. Now returns SUCCESS with (NULL, 0). Verified against main locally (regression test fails pre-fix, passes after), and it unblocks uniform empty-output draining — e.g. the backup-archive plaintext drain in #2435. CI green.

@ronaldtse
ronaldtse requested a review from ni4 August 27, 2026 07:14

@ni4 ni4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! But let's wait till non-approved workflows are finished and green.

@codecov

codecov Bot commented Aug 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.40%. Comparing base (7853ae9) to head (e9412b8).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2472   +/-   ##
=======================================
  Coverage   85.40%   85.40%           
=======================================
  Files         126      126           
  Lines       22964    22966    +2     
=======================================
+ Hits        19612    19614    +2     
  Misses       3352     3352           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ronaldtse

Copy link
Copy Markdown
Contributor

The windows-11-arm leg fails with SHLWAPI_LIBRARY NOTFOUND — that's a pre-existing ARM64 SDK-discovery bug in main, not caused by this PR (which only touches rnp_output_memory_get_buf and test files). It's fixed by #2471 (SHLWAPI linked by name instead of the broken GetUMWindowsSDKLibraryDir probing), which also makes all four windows preview legs blocking for the first time. Once #2471 merges, this leg goes green here automatically.

@ronaldtse ronaldtse left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving — the fix is correct and validated end to end (regression test fails on main, passes with the change; no other open PR touches this function).

@ronaldtse

Copy link
Copy Markdown
Contributor

All workflows here should pass (except fuzzing jobs due to google/oss-fuzz#15882 ). Will merge when it is done.

@ronaldtse
ronaldtse merged commit 68a06db into rnpgp:main Aug 28, 2026
139 of 141 checks passed
@ronaldtse

Copy link
Copy Markdown
Contributor

Thank you @MakerYuichi for contributing the fix!

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.

rnp_output_memory_get_buf() fails for a memory output with zero bytes written

3 participants