Skip to content

Commit

Permalink
fixed: possible memory corruption
Browse files Browse the repository at this point in the history
  • Loading branch information
eranif committed May 27, 2023
1 parent a6eab7b commit e310f24
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void get_file_contents(const char* filename, std::string* content)
fseek(file, 0, SEEK_SET);
content->resize(length);
// Set the contents of the string.
size_t bytes = fread(content->data(), sizeof(char), length, file);
size_t bytes = fread((void*)content->data(), sizeof(char), length, file);
(void)bytes;
// Close the file.
fclose(file);
Expand Down
5 changes: 3 additions & 2 deletions tinyjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <string_view>
#include <utility>

namespace tinyjson
Expand Down Expand Up @@ -75,8 +76,8 @@ std::string& escape_string(const std::string_view& str, std::string* escaped)
*ptr2++ = 't';
break;
default:
snprintf(ptr2, 5, "u%04x", token);
ptr2 += 5;
snprintf(ptr2, 6, "u%04x", token);
ptr2 += 6;
break; /* escape and print */
}
}
Expand Down
2 changes: 1 addition & 1 deletion tinyjson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ struct Element {
}

/// return the value as a number. return the `default_value` on error
template <typename T> FLATTEN_INLINE T&& to_str(const char* default_value = "") const
template <typename T> FLATTEN_INLINE T to_str(const char* default_value = "") const
{
T value;
as_str(&value, default_value);
Expand Down
7 changes: 7 additions & 0 deletions tinyjson.workspace
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
"name": "tinyjson",
"configs": [{
"name": "Debug",
"targets": [["build", "mkdir -p build-debug && cd build-debug && make -j$(nproc)"], ["clean", "mkdir -p build-debug && cd build-debug && make -j$(nproc) clean"], ["cmake", "mkdir -p build-debug && cd build-debug && cmake -DCMAKE_BUILD_TYPE=Debug .."]],
"file_extensions": "*.cpp;*.c;*.txt;*.json;*.hpp;*.cc;*.cxx;*.xml;*.h;*.wxcp;*.py;*.php;*.rb;*.html;*.js;*.ts;*.rs;*.iss;*.md;*.bat;*.sh;*.cmake",
"excludeFilesPattern": "*.o;*.pyc;*.obj;*.workspace;*.o.d;*.exe;*.dll;*.project",
"excludePaths": "",
"debugger": "GNU gdb debugger"
}, {
"name": "Debug_Windows",
"targets": [["build", "cd build-debug && mingw32-make -j24"], ["clean", "cd build-debug && mingw32-make clean"], ["cmake", "mkdir -p build-debug && cd build-debug && cmake -DCMAKE_BUILD_TYPE=Debug .. -G\"MinGW Makefiles\""]],
"file_extensions": "*.cpp;*.c;*.txt;*.json;*.hpp;*.cc;*.cxx;*.xml;*.h;*.wxcp;*.py;*.php;*.rb;*.html;*.js;*.ts;*.rs;*.iss;*.md;*.bat;*.sh;*.cmake",
"excludeFilesPattern": "*.o;*.pyc;*.obj;*.workspace;*.o.d;*.exe;*.dll;*.project",
Expand Down

0 comments on commit e310f24

Please sign in to comment.