Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace sprintf with more secure snprintf #32

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 61 additions & 60 deletions tools/MSXtk/src/exporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <iostream>
#include <fstream>
#include <ctime>
#include <stdio.h>
// MSXi
#include "MSXimg.h"
#include "types.h"
Expand Down Expand Up @@ -228,7 +229,7 @@ class ExporterText : public ExporterInterface
std::time_t result = std::time(nullptr);
char* ltime = std::asctime(std::localtime(&result));
ltime[strlen(ltime) - 1] = 0; // remove final '\n'
sprintf(strData, "Data generated using MSXimg %s on %s", MSXi_VERSION, ltime);
snprintf(strData, BUFFER_SIZE, "Data generated using MSXimg %s on %s", MSXi_VERSION, ltime);
WriteCommentLine(strData);

// Add author & license
Expand Down Expand Up @@ -336,7 +337,7 @@ class ExporterC: public ExporterText
{
if (Param->bStartAddr)
{
sprintf(strData,
snprintf(strData, BUFFER_SIZE,
"\n"
"// %s\n"
"%s =\n"
Expand All @@ -345,7 +346,7 @@ class ExporterC: public ExporterText
}
else if (Param->bDefine)
{
sprintf(strData,
snprintf(strData, BUFFER_SIZE,
"\n"
"#ifndef D_%s\n"
"\t#define D_%s\n"
Expand All @@ -357,7 +358,7 @@ class ExporterC: public ExporterText
}
else
{
sprintf(strData,
snprintf(strData, BUFFER_SIZE,
"\n"
"// %s\n"
"%s =\n"
Expand All @@ -369,58 +370,58 @@ class ExporterC: public ExporterText

virtual void WriteSpriteHeader(i32 number)
{
sprintf(strData,
snprintf(strData, BUFFER_SIZE,
"// Sprite[%i] (offset:%i)\n", number, TotalBytes);
outData += strData;
}

virtual void WriteCommentLine(std::string comment)
{
sprintf(strData, "// %s\n", comment.c_str());
snprintf(strData, BUFFER_SIZE, "// %s\n", comment.c_str());
outData += strData;
}

virtual void Write4BytesLine(u8 a, u8 b, u8 c, u8 d, std::string comment)
{
sprintf(strFormat,
snprintf(strFormat, BUFFER_SIZE,
"\t%s, %s, %s, %s, // %s\n", GetNumberFormat(), GetNumberFormat(), GetNumberFormat(), GetNumberFormat(), comment.c_str());
sprintf(strData, strFormat, a, b, c, d);
snprintf(strData, BUFFER_SIZE, strFormat, a, b, c, d);
outData += strData;
TotalBytes += 4;
}

virtual void Write2BytesLine(u8 a, u8 b, std::string comment)
{
sprintf(strFormat,
snprintf(strFormat, BUFFER_SIZE,
"\t%s, %s, // %s\n", GetNumberFormat(), GetNumberFormat(), comment.c_str());
sprintf(strData, strFormat, a, b);
snprintf(strData, BUFFER_SIZE, strFormat, a, b);
outData += strData;
TotalBytes += 2;
}

virtual void Write1ByteLine(u8 a, std::string comment)
{
sprintf(strFormat,
snprintf(strFormat, BUFFER_SIZE,
"\t%s, // %s\n", GetNumberFormat(), comment.c_str());
sprintf(strData, strFormat, a);
snprintf(strData, BUFFER_SIZE, strFormat, a);
outData += strData;
TotalBytes += 1;
}

virtual void Write1WordLine(u16 a, std::string comment)
{
sprintf(strFormat,
snprintf(strFormat, BUFFER_SIZE,
"\t%s, // %s\n", GetNumberFormat(2), comment.c_str());
sprintf(strData, strFormat, a);
snprintf(strData, BUFFER_SIZE, strFormat, a);
outData += strData;
TotalBytes += 2;
}

virtual void Write2WordsLine(u16 a, u16 b, std::string comment)
{
sprintf(strFormat,
snprintf(strFormat, BUFFER_SIZE,
"\t%s, %s, // %s\n", GetNumberFormat(2), GetNumberFormat(2), comment.c_str());
sprintf(strData, strFormat, a, b);
snprintf(strData, BUFFER_SIZE, strFormat, a, b);
outData += strData;
TotalBytes += 4;
}
Expand All @@ -432,17 +433,17 @@ class ExporterC: public ExporterText

virtual void Write1ByteData(u8 data)
{
sprintf(strFormat, "%s, ", GetNumberFormat());
sprintf(strData, strFormat, data);
snprintf(strFormat, BUFFER_SIZE, "%s, ", GetNumberFormat());
snprintf(strData, BUFFER_SIZE, strFormat, data);
outData += strData;
TotalBytes += 1;
}

virtual void Write8BitsData(u8 data)
{
sprintf(strFormat,
snprintf(strFormat, BUFFER_SIZE,
"%s, /* %%c%%c%%c%%c%%c%%c%%c%%c */ ", GetNumberFormat());
sprintf(strData, strFormat, data,
snprintf(strData, BUFFER_SIZE, strFormat, data,
data & 0x80 ? '#' : '.',
data & 0x40 ? '#' : '.',
data & 0x20 ? '#' : '.',
Expand All @@ -465,7 +466,7 @@ class ExporterC: public ExporterText
outData += "};\n";
if (comment != "")
{
sprintf(strData,
snprintf(strData, BUFFER_SIZE,
"// %s\n", comment.c_str());
outData += strData;
}
Expand All @@ -483,7 +484,7 @@ class ExporterASM: public ExporterText

virtual void WriteTableBegin(TableFormat format, std::string name, std::string comment)
{
sprintf(strData,
snprintf(strData, BUFFER_SIZE,
"\n"
"; %s\n"
"%s:\n",
Expand All @@ -493,58 +494,58 @@ class ExporterASM: public ExporterText

virtual void WriteSpriteHeader(i32 number)
{
sprintf(strData,
snprintf(strData, BUFFER_SIZE,
"; Sprite[%i] (offset:%i)\n", number, TotalBytes);
outData += strData;
}

virtual void WriteCommentLine(std::string comment)
{
sprintf(strData, "; %s\n", comment.c_str());
snprintf(strData, BUFFER_SIZE, "; %s\n", comment.c_str());
outData += strData;
}

virtual void Write4BytesLine(u8 a, u8 b, u8 c, u8 d, std::string comment)
{
sprintf(strFormat,
snprintf(strFormat, BUFFER_SIZE,
"\t%s %s, %s, %s, %s, ; %s\n", GetDataFormat(1), GetNumberFormat(), GetNumberFormat(), GetNumberFormat(), GetNumberFormat(), comment.c_str());
sprintf(strData, strFormat, a, b, c, d);
snprintf(strData, BUFFER_SIZE, strFormat, a, b, c, d);
outData += strData;
TotalBytes += 4;
}

virtual void Write2BytesLine(u8 a, u8 b, std::string comment)
{
sprintf(strFormat,
snprintf(strFormat, BUFFER_SIZE,
"\t%s %s, %s, ; %s\n", GetDataFormat(1), GetNumberFormat(), GetNumberFormat(), comment.c_str());
sprintf(strData, strFormat, a, b);
snprintf(strData, BUFFER_SIZE, strFormat, a, b);
outData += strData;
TotalBytes += 2;
}

virtual void Write1ByteLine(u8 a, std::string comment)
{
sprintf(strFormat,
snprintf(strFormat, BUFFER_SIZE,
"\t%s %s, ; %s\n", GetDataFormat(1), GetNumberFormat(), comment.c_str());
sprintf(strData, strFormat, a);
snprintf(strData, BUFFER_SIZE, strFormat, a);
outData += strData;
TotalBytes += 1;
}

virtual void Write1WordLine(u16 a, std::string comment)
{
sprintf(strFormat,
snprintf(strFormat, BUFFER_SIZE,
"\t%s %s, ; %s\n", GetDataFormat(2), GetNumberFormat(2), comment.c_str());
sprintf(strData, strFormat, a);
snprintf(strData, BUFFER_SIZE, strFormat, a);
outData += strData;
TotalBytes += 2;
}

virtual void Write2WordsLine(u16 a, u16 b, std::string comment)
{
sprintf(strFormat,
snprintf(strFormat, BUFFER_SIZE,
"\t%s %s, %s, ; %s\n", GetDataFormat(2), GetNumberFormat(), GetNumberFormat(), comment.c_str());
sprintf(strData, strFormat, a, b);
snprintf(strData, BUFFER_SIZE, strFormat, a, b);
outData += strData;
TotalBytes += 4;
}
Expand All @@ -558,16 +559,16 @@ class ExporterASM: public ExporterText

virtual void Write1ByteData(u8 data)
{
sprintf(strFormat, "%s, ", GetNumberFormat());
sprintf(strData, strFormat, data);
snprintf(strFormat, BUFFER_SIZE, "%s, ", GetNumberFormat());
snprintf(strData, BUFFER_SIZE, strFormat, data);
outData += strData;
TotalBytes += 1;
}

virtual void Write8BitsData(u8 data)
{
sprintf(strFormat, "%s, ", GetNumberFormat());
sprintf(strData, strFormat, data);
snprintf(strFormat, BUFFER_SIZE, "%s, ", GetNumberFormat());
snprintf(strData, BUFFER_SIZE, strFormat, data);
outData += strData;
TotalBytes += 1;
}
Expand All @@ -581,7 +582,7 @@ class ExporterASM: public ExporterText
{
if (comment != "")
{
sprintf(strData,
snprintf(strData, BUFFER_SIZE,
"; %s\n", comment.c_str());
outData += strData;
}
Expand All @@ -604,7 +605,7 @@ class ExporterBASIC : public ExporterText

virtual void WriteTableBegin(TableFormat format, std::string name, std::string comment)
{
sprintf(strData,
snprintf(strData, BUFFER_SIZE,
"\n"
"%i '%s:%s\n",
Line++, name.c_str(), comment.c_str());
Expand All @@ -613,76 +614,76 @@ class ExporterBASIC : public ExporterText

virtual void WriteSpriteHeader(i32 number)
{
sprintf(strData,
snprintf(strData, BUFFER_SIZE,
"%i 'Sprite[%i] (offset:%i)\n", Line++, number, TotalBytes);
outData += strData;
}

virtual void WriteCommentLine(std::string comment)
{
sprintf(strData, "%i '%s\n", Line++, comment.c_str());
snprintf(strData, BUFFER_SIZE, "%i '%s\n", Line++, comment.c_str());
outData += strData;
}

virtual void Write4BytesLine(u8 a, u8 b, u8 c, u8 d, std::string comment)
{
sprintf(strFormat,
snprintf(strFormat, BUFFER_SIZE,
"%i DATA %s,%s,%s,%s '%s\n", Line++, GetNumberFormat(), GetNumberFormat(), GetNumberFormat(), GetNumberFormat(), comment.c_str());
sprintf(strData, strFormat, a, b, c, d);
snprintf(strData, BUFFER_SIZE, strFormat, a, b, c, d);
outData += strData;
TotalBytes += 4;
}

virtual void Write2BytesLine(u8 a, u8 b, std::string comment)
{
sprintf(strFormat,
snprintf(strFormat, BUFFER_SIZE,
"%i DATA %s,%s '%s\n", Line++, GetNumberFormat(), GetNumberFormat(), comment.c_str());
sprintf(strData, strFormat, a, b);
snprintf(strData, BUFFER_SIZE, strFormat, a, b);
outData += strData;
TotalBytes += 2;
}

virtual void Write1ByteLine(u8 a, std::string comment)
{
sprintf(strFormat,
snprintf(strFormat, BUFFER_SIZE,
"%i DATA %s '%s\n", Line++, GetNumberFormat(), comment.c_str());
sprintf(strData, strFormat, a);
snprintf(strData, BUFFER_SIZE, strFormat, a);
outData += strData;
TotalBytes += 1;
}

virtual void Write1WordLine(u16 a, std::string comment)
{
sprintf(strFormat,
snprintf(strFormat, BUFFER_SIZE,
"%i DATA %s '%s\n", Line++, GetNumberFormat(2), comment.c_str());
sprintf(strData, strFormat, a);
snprintf(strData, BUFFER_SIZE, strFormat, a);
outData += strData;
TotalBytes += 2;
}

virtual void Write2WordsLine(u16 a, u16 b, std::string comment)
{
sprintf(strFormat,
snprintf(strFormat, BUFFER_SIZE,
"%i DATA %s,%s '%s\n", Line++, GetNumberFormat(), GetNumberFormat(), comment.c_str());
sprintf(strData, strFormat, a, b);
snprintf(strData, BUFFER_SIZE, strFormat, a, b);
outData += strData;
TotalBytes += 4;
}

virtual void WriteLineBegin()
{
sprintf(strData, "%i DATA ", Line++);
snprintf(strData, BUFFER_SIZE, "%i DATA ", Line++);
outData += strData;
bLineStart = TRUE;
}

virtual void Write1ByteData(u8 data)
{
if (bLineStart)
sprintf(strFormat, "%s", GetNumberFormat());
snprintf(strFormat, BUFFER_SIZE, "%s", GetNumberFormat());
else
sprintf(strFormat, ",%s", GetNumberFormat());
sprintf(strData, strFormat, data);
snprintf(strFormat, BUFFER_SIZE, ",%s", GetNumberFormat());
snprintf(strData, BUFFER_SIZE, strFormat, data);
outData += strData;
TotalBytes += 1;
bLineStart = FALSE;
Expand All @@ -691,10 +692,10 @@ class ExporterBASIC : public ExporterText
virtual void Write8BitsData(u8 data)
{
if (bLineStart)
sprintf(strFormat, "%s", GetNumberFormat());
snprintf(strFormat, BUFFER_SIZE, "%s", GetNumberFormat());
else
sprintf(strFormat, ",%s", GetNumberFormat());
sprintf(strData, strFormat, data);
snprintf(strFormat, BUFFER_SIZE, ",%s", GetNumberFormat());
snprintf(strData, BUFFER_SIZE, strFormat, data);
outData += strData;
TotalBytes += 1;
bLineStart = FALSE;
Expand All @@ -710,7 +711,7 @@ class ExporterBASIC : public ExporterText
{
if (comment != "")
{
sprintf(strData,
snprintf(strData, BUFFER_SIZE,
"' %s\n", comment.c_str());
outData += strData;
}
Expand Down
Loading