X Tutup
Skip to content

Commit 4a25b60

Browse files
authored
JANITORIAL: DEVTOOLS: replace sprintf with snprintf
1 parent 0aa12e9 commit 4a25b60

File tree

6 files changed

+30
-19
lines changed

6 files changed

+30
-19
lines changed

devtools/create_kyradat/create_kyradat.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,8 +1269,8 @@ uint32 getFilename(const ResourceProvider *provider, const ExtractFilename *fDes
12691269
return getFilename(provider->game, provider->platform, provider->special, provider->language, fDesc);
12701270
}
12711271

1272-
bool getFilename(char *dstFilename, const Game *g, const int id) {
1273-
sprintf(dstFilename, "%08X", getFilename(g, id));
1272+
bool getFilename(char *dstFilename, size_t bufcnt, const Game *g, const int id) {
1273+
snprintf(dstFilename, bufcnt, "%08X", getFilename(g, id));
12741274
return true;
12751275
}
12761276

@@ -1374,7 +1374,7 @@ bool createIDMap(PAKFile &out, const Game *g, const int *needList) {
13741374
// present
13751375
for (const int *n = needList; *n != -1; ++n) {
13761376
char filename[12];
1377-
if (!getFilename(filename, g, *n) || !out.getFileList()->findEntry(filename)) {
1377+
if (!getFilename(filename, ARRAYSIZE(filename), g, *n) || !out.getFileList()->findEntry(filename)) {
13781378
fprintf(stderr, "ERROR: Could not find need %d for game %04X", *n, createGameDef(g));
13791379
return false;
13801380
}
@@ -1398,7 +1398,7 @@ bool createIDMap(PAKFile &out, const Game *g, const int *needList) {
13981398
}
13991399

14001400
char filename[12];
1401-
if (!getFilename(filename, g, 0)) {
1401+
if (!getFilename(filename, ARRAYSIZE(filename), g, 0)) {
14021402
fprintf(stderr, "ERROR: Could not create ID map for game\n");
14031403
delete[] map;
14041404
return false;

devtools/create_mm/create_xeen/file.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ class File : public Stream {
246246
_f = fopen(filename, "rb");
247247
} else {
248248
char fname[256];
249-
sprintf(fname, "../files/xeen/%s", filename);
249+
snprintf(fname, ARRAYSIZE(fname), "../files/xeen/%s", filename);
250250
_f = fopen(fname, "wb+");
251251
}
252252

devtools/create_supernova/create_supernova.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#include "po_parser.h"
55
#include <iostream>
66

7+
#define FORBIDDEN_SYMBOL_ALLOW_ALL
8+
#include "common/util.h"
9+
710
// List of languages to look for. To add new languages you only need to change the array below
811
// and add the supporting files:
912
// - 640x480 bitmap picture for the newpaper named 'img1-##.pbm' and 'img2-##.pbm'
@@ -20,9 +23,9 @@ void writeDatafile(File& outputFile, int fileNumber, const char* language, int p
2023
File dataFile;
2124
char fileName[20];
2225
if (part == 1)
23-
sprintf(fileName, "msn_data.%03d-%s", fileNumber, language);
26+
snprintf(fileName, ARRAYSIZE(fileName), "msn_data.%03d-%s", fileNumber, language);
2427
if (part == 2)
25-
sprintf(fileName, "ms2_data.%03d-%s", fileNumber, language);
28+
snprintf(fileName, ARRAYSIZE(fileName), "ms2_data.%03d-%s", fileNumber, language);
2629
if (!dataFile.open(fileName, kFileReadMode)) {
2730
printf("Cannot find dataFile %s. This file will be skipped.\n", fileName);
2831
return;
@@ -31,7 +34,7 @@ void writeDatafile(File& outputFile, int fileNumber, const char* language, int p
3134
// Write block header in output file (4 bytes).
3235
// M(fileNumber) for example M015
3336
char number[4];
34-
sprintf(number, "%03d", fileNumber);
37+
snprintf(number, ARRAYSIZE(number), "%03d", fileNumber);
3538
outputFile.writeByte('M');
3639
for (int i = 0 ; i < 3 ; ++i) {
3740
outputFile.writeByte(number[i]);
@@ -67,9 +70,9 @@ void writeDocFile(File& outputFile, const char *fileExtension, const char* langu
6770
File docFile;
6871
char fileName[20];
6972
if (part == 1)
70-
sprintf(fileName, "msn.%s-%s", fileExtension, language);
73+
snprintf(fileName, ARRAYSIZE(fileName), "msn.%s-%s", fileExtension, language);
7174
if (part == 2)
72-
sprintf(fileName, "ms2.%s-%s", fileExtension, language);
75+
snprintf(fileName, ARRAYSIZE(fileName), "ms2.%s-%s", fileExtension, language);
7376
if (!docFile.open(fileName, kFileReadMode)) {
7477
printf("Cannot find file '%s'. This file will be skipped.\n", fileName);
7578
return;
@@ -113,7 +116,7 @@ void writeDocFile(File& outputFile, const char *fileExtension, const char* langu
113116
void writeImage(File& outputFile, const char *name, const char* language) {
114117
File imgFile;
115118
char fileName[16];
116-
sprintf(fileName, "%s-%s.pbm", name, language);
119+
snprintf(fileName, ARRAYSIZE(fileName), "%s-%s.pbm", name, language);
117120
if (!imgFile.open(fileName, kFileReadMode)) {
118121
printf("Cannot find image '%s' for language '%s'. This image will be skipped.\n", name, language);
119122
return;
@@ -256,7 +259,7 @@ void writeGermanStrings(File& outputFile, int part) {
256259

257260
void writeStrings(File& outputFile, const char* language, int part) {
258261
char fileName[16];
259-
sprintf(fileName, "strings%d-%s.po", part, language);
262+
snprintf(fileName, ARRAYSIZE(fileName), "strings%d-%s.po", part, language);
260263
PoMessageList* poList = parsePoFile(fileName);
261264
if (!poList) {
262265
printf("Cannot find strings%d file for language '%s'.\n", part, language);

devtools/create_titanic/create_titanic_dat.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ void writeResource(const char *resName, const char *sectionStr, uint32 resId, bo
11431143

11441144
void writeResource(const char *sectionStr, uint32 resId, bool isEnglish = true) {
11451145
char nameBuffer[256];
1146-
sprintf(nameBuffer, "%s/%u", sectionStr, resId);
1146+
snprintf(nameBuffer, ARRAYSIZE(nameBuffer), "%s/%u", sectionStr, resId);
11471147
if (!isEnglish)
11481148
strcat(nameBuffer, "/DE");
11491149

@@ -1160,7 +1160,7 @@ void writeResource(const char *resName, const char *sectionStr, const char *resI
11601160

11611161
void writeResource(const char *sectionStr, const char *resId, bool isEnglish = true) {
11621162
char nameBuffer[256];
1163-
sprintf(nameBuffer, "%s/%s", sectionStr, resId);
1163+
snprintf(nameBuffer, ARRAYSIZE(nameBuffer), "%s/%s", sectionStr, resId);
11641164
if (!isEnglish)
11651165
strcat(nameBuffer, "/DE");
11661166

@@ -1204,7 +1204,7 @@ void writeBitmap(const char *name, Common::File *file) {
12041204

12051205
void writeBitmap(const char *sectionStr, const char *resId, bool isEnglish = true) {
12061206
char nameBuffer[256];
1207-
sprintf(nameBuffer, "%s/%s%s", sectionStr, resId,
1207+
snprintf(nameBuffer, ARRAYSIZE(nameBuffer), "%s/%s%s", sectionStr, resId,
12081208
isEnglish ? "" : "/DE");
12091209

12101210
Common::PEResources *res = isEnglish ? resEng : resGer;
@@ -1216,7 +1216,7 @@ void writeBitmap(const char *sectionStr, const char *resId, bool isEnglish = tru
12161216

12171217
void writeBitmap(const char *sectionStr, uint32 resId, bool isEnglish = true) {
12181218
char nameBuffer[256];
1219-
sprintf(nameBuffer, "%s/%u%s", sectionStr, resId,
1219+
snprintf(nameBuffer, ARRAYSIZE(nameBuffer), "%s/%u%s", sectionStr, resId,
12201220
isEnglish ? "" : "/DE");
12211221

12221222
Common::PEResources *res = isEnglish ? resEng : resGer;

devtools/create_ultima/file.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
typedef unsigned char byte;
3333
typedef unsigned int uint32;
3434

35+
#define FORBIDDEN_SYMBOL_ALLOW_ALL
36+
#include "common/util.h"
37+
3538
#include "create_ultima.h"
3639

3740
class File {
@@ -42,7 +45,7 @@ class File {
4245
_file = fopen(filename, "rb");
4346
if (!_file) {
4447
char buf[255];
45-
sprintf(buf, "Could not open file %s", filename);
48+
snprintf(buf, ARRAYSIZE(buf), "Could not open file %s", filename);
4649
error(buf);
4750
}
4851
}

devtools/skycpt/cptcompiler.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
#include <stdlib.h>
2727
#include <string.h>
2828

29+
// Including common/util.h would cause type declaration conflicts
30+
#ifndef ARRAYSIZE
31+
#define ARRAYSIZE(x) ((int)(sizeof(x) / sizeof(x[0])))
32+
#endif
33+
2934
uint32 crop(char *line);
3035
uint16 findCptId(char *name, TextFile *cptFile);
3136

@@ -477,7 +482,7 @@ void doCompile(FILE *inf, FILE *debOutf, FILE *resOutf, TextFile *cptDef, FILE *
477482
bool filesExist = true;
478483
char inName[32];
479484
for (int i = 0; i < 7; i++) {
480-
sprintf(inName, "RESET.%03d", gameVers[i]);
485+
snprintf(inName, ARRAYSIZE(inName), "RESET.%03d", gameVers[i]);
481486
FILE *test = fopen(inName, "rb");
482487
if (test)
483488
fclose(test);
@@ -511,7 +516,7 @@ void doCompile(FILE *inf, FILE *debOutf, FILE *resOutf, TextFile *cptDef, FILE *
511516
for (int cnt = 0; cnt < 6; cnt++) {
512517
printf("Processing diff v0.0%03d\n", gameVers[cnt]);
513518
uint16 diffPos = 0;
514-
sprintf(inName, "RESET.%03d", gameVers[cnt]);
519+
snprintf(inName, ARRAYSIZE(inName), "RESET.%03d", gameVers[cnt]);
515520
FILE *resDiff = fopen(inName, "rb");
516521
fseek(resDiff, 0, SEEK_END);
517522
assert(ftell(resDiff) == (resSize * 2));

0 commit comments

Comments
 (0)
X Tutup