Skip to content

Commit 69ce3bd

Browse files
committed
voxseparate/vox2fbx: fixed warning about deprecated use of sprintf
also fixed usage of strcat_s, strcpy_s and sprintf_s missing the buffer size parameter (not sure how this could have worked on msvc) https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strcat-s-wcscat-s-mbscat-s?view=msvc-170
1 parent 0b19305 commit 69ce3bd

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

apps/vox2fbx.cpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
------------------------------------------------------------------------------------------------------------------------------------------------- */
1919
#include <string.h>
2020
#if !_MSC_VER
21-
#define strcat_s strcat
22-
#define strcpy_s strcpy
23-
#define sprintf_s sprintf
21+
#define strcpy_s strncpy
22+
#define sprintf_s snprintf
2423
#endif
2524

2625
#define OGT_VOX_IMPLEMENTATION
@@ -255,6 +254,24 @@ bool write_mesh_to_fbx(const char* output_filename, const ogt_mesh* mesh, const
255254
return true;
256255
}
257256

257+
static void make_output_filename(const char* input_filename, const char* model_name, char* output_filename, size_t size) {
258+
// Copy input_filename
259+
strcpy_s(output_filename, input_filename, size);
260+
output_filename[size - 1] = '\0'; // ensure null-termination
261+
262+
// Strip the extension
263+
char* ext = strchr(output_filename, '.');
264+
if (ext) {
265+
*ext = '\0';
266+
}
267+
268+
// Append "-", model_name, ".vox" using snprintf
269+
sprintf_s(output_filename + strlen(output_filename),
270+
size - strlen(output_filename),
271+
"-%s.fbx",
272+
model_name);
273+
}
274+
258275
int32_t main(int32_t argc, char** argv) {
259276
// just print help if no args are provided
260277
if (argc == 1) {
@@ -354,17 +371,12 @@ int32_t main(int32_t argc, char** argv) {
354371

355372
// otherwise, autogenerate a name for the model based on its index in the vox file.
356373
model_name = tmp_model_name;
357-
sprintf_s(tmp_model_name, "model%i", model_index);
374+
sprintf_s(tmp_model_name, sizeof(tmp_model_name), "model%i", model_index);
358375
}
359376

360377
// construct the output filename for this model.
361378
char output_filename[1024];
362-
strcpy_s(output_filename, input_filename);
363-
char* ext = strstr(output_filename, ".");
364-
*ext = 0;
365-
strcat_s(output_filename, "-");
366-
strcat_s(output_filename, model_name);
367-
strcat_s(output_filename, ".fbx");
379+
make_output_filename(input_filename, model_name, output_filename, sizeof(output_filename));
368380

369381
// generate a mesh for this model using the mesh_algorithm specified
370382
ogt_voxel_meshify_context ctx;

apps/voxseparate.cpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
IN THE SOFTWARE.
1717
1818
------------------------------------------------------------------------------------------------------------------------------------------------- */
19+
#include <stdio.h>
1920
#include <string.h>
2021
#if !_MSC_VER
21-
#define strcat_s strcat
22-
#define strcpy_s strcpy
23-
#define sprintf_s sprintf
22+
#define strcpy_s strncpy
23+
#define sprintf_s snprintf
2424
#endif
2525

2626
#define OGT_VOX_IMPLEMENTATION
@@ -80,6 +80,7 @@ void save_vox_scene(const char* pcFilename, const ogt_vox_scene* scene)
8080
// open the file for write
8181
FILE * fp = open_file(pcFilename, "wb");
8282
if (!fp) {
83+
fprintf(stderr, "ERROR: could not open file '%s' for write - aborting!\n", pcFilename);
8384
ogt_vox_free(buffer);
8485
return;
8586
}
@@ -102,6 +103,24 @@ void print_help() {
102103
);
103104
}
104105

106+
static void make_output_filename(const char* input_filename, const char* model_name, char* output_filename, size_t size) {
107+
// Copy input_filename
108+
strcpy_s(output_filename, input_filename, size);
109+
output_filename[size - 1] = '\0'; // ensure null-termination
110+
111+
// Strip the extension
112+
char* ext = strchr(output_filename, '.');
113+
if (ext) {
114+
*ext = '\0';
115+
}
116+
117+
// Append "-", model_name, ".vox" using snprintf
118+
sprintf_s(output_filename + strlen(output_filename),
119+
size - strlen(output_filename),
120+
"-%s.vox",
121+
model_name);
122+
}
123+
105124
int32_t main(int32_t argc, char** argv) {
106125
// validate we have at least one input file provided on command-line.
107126
if (argc <= 1) {
@@ -159,7 +178,7 @@ int32_t main(int32_t argc, char** argv) {
159178
if (!model_name) {
160179
// otherwise, autogenerate a name for the model based on its index in the vox file.
161180
model_name = tmp_model_name;
162-
sprintf_s(tmp_model_name, "model%i", model_index);
181+
sprintf_s(tmp_model_name, sizeof(tmp_model_name), "model%i", model_index);
163182
}
164183

165184
// construct the scene with a single instance referencing this model
@@ -186,12 +205,7 @@ int32_t main(int32_t argc, char** argv) {
186205

187206
// construct the output filename for this model.
188207
char output_filename[1024];
189-
strcpy_s(output_filename, input_filename);
190-
char* ext = strstr(output_filename, ".");
191-
*ext = 0;
192-
strcat_s(output_filename, "-");
193-
strcat_s(output_filename, model_name);
194-
strcat_s(output_filename, ".vox");
208+
make_output_filename(input_filename, model_name, output_filename, sizeof(output_filename));
195209

196210
save_vox_scene(output_filename, &output_scene);
197211
}

0 commit comments

Comments
 (0)