Commit 92a4e169

92a4e169e5deMerged

Sam Ruby <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

File.size: address review — capture errno, guard narrowing, harden test

  • Capture errno into a local immediately after the failed stat() and use it for both the Errno class choice and strerror(), since strerror() / argument-evaluation-order can otherwise observe a different errno.
  • Guard the off_t -> mrb_int narrowing: on a 32-bit build (mrb_int = intptr_t) a >2GB size would silently truncate, so raise RangeError on loss, matching how spinel guards integer overflow elsewhere.
  • Test: move the initial File.write inside the begin and make the ensure delete conditional on File.exist?, so a write failure can't leave a stale temp file or mask the original error.

make test: 951 pass / 0 fail / 0 error.

Details

Commit
92a4e169e5de
Parent
dfdea4bd91a5

lib/sp_runtime.hmodifiedView file

@@ -5119,7 +5119,14 @@
51195119 }
51205120 struct stat st;
51215121 if (stat(path, &st) == -1) {
5122- sp_raise_cls(errno == ENOENT ? "Errno::ENOENT" : "RuntimeError", sp_sprintf("%s @ File.size - %s", strerror(errno), path));
5122+ int err = errno; /* capture once: strerror() may clobber errno */
5123+ sp_raise_cls(err == ENOENT ? "Errno::ENOENT" : "RuntimeError", sp_sprintf("%s @ File.size - %s", strerror(err), path));
5124+ return 0;
5125+ }
5126+ /* off_t (typically 64-bit) into mrb_int (intptr_t -> 32-bit on a 32-bit
5127+ build): guard the narrowing, as spinel does for int arithmetic. */
5128+ if ((off_t)(mrb_int)st.st_size != st.st_size) {
5129+ sp_raise_cls("RangeError", "file size out of range for Integer");
51235130 return 0;
51245131 }
51255132 return (mrb_int)st.st_size;