Skip to content

Commit

Permalink
Fix: Time-of-check time-of-use filesystem race condition (#4473)
Browse files Browse the repository at this point in the history
* Fix code scanning alert no. 6: Time-of-check time-of-use filesystem race condition

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

* Update CHANGELOG.md

* Apply suggestions from code review

* Format code

---------

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Co-authored-by: Sentry Github Bot <[email protected]>
  • Loading branch information
3 people authored Oct 30, 2024
1 parent 0c44375 commit a90f228
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Add option to report uncaught NSExceptions on macOS (#4471)
- Build visionOS project with static Sentry SDK (#4462)
- Time-of-check time-of-use filesystem race condition (#4473)
- Capture all touches with session replay (#4477)

### Improvements
Expand Down
12 changes: 6 additions & 6 deletions Sources/SentryCrash/Recording/Tools/SentryCrashFileUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,18 @@ sentrycrashfu_readEntireFile(const char *const path, char **data, int *length, i
int fd = -1;
int bytesToRead = maxLength;

struct stat st;
if (stat(path, &st) < 0) {
SENTRY_ASYNC_SAFE_LOG_ERROR("Could not stat %s: %s", path, strerror(errno));
goto done;
}

fd = open(path, O_RDONLY);
if (fd < 0) {
SENTRY_ASYNC_SAFE_LOG_ERROR("Could not open %s: %s", path, strerror(errno));
goto done;
}

struct stat st;
if (fstat(fd, &st) < 0) {
SENTRY_ASYNC_SAFE_LOG_ERROR("Could not fstat %s: %s", path, strerror(errno));
goto done;
}

if (bytesToRead == 0 || bytesToRead >= (int)st.st_size) {
bytesToRead = (int)st.st_size;
} else if (bytesToRead > 0) {
Expand Down

0 comments on commit a90f228

Please sign in to comment.