Commit c4d5f9e2
c4d5f9e267d2Merged
ydah <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(runtime): raise on File.write directory paths
Details
- Commit
- c4d5f9e267d2
- Parent
- 8efdfe8779ba
lib/sp_runtime.hmodifiedView file
@@ -1744,7 +1744,7 @@
17441744
null-termination because text mode shrinks the byte count below17451745
ftell's raw-file size. */17461746
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; }1747-
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); } }1747+
static void sp_file_write(const char *path, const char *data) { if (sp_file_directory(path)) sp_raise_cls("Errno::EISDIR", sp_sprintf("Is a directory @ rb_sysopen - %s", path)); 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); }17501750
static const char *sp_backtick(const char *cmd) { FILE *p = popen(cmd, "r"); if (!p) return sp_str_empty; char *buf = sp_str_alloc_raw(4096); size_t n = fread(buf, 1, 4095, p); buf[n] = 0; pclose(p); return buf; }test/file_write_directory_error.rbaddedView file
Loading diff…
test/file_write_directory_error.rb.expectedaddedView file
Loading diff…