Replaced sprintf by snprintf

This commit is contained in:
ritt 2023-02-06 13:22:54 +01:00
parent 15cd4bd1e0
commit 726495c1e5

View File

@ -159,7 +159,7 @@ static size_t mxml_strlcpy(char *dst, const char *src, size_t size)
static std::string toString(int i) static std::string toString(int i)
{ {
char buf[100]; char buf[100];
sprintf(buf, "%d", i); snprintf(buf, sizeof(buf), "%d", i);
return buf; return buf;
} }
@ -168,7 +168,7 @@ static std::string toString(int i)
static std::string toStrerror(int err) static std::string toStrerror(int err)
{ {
char buf[256]; char buf[256];
sprintf(buf, "errno %d (%s)", err, strerror(err)); snprintf(buf, sizeof(buf), "errno %d (%s)", err, strerror(err));
return buf; return buf;
} }
@ -240,7 +240,6 @@ MXML_WRITER *mxml_open_buffer(void)
std::string str = ctime(&now); std::string str = ctime(&now);
str.resize(24); str.resize(24);
std::string line = ""; std::string line = "";
//sprintf(line, "<!-- created by MXML on %s -->\n", str);
line += "<!-- created by MXML on "; line += "<!-- created by MXML on ";
line += str; line += str;
line += " -->\n"; line += " -->\n";
@ -279,7 +278,6 @@ MXML_WRITER *mxml_open_file(const char *file_name)
if (writer->fh == -1) { if (writer->fh == -1) {
std::string line = ""; std::string line = "";
//sprintf(line, "Unable to open file \"%s\": ", file_name);
line += "Unable to open file \""; line += "Unable to open file \"";
line += file_name; line += file_name;
line += "\": "; line += "\": ";
@ -296,7 +294,6 @@ MXML_WRITER *mxml_open_file(const char *file_name)
std::string str = ctime(&now); std::string str = ctime(&now);
str.resize(24); str.resize(24);
std::string line = ""; std::string line = "";
//sprintf(line, "<!-- created by MXML on %s -->\n", str);
line += "<!-- created by MXML on "; line += "<!-- created by MXML on ";
line += str; line += str;
line += " -->\n"; line += " -->\n";
@ -1317,14 +1314,12 @@ PMXML_NODE read_error(PMXML_NODE root, const char *file_name, int line_number, c
{ {
std::string msg; std::string msg;
if (file_name && file_name[0]) { if (file_name && file_name[0]) {
//sprintf(str, "XML read error in file \"%s\", line %d: ", file_name, line_number);
msg += "XML read error in file \""; msg += "XML read error in file \"";
msg += file_name; msg += file_name;
msg += "\", line "; msg += "\", line ";
msg += toString(line_number); msg += toString(line_number);
msg += ": "; msg += ": ";
} else { } else {
//sprintf(str, "XML read error, line %d: ", line_number);
msg += "XML read error, line "; msg += "XML read error, line ";
msg += toString(line_number); msg += toString(line_number);
msg += ": "; msg += ": ";
@ -1962,7 +1957,6 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_
if ( entity_reference_name[i][0] == DIR_SEPARATOR ) { /* absolute path */ if ( entity_reference_name[i][0] == DIR_SEPARATOR ) { /* absolute path */
filename = entity_reference_name[i]; filename = entity_reference_name[i];
} else { /* relative path */ } else { /* relative path */
//sprintf(filename, "%s%c%s", directoryname, DIR_SEPARATOR, entity_reference_name[i]);
filename += directoryname; filename += directoryname;
filename += DIR_SEPARATOR; filename += DIR_SEPARATOR;
filename += entity_reference_name[i]; filename += entity_reference_name[i];
@ -2429,7 +2423,7 @@ void mxml_dirname(char *path)
if (p == 0) /* current directory */ if (p == 0) /* current directory */
strcpy(path, "."); strcpy(path, ".");
else if (p == path) /* root directory */ else if (p == path) /* root directory */
sprintf(path, "%c", *p); snprintf(path, FILENAME_MAX, "%c", *p);
else else
*p = 0; *p = 0;