Cleaned up usage of sprintf in the codebase, changing to

BSPF_snprintf instead, which should eliminate any potential
buffer overflows.  Also moved from static arrays to stringstreams
where appropriate to make things safer and more C++ like.


git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@2263 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba
This commit is contained in:
stephena 2011-07-13 14:13:49 +00:00
parent d6e1360dae
commit 40a66edee4
13 changed files with 34 additions and 100 deletions

View File

@ -185,8 +185,8 @@ string CartDebug::toString()
// bytes have been previously output
if(state.rport[i] - curraddr > bytesPerLine || bytesSoFar >= 256)
{
char port[50];
sprintf(port, "%04x: (rport = %04x, wport = %04x)\n",
char port[37];
BSPF_snprintf(port, 36, "%04x: (rport = %04x, wport = %04x)\n",
state.rport[i], state.rport[i], state.wport[i]);
port[2] = port[3] = 'x';
buf << DebuggerParser::red(port);

View File

@ -71,65 +71,6 @@ void CpuDebug::saveOldState()
Debugger::set_bits(myOldState.PS, myOldState.PSbits);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string CpuDebug::toString()
{
// TODO - this doesn't seem to be used anywhere ??
// if it's ever used, convert to C++ stringstream
string result;
char buf[255];
const CpuState& state = (CpuState&) getState();
const CpuState& oldstate = (CpuState&) getOldState();
result += "\nPC=";
result += myDebugger.invIfChanged(state.PC, oldstate.PC);
result += " A=";
result += myDebugger.invIfChanged(state.A, oldstate.A);
result += " X=";
result += myDebugger.invIfChanged(state.X, oldstate.X);
result += " Y=";
result += myDebugger.invIfChanged(state.Y, oldstate.Y);
result += " S=";
result += myDebugger.invIfChanged(state.SP, oldstate.SP);
result += " P=";
result += myDebugger.invIfChanged(state.PS, oldstate.PS);
result += "/";
// NV-BDIZC
buf[0] = n() ? 'N' : 'n';
buf[1] = v() ? 'V' : 'v';
buf[2] = '-';
buf[3] = b() ? 'B' : 'b';
buf[4] = d() ? 'D' : 'd';
buf[5] = i() ? 'I' : 'i';
buf[6] = z() ? 'Z' : 'z';
buf[7] = c() ? 'C' : 'c';
buf[8] = '\0';
result += buf;
result += "\n FrameCyc:";
sprintf(buf, "%d", mySystem.cycles());
result += buf;
result += " Frame:";
sprintf(buf, "%d", myDebugger.tiaDebug().frameCount());
result += buf;
result += " ScanLine:";
sprintf(buf, "%d", myDebugger.tiaDebug().scanlines());
result += buf;
result += " Clk/Pix/Cyc:";
int clk = myDebugger.tiaDebug().clocksThisLine();
sprintf(buf, "%d/%d/%d", clk, clk-68, clk/3);
result += buf;
result += " 6502Ins:";
sprintf(buf, "%d", mySystem.m6502().totalInstructionCount());
result += buf;
result += "\n ";
result += myDebugger.cartDebug().disassemble(state.PC, 1);
return result;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CpuDebug::setPC(int pc)
{

View File

@ -50,7 +50,7 @@ class CpuDebug : public DebuggerSystem
const DebuggerState& getOldState() { return myOldState; }
void saveOldState();
string toString();
string toString() { return ""; } // Not needed, since CPU stuff is always visible
// I know, we ain't supposed to do this...
M6502& m6502() { return mySystem.m6502(); }

View File

@ -372,22 +372,19 @@ void Debugger::reset()
/* Element 0 of args is the address. The remaining elements are the data
to poke, starting at the given address.
*/
const string Debugger::setRAM(IntArray& args)
string Debugger::setRAM(IntArray& args)
{
char buf[10];
ostringstream buf;
int count = args.size();
int address = args[0];
for(int i=1; i<count; i++)
for(int i = 1; i < count; ++i)
mySystem->poke(address++, args[i]);
string ret = "changed ";
sprintf(buf, "%d", count-1);
ret += buf;
ret += " location";
if(count != 0)
ret += "s";
return ret;
buf << "changed " << (count-1) << " location";
if(count != 2)
buf << "s";
return buf.str();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -296,7 +296,7 @@ class Debugger : public DialogContainer
void reloadROM();
// Set a bunch of RAM locations at once
const string setRAM(IntArray& args);
string setRAM(IntArray& args);
void reset();
void clearAllBreakPoints();

View File

@ -292,31 +292,25 @@ int DebuggerParser::decipher_arg(const string &str)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string DebuggerParser::showWatches()
{
string ret;
char buf[10];
for(unsigned int i=0; i<watches.size(); i++) {
if(watches[i] != "") {
ostringstream buf;
for(unsigned int i = 0; i < watches.size(); i++)
{
if(watches[i] != "")
{
// Clear the args, since we're going to pass them to eval()
argStrings.clear();
args.clear();
sprintf(buf, "%d", i+1);
argCount = 1;
argStrings.push_back(watches[i]);
args.push_back(decipher_arg(argStrings[0]));
if(args[0] < 0) {
ret += "BAD WATCH ";
ret += buf;
ret += ": " + argStrings[0] + "\n";
} else {
ret += " watch #";
ret += buf;
ret += " (" + argStrings[0] + ") -> " + eval() + "\n";
}
if(args[0] < 0)
buf << "BAD WATCH " << (i+1) << ": " << argStrings[0] << endl;
else
buf << " watch #" << (i+1) << " (" << argStrings[0] << ") -> " << eval() << endl;
}
}
return ret;
return buf.str();
}

View File

@ -916,8 +916,8 @@ void DiStella::addEntry(CartDebug::DisasmType type)
else if(settings.show_addresses && tag.type == CartDebug::CODE)
{
// Have addresses indented, to differentiate from actual labels
char address[8];
sprintf(address, " %X", tag.address);
char address[7];
BSPF_snprintf(address, 6, " %4X", tag.address);
tag.label = address;
tag.hllabel = false;
}

View File

@ -698,14 +698,14 @@ string TIADebug::colorSwatch(uInt8 c)
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const string audFreq(uInt8 div)
string TIADebug::audFreq(uInt8 div)
{
string ret;
char buf[20];
char buf[10];
double hz = 30000.0;
if(div) hz /= div;
sprintf(buf, "%5.1f", hz);
BSPF_snprintf(buf, 9, "%5.1f", hz);
ret += buf;
ret += "Hz";

View File

@ -169,6 +169,7 @@ class TIADebug : public DebuggerSystem
/** Get/set specific bits in the collision register (used by collXX_XX) */
bool collision(int collID, int newVal);
string audFreq(uInt8 div);
string booleanWithLabel(string label, bool value);
private:

View File

@ -303,12 +303,12 @@ void RamWidget::fillGrid(bool updateOld)
// Update RAM labels
char buf[5];
sprintf(buf, "%04x", state.rport[start] & 0xff00);
BSPF_snprintf(buf, 5, "%04X", state.rport[start] & 0xff00);
buf[2] = buf[3] = 'x';
myRamStart->setLabel(buf);
for(uInt32 i = start, row = 0; i < start + 16*8; i += 16, ++row)
{
sprintf(buf, "%02x:", state.rport[i] & 0x00ff);
BSPF_snprintf(buf, 3, "%02X:", state.rport[i] & 0x00ff);
myRamLabels[row]->setLabel(buf);
}
}

View File

@ -221,7 +221,7 @@ void FrameBuffer::update()
{
const ConsoleInfo& info = myOSystem->console().about();
char msg[30];
sprintf(msg, "%u @ %2.2ffps => %s",
BSPF_snprintf(msg, 29, "%u @ %2.2ffps => %s",
myOSystem->console().tia().scanlines(),
myOSystem->console().getFramerate(), info.DisplayFormat.c_str());
myStatsMsg.surface->fillRect(0, 0, myStatsMsg.w, myStatsMsg.h, kBGColor);

View File

@ -79,7 +79,7 @@ void StringListWidget::drawWidget(bool hilite)
if (_numberingMode != kListNumberingOff)
{
char temp[10];
sprintf(temp, "%2d. ", (pos + _numberingMode));
BSPF_snprintf(temp, 9, "%2d. ", (pos + _numberingMode));
buffer = temp;
s.drawString(_font, buffer, _x + 2, y, _w - 4, textColor);
}
@ -120,7 +120,7 @@ GUI::Rect StringListWidget::getEditRect() const
{
char temp[10];
// FIXME: Assumes that all digits have the same width.
sprintf(temp, "%2d. ", (_list.size() - 1 + _numberingMode));
BSPF_snprintf(temp, 9, "%2d. ", (_list.size() - 1 + _numberingMode));
r.left += _font->getStringWidth(temp);
}

View File

@ -323,7 +323,7 @@ StaticTextWidget::StaticTextWidget(GuiObject *boss, const GUI::Font& font,
void StaticTextWidget::setValue(int value)
{
char buf[256];
sprintf(buf, "%d", value);
BSPF_snprintf(buf, 255, "%d", value);
_label = buf;
setDirty(); draw();
@ -333,6 +333,7 @@ void StaticTextWidget::setValue(int value)
void StaticTextWidget::setLabel(const string& label)
{
_label = label;
setDirty(); draw();
}