Skip to content

Conversation

@sergei-boiko-trustwallet
Copy link
Contributor

This pull request refactors the random buffer generation logic across multiple platforms to improve error handling and consistency. The main change is that the random_buffer function now returns an integer status code (0 for success, <0 for error) instead of being void, and all call sites now check for errors and handle them appropriately. Additionally, the JNI implementation is significantly improved for safety and resource management.

Random buffer API refactor:

  • The random_buffer function signature is changed to return an int status code across all platform implementations (jni/cpp/Random.cpp, src/rand.cpp, swift/Sources/SecRandom.m, wasm/src/Random.cpp). Callers now check the return value and handle errors instead of assuming success.

Error handling improvements:

  • All usages of random_buffer now check its return value. If random data generation fails, they throw exceptions or return error codes instead of proceeding with invalid data. This affects IV and salt generation in encryption and keystore modules, and private key creation.

JNI implementation enhancements:

  • The JNI random buffer logic is rewritten to use a RAII-style JNIEnvContext class for safe environment management, proper reference handling, and exception checking. The implementation now returns error codes on failure rather than throwing or crashing.

Interface documentation and consistency:

  • The header file src/rand.h is updated to document the new return value semantics for random_buffer, ensuring consistent usage and expectations across the codebase.

Platform-specific improvements:

  • Each platform implementation (Linux, Swift, WASM, JNI) now consistently returns error codes for random generation failures, improving reliability and making error handling uniform.

These changes improve the robustness and safety of cryptographic operations by ensuring that random data generation failures are properly detected and handled.

* TODO consider refactoring WASM Random.cpp
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request refactors the random_buffer API to return error codes instead of aborting on failure, improving error handling across all platform implementations and call sites.

Changes:

  • Changed random_buffer function signature to return int (0 for success, <0 for error) across all platform implementations
  • Updated all call sites to check return values and handle errors appropriately
  • Enhanced JNI implementation with RAII-style resource management and proper exception handling

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
src/rand.h Added documentation for return value semantics (0 on success, <0 on error)
src/rand.cpp Linux implementation now returns error codes instead of throwing exceptions
swift/Sources/SecRandom.m Swift/macOS implementation returns -1 on failure instead of calling abort()
wasm/src/Random.cpp WASM implementation updated to return 0 on success
jni/cpp/Random.cpp Major refactor with JNIEnvContext class for safe resource management and error handling
src/interface/TWPrivateKey.cpp Returns nullptr on random generation failure instead of calling std::terminate()
src/Keystore/ScryptParameters.cpp Throws exception with error message if salt generation fails
src/Keystore/PBKDF2Parameters.cpp Throws exception with error message if salt generation fails
src/Keystore/AESParameters.cpp Throws exception with error message if IV generation fails
src/FIO/Encryption.cpp Throws exception with error message if IV generation fails

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +38 to +40
ssize_t readLen = read(randomData, buf, len);
close(randomData);
if (readLen != static_cast<ssize_t>(len)) {
Copy link

Copilot AI Jan 13, 2026

Choose a reason for hiding this comment

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

The comparison readLen != static_cast<ssize_t>(len) doesn't distinguish between partial reads and actual errors. A negative readLen indicates an error, while a positive value less than len indicates a short read. For /dev/urandom, short reads are possible if interrupted by a signal. Consider checking if (readLen < 0 || readLen != static_cast<ssize_t>(len)) or handling EINTR with a retry loop for robustness.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

TODO!

@github-actions
Copy link

github-actions bot commented Jan 13, 2026

Binary size comparison

➡️ aarch64-apple-ios: 15.25 MB

➡️ aarch64-apple-ios-sim: 15.25 MB

➡️ aarch64-linux-android: 20.10 MB

➡️ armv7-linux-androideabi: 17.31 MB

➡️ wasm32-unknown-emscripten: 14.85 MB

@sergei-boiko-trustwallet sergei-boiko-trustwallet requested a review from a team as a code owner January 14, 2026 14:46
@sergei-boiko-trustwallet sergei-boiko-trustwallet marked this pull request as draft January 14, 2026 18:07
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