Skip to content

crypto/rand: return correct count on hardware RNG read error#5265

Open
cuiweixie wants to merge 1 commit intotinygo-org:devfrom
cuiweixie:crypto/rand-fix-baremetal-rng-error-return
Open

crypto/rand: return correct count on hardware RNG read error#5265
cuiweixie wants to merge 1 commit intotinygo-org:devfrom
cuiweixie:crypto/rand-fix-baremetal-rng-error-return

Conversation

@cuiweixie
Copy link
Copy Markdown

Summary

Fix the baremetal crypto/rand reader so that when machine.GetRNG() fails after some bytes were already written, Read returns (i, err) instead of (n, err). Previously n was still zero, so callers could not tell how many bytes were valid.

Testing

Not run (small one-line semantic fix; hardware RNG path).

When machine.GetRNG fails mid-read, return the number of bytes already
written (i) instead of n, which is still zero at that point.
@deadprogram deadprogram changed the base branch from release to dev March 28, 2026 20:55
@deadprogram
Copy link
Copy Markdown
Member

Hello @cuiweixie thanks for the PR. I changed the branch to dev as described here https://tinygo.org/docs/guides/contributing/#how-to-use-our-github-repository

As far as the content, is the current behavior of returning 0 the right choice for when an error in RNG has occurred? Can we depend on the values that were returned so far when an error condition has occurred? I would guess yes.

Could you please describe the situation that resulted in you needing this change?

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