Skip to content

Commit

Permalink
thcrap: Fix memory problems
Browse files Browse the repository at this point in the history
  • Loading branch information
zero318 committed Jun 28, 2024
1 parent 9292f3c commit b028919
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
8 changes: 4 additions & 4 deletions thcrap/src/build_str.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ static inline constexpr bool is_compatible_char_type_v = is_compatible_char_type
// Special template to check if two string views are compatible
// since otherwise detecting this is a pain.

template<typename T, typename V, typename C = void*, bool = is_compatible_char_type_v<T, C>>
template<typename T, typename V, typename = void>
struct is_compatible_string_view : std::false_type {};

template<typename T, typename C>
struct is_compatible_string_view<T, std::basic_string_view<C>, typename std::basic_string_view<C>::value_type, true> : std::true_type {};
template<typename T, typename V>
struct is_compatible_string_view<T, V, std::void_t<typename V::value_type>> : std::conditional_t<is_compatible_char_type_v<T, typename V::value_type> && std::is_same_v<V, std::basic_string_view<typename V::value_type>>, std::true_type, std::false_type> {};

template<typename T, typename V>
static inline constexpr bool is_compatible_string_view_v = is_compatible_string_view<T, V>::value;
Expand Down Expand Up @@ -430,7 +430,7 @@ struct AllocStrHelper<T, null_terminate, std::tuple<StrsT...>> {
// avoid a dangling reference when returning.
return build_vla_data_impl3(
std::forward<std::conditional_t<
is_compatible_string_view_v<T, ArgsT>,
is_compatible_string_view_v<T, std::remove_reference_t<ArgsT>>,
std::remove_reference_t<ArgsT>,
std::reference_wrapper<std::remove_reference_t<ArgsT>>
>>(strs)...
Expand Down
13 changes: 7 additions & 6 deletions thcrap/src/jansson_ex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,16 @@ json_t* json_load_file_report_size(const char *json_fn, size_t* size_out)
int msgbox_ret;

do {
msgbox_ret = 0;

size_t json_size;
uint8_t* json_buffer = (uint8_t*)file_read(json_fn, &json_size);
uint8_t* heap_buffer = (uint8_t*)file_read(json_fn, &json_size);

if unexpected(!json_buffer || !json_size) {
if unexpected(!heap_buffer || !json_size) {
break;
}

uint8_t* json_buffer = heap_buffer;

if (!skip_bom(json_buffer, json_size, utf8_bom)) {
// Convert UTF-16LE to UTF-8.
// NULL bytes do not count as significant whitespace in JSON, so
Expand All @@ -428,15 +429,15 @@ json_t* json_load_file_report_size(const char *json_fn, size_t* size_out)
CP_UTF8, 0, (const wchar_t*)json_buffer, json_size / 2,
(char*)converted_buffer, converted_len, NULL, NULL
);
free(json_buffer);
json_buffer = converted_buffer;
free(heap_buffer);
heap_buffer = json_buffer = converted_buffer;
}
}

char* error;
json_t* ret = json5_loadb(json_buffer, json_size, &error);

free(json_buffer);
free(heap_buffer);

if (ret) {
*size_out = json_size;
Expand Down
2 changes: 1 addition & 1 deletion thcrap_loader/src/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ json_t *load_config_from_file(const char *rel_start, json_t *run_cfg, const char
run_cfg_fn = alloc_str(rel_start, config_path);
}
else {
run_cfg_fn = alloc_str<char>((std::filesystem::current_path() / "config" / config_path).u8string());
run_cfg_fn = alloc_str<char>(std::filesystem::current_path().u8string(), "\\config\\", config_path);
}
}
else {
Expand Down

0 comments on commit b028919

Please sign in to comment.