Commit 754cf764
754cf764b56cMerged
ydah <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(runtime): raise on File.read directory paths
Details
- Commit
- 754cf764b56c
- Parent
- b1604e893600
lib/sp_runtime.hmodifiedView file
@@ -1743,7 +1743,7 @@
17431743
and `\r\n` becomes `\r\r\n`. fread's actual byte count drives17441744
null-termination because text mode shrinks the byte count below17451745
ftell's raw-file size. */1746-
static const char *sp_file_read(const char *path) { FILE *f = fopen(path, "r"); if (!f) return &("\xff" "")[1]; fseek(f, 0, SEEK_END); long sz = ftell(f); fseek(f, 0, SEEK_SET); char *buf = sp_str_alloc(sz); size_t n = 0; if (sz > 0) { n = fread(buf, 1, sz, f); } buf[n] = 0; fclose(f); return buf; }1746+
static const char *sp_file_read(const char *path) { if (sp_file_directory(path)) sp_raise_cls("Errno::EISDIR", sp_sprintf("Is a directory @ io_fread - %s", path)); FILE *f = fopen(path, "r"); if (!f) return &("\xff" "")[1]; fseek(f, 0, SEEK_END); long sz = ftell(f); fseek(f, 0, SEEK_SET); char *buf = sp_str_alloc(sz); size_t n = 0; if (sz > 0) { n = fread(buf, 1, sz, f); } buf[n] = 0; fclose(f); return buf; }17471747
static void sp_file_write(const char *path, const char *data) { FILE *f = fopen(path, "wb"); if (f) { fwrite(data, 1, sp_str_byte_len(data), f); fclose(f); } }17481748
static mrb_bool sp_file_exist(const char *path) { FILE *f = fopen(path, "r"); if (f) { fclose(f); return TRUE; } return FALSE; }17491749
static void sp_file_delete(const char *path) { remove(path); }test/file_read_directory_error.rbaddedView file
Loading diff…
test/file_read_directory_error.rb.expectedaddedView file
Loading diff…