mruby-dir/dir.c (mrb_di_getwd): reduce buffer allocation size

Instead of allocating `MAXPATHLEN` buffer string at the beginning,
allocate smaller (64 bytes) buffer, then resize it if needed. The
allocating max size is safe but consumes more memory.
This commit is contained in:
Yukihiro "Matz" Matsumoto
2023-08-02 19:23:48 +09:00
parent a2649652f1
commit 5de3be1bee
+10 -3
View File
@@ -34,6 +34,7 @@
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#define E_IO_ERROR mrb_exc_get_id(mrb, MRB_SYM(IOError))
@@ -128,10 +129,16 @@ static mrb_value
mrb_dir_getwd(mrb_state *mrb, mrb_value klass)
{
mrb_value path;
mrb_int size = 64;
path = mrb_str_buf_new(mrb, MAXPATHLEN);
if (getcwd(RSTRING_PTR(path), MAXPATHLEN) == NULL) {
mrb_sys_fail(mrb, "getcwd(2)");
path = mrb_str_buf_new(mrb, size);
while (getcwd(RSTRING_PTR(path), size) == NULL) {
int e = errno;
if (e != ERANGE) {
mrb_sys_fail(mrb, "getcwd(2)");
}
size *= 2;
mrb_str_resize(mrb, path, size);
}
mrb_str_resize(mrb, path, strlen(RSTRING_PTR(path)));
return path;