Skip to content

Commit

Permalink
fix: use snprintf instead of sprintf
Browse files Browse the repository at this point in the history
  • Loading branch information
Gowa2017 committed Sep 10, 2024
1 parent 3a1a20f commit daa2e74
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
4 changes: 3 additions & 1 deletion TerraForge3D/include/Utils/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ struct Hash
};


#ifndef MAX
#define MAX(x, y) (x > y ? x : y)
#endif // !MAX


#ifdef TERR3D_WIN32
Expand Down Expand Up @@ -263,4 +265,4 @@ std::string FormatMemoryToString(uint64_t size);
#define TERR3D_KEY_RIGHT_SUPER 347
#define TERR3D_KEY_MENU 348

#define TERR3D_KEY_LAST TERR3D_KEY_MENU
#define TERR3D_KEY_LAST TERR3D_KEY_MENU
6 changes: 3 additions & 3 deletions TerraForge3D/src/Exporters/STLExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ bool STLExporter::ExportBinary(const std::string& path, Mesh* mesh, float* progr
BinaryFileWriter writer(path);
buffer[0] = buffer[1] = 0;
if (!writer.IsOpen()) return false;
memset(buffer, 0, 80); sprintf(buffer, "Generated by TerraForge3D Gen 2");
memset(buffer, 0, 80); snprintf(buffer, 4096, "Generated by TerraForge3D Gen 2");
writer.Write(buffer, 80);
uint32_t facet_count = (uint32_t)(mesh->GetFaceCount());
writer.Write(facet_count);
Expand Down Expand Up @@ -55,7 +55,7 @@ bool STLExporter::ExportASCII(const std::string& path, Mesh* mesh, float* progre
const auto& pb = mesh->GetPosition(face.b);
const auto& pc = mesh->GetPosition(face.c);
const auto normal = glm::normalize(glm::cross(glm::vec3(pc - pb), glm::vec3(pa - pb)));
sprintf(buffer,
snprintf(buffer, 4096,
" facet normal % f % f % f" "\n"
" outer loop" "\n"
" vertex %f %f %f" "\n"
Expand All @@ -76,4 +76,4 @@ bool STLExporter::ExportASCII(const std::string& path, Mesh* mesh, float* progre
writer.Write(out_str.data(), out_str.size());
*progress = 1.0f;
return true;
}
}
2 changes: 1 addition & 1 deletion TerraForge3D/src/Generators/BiomeManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ BiomeManager::BiomeManager(ApplicationState* appState)
bool success = false;
m_Data = std::make_shared<GeneratorData>();
static int s_BiomeID = 1;
sprintf(m_BiomeName, "Biome %d", s_BiomeID++);
snprintf(m_BiomeName, 64, "Biome %d", s_BiomeID++);
LoadUpResources();
// m_SelectedBaseShapeGeneratorMode = BiomeBaseShapeGeneratorMode_GlobalElevation;
for (auto i = 0; i < static_cast<int32_t>(m_BaseShapeGenerators.size()); i++)
Expand Down
6 changes: 3 additions & 3 deletions TerraForge3D/src/Utils/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,13 @@ bool PowerOfTwoDropDown(const char* label, int32_t* value, int start, int end)
if (!value) return false;
static char buffer[32];
int tmp = (int)(log((double)*value) / log(2.0));
sprintf(buffer, "%d", (int)pow(2, tmp));
snprintf(buffer, 32, "%d", (int)pow(2, tmp));
if (ImGui::BeginCombo(label, buffer))
{
for (int i = start; i <= end; i++)
{
bool is_selected = (tmp == i);
sprintf(buffer, "%d", (int)pow(2, i));
snprintf(buffer, 32, "%d", (int)pow(2, i));
if (ImGui::Selectable(buffer, is_selected)) tmp = i;
if (is_selected) ImGui::SetItemDefaultFocus();
}
Expand Down Expand Up @@ -734,7 +734,7 @@ bool ShowSeedSettings(const std::string& label, int* seed, std::vector<int>& his
ImGui::PushID(label.c_str());
for (auto i : historyStack)
{
sprintf(s_Buffer, "%d", i);
snprintf(s_Buffer, 256, "%d", i);
if (ImGui::Selectable(s_Buffer, *seed == i))
{
*seed = i;
Expand Down

0 comments on commit daa2e74

Please sign in to comment.