Selaa lähdekoodia

Fix allocs in function_cache and value_to_c_string

Lucas Stadler 9 vuotta sitten
vanhempi
commit
fda8b26104
1 muutettua tiedostoa jossa 9 lisäystä ja 10 poistoa
  1. 9 10
      c/ton/main.c

+ 9 - 10
c/ton/main.c

@ -176,23 +176,21 @@ JSValueRef function_cache(JSContextRef ctx, JSObjectRef function, JSObjectRef th
176 176
		int max_suffix_len = 20;
177 177
		int prefix_len = strlen(cache_prefix);
178 178
		char *path = malloc((prefix_len + max_suffix_len) * sizeof(char));
179
		memset(path, 0, prefix_len + max_suffix_len);
179 180
180 181
		suffix = ".js";
181
		strncpy(path, cache_prefix, prefix_len);
182
		path[prefix_len] = '\0';
183
		strncat(path, suffix, strlen(suffix));
182
		strcpy(path, cache_prefix);
183
		strcat(path, suffix);
184 184
		write_contents(path, source);
185 185
186 186
		suffix = ".cache.json";
187
		strncpy(path, cache_prefix, prefix_len);
188
		path[prefix_len] = '\0';
189
		strncat(path, suffix, strlen(suffix));
187
		strcpy(path, cache_prefix);
188
		strcat(path, suffix);
190 189
		write_contents(path, cache);
191 190
192 191
		suffix = ".js.map.json";
193
		strncpy(path, cache_prefix, prefix_len);
194
		path[prefix_len] = '\0';
195
		strncat(path, suffix, strlen(suffix));
192
		strcpy(path, cache_prefix);
193
		strcat(path, suffix);
196 194
		write_contents(path, sourcemap);
197 195
198 196
		free(cache_prefix);
@ -517,8 +515,9 @@ char *value_to_c_string(JSContextRef ctx, JSValueRef val) {
517 515
	}
518 516
519 517
	JSStringRef str_ref = JSValueToStringCopy(ctx, val, NULL);
520
	size_t len = JSStringGetLength(str_ref);
518
	size_t len = JSStringGetLength(str_ref) + 1;
521 519
	char *str = malloc(len * sizeof(char));
520
	memset(str, 0, len);
522 521
	JSStringGetUTF8CString(str_ref, str, len);
523 522
	JSStringRelease(str_ref);
524 523