summaryrefslogtreecommitdiff
path: root/dir.c
diff options
context:
space:
mode:
Diffstat (limited to 'dir.c')
-rw-r--r--dir.c24
1 files changed, 18 insertions, 6 deletions
diff --git a/dir.c b/dir.c
index 9a6d977dca..89f7bb89cb 100644
--- a/dir.c
+++ b/dir.c
@@ -1489,21 +1489,33 @@ replace_real_basename(char *path, long base, rb_encoding *enc, int norm_p)
if (h == INVALID_HANDLE_VALUE) return path;
FindClose(h);
if (tmp) {
+ char *buf;
tmp = rb_w32_conv_from_wchar(fd.cFileName, enc);
wlen = RSTRING_LEN(tmp);
- path = GLOB_REALLOC(path, base + wlen + 1);
- memcpy(path + base, RSTRING_PTR(tmp), wlen);
+ buf = GLOB_REALLOC(path, base + wlen + 1);
+ if (buf) {
+ path = buf;
+ memcpy(path + base, RSTRING_PTR(tmp), wlen);
+ path[base + wlen] = 0;
+ }
rb_str_resize(tmp, 0);
}
else {
char *utf8filename;
wlen = WideCharToMultiByte(CP_UTF8, 0, fd.cFileName, -1, NULL, 0, NULL, NULL);
utf8filename = GLOB_REALLOC(0, wlen);
- WideCharToMultiByte(CP_UTF8, 0, fd.cFileName, -1, utf8filename, wlen, NULL, NULL);
- memcpy(path + base, utf8filename, wlen);
- GLOB_FREE(utf8filename);
+ if (utf8filename) {
+ char *buf;
+ WideCharToMultiByte(CP_UTF8, 0, fd.cFileName, -1, utf8filename, wlen, NULL, NULL);
+ buf = GLOB_REALLOC(path, base + wlen + 1);
+ if (buf) {
+ path = buf;
+ memcpy(path + base, utf8filename, wlen);
+ path[base + wlen] = 0;
+ }
+ GLOB_FREE(utf8filename);
+ }
}
- path[base + wlen] = 0;
return path;
}
#elif USE_NAME_ON_FS == 1