Skip to content

Commit b082d67

Browse files
nobumatzbot
authored andcommitted
[ruby/prism] Fix dangling pointers on Windows as well
Share the empty source string in `pm_string_mapped_init` and `pm_string_file_init`. ruby/prism@f7a9a03a92
1 parent a54351f commit b082d67

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

prism/util/pm_string.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "prism/util/pm_string.h"
22

3+
static const uint8_t empty_source[] = "";
4+
35
/**
46
* Returns the size of the pm_string_t struct. This is necessary to allocate the
57
* correct amount of memory in the FFI backend.
@@ -133,8 +135,7 @@ pm_string_mapped_init(pm_string_t *string, const char *filepath) {
133135
// the source to a constant empty string and return.
134136
if (file_size == 0) {
135137
pm_string_file_handle_close(&handle);
136-
const uint8_t source[] = "";
137-
*string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
138+
*string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = empty_source, .length = 0 };
138139
return PM_STRING_INIT_SUCCESS;
139140
}
140141

@@ -182,8 +183,7 @@ pm_string_mapped_init(pm_string_t *string, const char *filepath) {
182183

183184
if (size == 0) {
184185
close(fd);
185-
static const uint8_t source[] = "";
186-
*string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
186+
*string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = empty_source, .length = 0 };
187187
return PM_STRING_INIT_SUCCESS;
188188
}
189189

@@ -225,8 +225,7 @@ pm_string_file_init(pm_string_t *string, const char *filepath) {
225225
// the source to a constant empty string and return.
226226
if (file_size == 0) {
227227
pm_string_file_handle_close(&handle);
228-
const uint8_t source[] = "";
229-
*string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
228+
*string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = empty_source, .length = 0 };
230229
return PM_STRING_INIT_SUCCESS;
231230
}
232231

@@ -278,8 +277,7 @@ pm_string_file_init(pm_string_t *string, const char *filepath) {
278277
size_t size = (size_t) sb.st_size;
279278
if (size == 0) {
280279
close(fd);
281-
static const uint8_t source[] = "";
282-
*string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
280+
*string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = empty_source, .length = 0 };
283281
return PM_STRING_INIT_SUCCESS;
284282
}
285283

0 commit comments

Comments
 (0)