Skip to content

Commit d4afad4

Browse files
committed
Protect path refs because they are in C heap
Fixes #421
1 parent f3323ad commit d4afad4

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

planck-c/functions.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,9 @@ JSValueRef function_list_files(JSContextRef ctx, JSObjectRef function, JSObjectR
778778
size_t buf_len = path_len + strlen(dir->d_name) + 2;
779779
char *buf = malloc(buf_len);
780780
snprintf(buf, buf_len, "%s/%s", path, dir->d_name);
781-
paths[count++] = c_string_to_value(ctx, buf);
781+
JSValueRef path_ref = c_string_to_value(ctx, buf);
782+
paths[count++] = path_ref;
783+
JSValueProtect(ctx, path_ref);
782784
free(buf);
783785

784786
if (count == capacity) {
@@ -793,6 +795,11 @@ JSValueRef function_list_files(JSContextRef ctx, JSObjectRef function, JSObjectR
793795

794796
JSValueRef rv = JSObjectMakeArray(ctx, count, paths, NULL);
795797

798+
size_t i = 0;
799+
for (i=0; i<count; ++i) {
800+
JSValueUnprotect(ctx, paths[i]);
801+
}
802+
796803
free(path);
797804
free(paths);
798805

0 commit comments

Comments
 (0)