Skip to content

fix tmpfile#424

Merged
andiwand merged 6 commits intomainfrom
fix-tmpfile
Aug 6, 2025
Merged

fix tmpfile#424
andiwand merged 6 commits intomainfrom
fix-tmpfile

Conversation

@andiwand
Copy link
Copy Markdown
Member

@andiwand andiwand commented Aug 4, 2025

No description provided.

@claude
Copy link
Copy Markdown

claude Bot commented Aug 4, 2025

Claude finished @andiwand's task —— View job


Code Review for PR #424: fix tmpfile

I've completed my review of this pull request. Here's my comprehensive feedback:

Todo List:

  • Review tmpfile hack implementation
  • Analyze header file renaming changes
  • Check CMake integration modifications
  • Review Java-side changes
  • Assess security and performance implications
  • Check test coverage

Summary

This PR implements a workaround for the tmpfile() function on Android, which typically fails due to restricted access to the default temporary directory. The solution involves creating a custom tmpfile() implementation that allows configuring the temporary directory location.

Code Quality and Best Practices

✅ Strengths

  1. Clean Architecture: The tmpfile hack is well-isolated in its own module (tmpfile_hack.cpp/hpp)
  2. Proper Error Handling: Good use of Android logging for both success and failure cases
  3. Thread Safety Consideration: Using static variables with proper initialization
  4. Naming Convention Consistency: Header file renamed from CoreWrapper.hpp to core_wrapper.hpp to match C++ naming conventions

⚠️ Areas for Improvement

  1. Global State Management: The s_tmpfile_directory static variable could benefit from thread-safe access patterns if set_tmpfile_directory might be called from multiple threads
  2. Missing Documentation: No comments explaining why this hack is necessary or how it works

Potential Bugs or Issues

🐛 Critical Issues

  1. Symbol Collision Risk: The extern "C" FILE *tmpfile() function (tmpfile_hack.cpp:35) overrides the standard library function. This could cause issues if:

    • Other libraries in the app also try to override tmpfile()
    • The linker doesn't properly resolve the symbol precedence
    • Future Android versions change how tmpfile() is implemented
  2. File Descriptor Leak: In the error path at tmpfile_hack.cpp:51-55, if fdopen() fails, the file descriptor is closed but the temporary file created by mkstemp() is not removed from the filesystem

🔍 Minor Issues

  1. Inconsistent Logging Tags: Uses "tmpfile_hack" for logging while core_wrapper.cpp uses "smn"
  2. No Validation: set_tmpfile_directory doesn't validate if the provided directory exists or is writable

Performance Considerations

✅ Good Performance Practices

  1. Efficient String Handling: Uses std::string_view for constants
  2. Lazy Initialization: Directory resolution only happens when needed
  3. Proper Resource Cleanup: unlink() is called immediately after file creation (tmpfile_hack.cpp:49)

⚠️ Performance Concerns

  1. String Concatenation: Each tmpfile() call creates new string objects for path construction. Consider using a thread-local buffer for better performance in high-frequency scenarios

Security Concerns

🔒 Security Issues

  1. Predictable Filenames: While mkstemp() provides some randomness with "XXXXXX", the pattern is still somewhat predictable. Consider:

    • Using a more complex template pattern
    • Adding process/thread ID to the filename
  2. Directory Permissions: No validation that the configured directory has appropriate permissions. Temporary files might be created in world-readable locations

  3. Path Traversal: set_tmpfile_directory doesn't validate the input path, potentially allowing path traversal if the directory comes from untrusted sources

Test Coverage

❌ Missing Tests

No tests were added for this functionality. Recommended test cases:

  1. Basic tmpfile creation and cleanup
  2. Concurrent tmpfile creation
  3. Error handling when directory doesn't exist
  4. Error handling when directory isn't writable
  5. Behavior when TMPDIR environment variable is set
  6. Integration tests with the ODR core library

Additional Recommendations

  1. Add a Compile Flag: Consider adding a CMake option to disable this hack on platforms where it's not needed
  2. Document the Android Issue: Add comments linking to Android documentation or issue trackers explaining why this is necessary
  3. Consider Alternative Solutions:
    • Using std::filesystem::temp_directory_path() with C++17
    • Creating files directly in the app's cache directory without overriding tmpfile()

Conclusion

While this PR successfully addresses the tmpfile issue on Android, there are several security and robustness concerns that should be addressed. The approach of overriding a standard library function is risky and could lead to maintenance issues. Consider implementing a wrapper function with a different name that the ODR core library can use instead of relying on symbol interposition.

@andiwand andiwand merged commit 62e5ff8 into main Aug 6, 2025
2 checks passed
@andiwand andiwand deleted the fix-tmpfile branch August 6, 2025 05:37
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.

1 participant