Update remaining sprintf calls with a stack buffer to use ssprintf.

The commit 11f29b123 has replaced most of the uses of sprintf,
but there were still many remaining in Screen* functions, and it
was annoyingly inconsistent. Moreover, while most usage of sprintf
there was fine, it is bad hygiene to leave stack overflow prone
code around.
This commit is contained in:
EvilSpirit 2016-01-27 11:13:04 +06:00 committed by whitequark
parent b7409b8ad6
commit fd0b7fbc29
11 changed files with 43 additions and 82 deletions

View File

@ -301,23 +301,19 @@ bool TextWindow::EditControlDoneForPaste(const char *s) {
}
void TextWindow::ScreenChangePasteTransformed(int link, uint32_t v) {
char str[300];
switch(link) {
case 't':
sprintf(str, "%d", SS.TW.shown.paste.times);
SS.TW.ShowEditControl(10, 13, str);
SS.TW.ShowEditControl(10, 13, ssprintf("%d", SS.TW.shown.paste.times));
SS.TW.edit.meaning = EDIT_PASTE_TIMES_REPEATED;
break;
case 'r':
sprintf(str, "%.3f", SS.TW.shown.paste.theta*180/PI);
SS.TW.ShowEditControl(12, 13, str);
SS.TW.ShowEditControl(12, 13, ssprintf("%.3f", SS.TW.shown.paste.theta*180/PI));
SS.TW.edit.meaning = EDIT_PASTE_ANGLE;
break;
case 's':
sprintf(str, "%.3f", SS.TW.shown.paste.scale);
SS.TW.ShowEditControl(18, 13, str);
SS.TW.ShowEditControl(18, 13, ssprintf("%.3f", SS.TW.shown.paste.scale));
SS.TW.edit.meaning = EDIT_PASTE_SCALE;
break;
}

View File

@ -7,17 +7,13 @@
#include "solvespace.h"
void TextWindow::ScreenChangeLightDirection(int link, uint32_t v) {
char str[1024];
sprintf(str, "%.2f, %.2f, %.2f", CO(SS.lightDir[v]));
SS.TW.ShowEditControl(29+2*v, 8, str);
SS.TW.ShowEditControl(29+2*v, 8, ssprintf("%.2f, %.2f, %.2f", CO(SS.lightDir[v])));
SS.TW.edit.meaning = EDIT_LIGHT_DIRECTION;
SS.TW.edit.i = v;
}
void TextWindow::ScreenChangeLightIntensity(int link, uint32_t v) {
char str[1024];
sprintf(str, "%.2f", SS.lightIntensity[v]);
SS.TW.ShowEditControl(29+2*v, 31, str);
SS.TW.ShowEditControl(29+2*v, 31, ssprintf("%.2f", SS.lightIntensity[v]));
SS.TW.edit.meaning = EDIT_LIGHT_INTENSITY;
SS.TW.edit.i = v;
}
@ -30,23 +26,17 @@ void TextWindow::ScreenChangeColor(int link, uint32_t v) {
}
void TextWindow::ScreenChangeChordTolerance(int link, uint32_t v) {
char str[1024];
sprintf(str, "%.2f", SS.chordTol);
SS.TW.ShowEditControl(37, 3, str);
SS.TW.ShowEditControl(37, 3, ssprintf("%.2f", SS.chordTol));
SS.TW.edit.meaning = EDIT_CHORD_TOLERANCE;
}
void TextWindow::ScreenChangeMaxSegments(int link, uint32_t v) {
char str[1024];
sprintf(str, "%d", SS.maxSegments);
SS.TW.ShowEditControl(41, 3, str);
SS.TW.ShowEditControl(41, 3, ssprintf("%d", SS.maxSegments));
SS.TW.edit.meaning = EDIT_MAX_SEGMENTS;
}
void TextWindow::ScreenChangeCameraTangent(int link, uint32_t v) {
char str[1024];
sprintf(str, "%.3f", 1000*SS.cameraTangent);
SS.TW.ShowEditControl(47, 3, str);
SS.TW.ShowEditControl(47, 3, ssprintf("%.3f", 1000*SS.cameraTangent));
SS.TW.edit.meaning = EDIT_CAMERA_TANGENT;
}
@ -56,17 +46,12 @@ void TextWindow::ScreenChangeGridSpacing(int link, uint32_t v) {
}
void TextWindow::ScreenChangeDigitsAfterDecimal(int link, uint32_t v) {
char buf[128];
sprintf(buf, "%d", SS.UnitDigitsAfterDecimal());
SS.TW.ShowEditControl(55, 3, buf);
SS.TW.ShowEditControl(55, 3, ssprintf("%d", SS.UnitDigitsAfterDecimal()));
SS.TW.edit.meaning = EDIT_DIGITS_AFTER_DECIMAL;
}
void TextWindow::ScreenChangeExportScale(int link, uint32_t v) {
char str[1024];
sprintf(str, "%.3f", (double)SS.exportScale);
SS.TW.ShowEditControl(61, 5, str);
SS.TW.ShowEditControl(61, 5, ssprintf("%.3f", (double)SS.exportScale));
SS.TW.edit.meaning = EDIT_EXPORT_SCALE;
}

View File

@ -495,18 +495,18 @@ void SvgFileWriter::StartPath(RgbaColor strokeRgb, double lineWidth,
void SvgFileWriter::FinishPath(RgbaColor strokeRgb, double lineWidth,
bool filled, RgbaColor fillRgb)
{
char fill[100];
std::string fill;
if(filled) {
sprintf(fill, "#%02x%02x%02x",
fill = ssprintf("#%02x%02x%02x",
fillRgb.red, fillRgb.green, fillRgb.blue);
} else {
strcpy(fill, "none");
fill = "none";
}
fprintf(f, "' stroke-width='%.3f' stroke='#%02x%02x%02x' "
"stroke-linecap='round' stroke-linejoin='round' "
"fill='%s' />\r\n",
lineWidth, strokeRgb.red, strokeRgb.green, strokeRgb.blue,
fill);
fill.c_str());
}
void SvgFileWriter::MaybeMoveTo(Vector st, Vector fi) {

View File

@ -194,8 +194,7 @@ void SolveSpaceUI::GenerateAll(int first, int last, bool andFindFree) {
displayedStatusMessage)
{
displayedStatusMessage = true;
char msg[1024];
sprintf(msg, "generating group %d/%d", i, SK.group.n);
std::string msg = ssprintf("generating group %d/%d", i, SK.group.n);
int w, h;
GetGraphicsWindowSize(&w, &h);
@ -218,7 +217,7 @@ void SolveSpaceUI::GenerateAll(int first, int last, bool andFindFree) {
glPushMatrix();
glTranslated(left+8, top-20, 0);
glScaled(1, -1, 1);
ssglBitmapText(msg, Vector::From(0, 0, 0));
ssglBitmapText(msg.c_str(), Vector::From(0, 0, 0));
glPopMatrix();
glFlush();
glDrawBuffer(GL_BACK);

View File

@ -581,17 +581,15 @@ void SolveSpaceUI::MenuAnalyze(int id) {
"The mesh has naked edges (NOT okay, invalid)." :
"The mesh is watertight (okay, valid).";
char cntMsg[1024];
sprintf(cntMsg, "\n\nThe model contains %d triangles, from "
"%d surfaces.",
g->displayMesh.l.n, g->runningShell.surface.n);
std::string cntMsg = ssprintf("\n\nThe model contains %d triangles, from "
"%d surfaces.", g->displayMesh.l.n, g->runningShell.surface.n);
if(SS.nakedEdges.l.n == 0) {
Message("%s\n\n%s\n\nZero problematic edges, good.%s",
intersMsg, leaksMsg, cntMsg);
intersMsg, leaksMsg, cntMsg.c_str());
} else {
Error("%s\n\n%s\n\n%d problematic edges, bad.%s",
intersMsg, leaksMsg, SS.nakedEdges.l.n, cntMsg);
intersMsg, leaksMsg, SS.nakedEdges.l.n, cntMsg.c_str());
}
break;
}
@ -672,19 +670,16 @@ void SolveSpaceUI::MenuAnalyze(int id) {
vol += integral;
}
char msg[1024];
sprintf(msg, "The volume of the solid model is:\n\n"
" %.3f %s^3",
std::string msg = ssprintf("The volume of the solid model is:\n\n"" %.3f %s^3",
vol / pow(SS.MmPerUnit(), 3),
SS.UnitName());
if(SS.viewUnits == SolveSpaceUI::UNIT_MM) {
sprintf(msg+strlen(msg), "\n %.2f mL", vol/(10*10*10));
msg += ssprintf("\n %.2f mL", vol/(10*10*10));
}
strcpy(msg+strlen(msg),
"\n\nCurved surfaces have been approximated as triangles.\n"
"This introduces error, typically of around 1%.");
Message("%s", msg);
msg += "\n\nCurved surfaces have been approximated as triangles.\n"
"This introduces error, typically of around 1%.";
Message("%s", msg.c_str());
break;
}

View File

@ -417,10 +417,8 @@ err:
}
void TextWindow::ScreenChangeBackgroundImageScale(int link, uint32_t v) {
char str[300];
sprintf(str, "%.3f", SS.bgImage.scale * SS.MmPerUnit());
SS.TW.edit.meaning = EDIT_BACKGROUND_IMG_SCALE;
SS.TW.ShowEditControl(v, 10, str);
SS.TW.ShowEditControl(v, 10, ssprintf("%.3f", SS.bgImage.scale * SS.MmPerUnit()));
}
void TextWindow::ShowListOfStyles(void) {

View File

@ -229,9 +229,7 @@ void TextWindow::ScreenColor(int link, uint32_t v) {
void TextWindow::ScreenOpacity(int link, uint32_t v) {
Group *g = SK.GetGroup(SS.TW.shown.group);
char str[1024];
sprintf(str, "%.2f", g->color.alphaF());
SS.TW.ShowEditControl(22, 11, str);
SS.TW.ShowEditControl(22, 11, ssprintf("%.2f", g->color.alphaF()));
SS.TW.edit.meaning = EDIT_GROUP_OPACITY;
SS.TW.edit.group.v = g->h.v;
}
@ -241,9 +239,7 @@ void TextWindow::ScreenChangeExprA(int link, uint32_t v) {
// There's an extra line for the skipFirst parameter in one-sided groups.
int r = (g->subtype == Group::ONE_SIDED) ? 16 : 14;
char str[1024];
sprintf(str, "%d", (int)g->valA);
SS.TW.ShowEditControl(r, 10, str);
SS.TW.ShowEditControl(r, 10, ssprintf("%d", (int)g->valA));
SS.TW.edit.meaning = EDIT_TIMES_REPEATED;
SS.TW.edit.group.v = v;
}

View File

@ -91,9 +91,6 @@ void TextWindow::ShowEditControl(int halfRow, int col, const std::string &str) {
void TextWindow::ShowEditControlWithColorPicker(int halfRow, int col, RgbaColor rgb)
{
char str[1024];
sprintf(str, "%.2f, %.2f, %.2f", rgb.redF(), rgb.greenF(), rgb.blueF());
SS.ScheduleShowTW();
editControl.colorPicker.show = true;
@ -101,7 +98,7 @@ void TextWindow::ShowEditControlWithColorPicker(int halfRow, int col, RgbaColor
editControl.colorPicker.h = 0;
editControl.colorPicker.s = 0;
editControl.colorPicker.v = 1;
ShowEditControl(halfRow, col, str);
ShowEditControl(halfRow, col, ssprintf("%.2f, %.2f, %.2f", rgb.redF(), rgb.greenF(), rgb.blueF()));
}
void TextWindow::ClearScreen(void) {
@ -437,23 +434,23 @@ void TextWindow::DrawOrHitTestIcons(int how, double mx, double my)
if(tooltippedIcon) {
if(how == PAINT) {
char str[1024];
std::string str;
if(tooltippedIcon->icon == Icon_faces) {
if(SS.GW.showFaces) {
strcpy(str, "Don't make faces selectable with mouse");
str = "Don't make faces selectable with mouse";
} else {
strcpy(str, "Make faces selectable with mouse");
str = "Make faces selectable with mouse";
}
} else {
sprintf(str, "%s %s", *(tooltippedIcon->var) ? "Hide" : "Show",
str = ssprintf("%s %s", *(tooltippedIcon->var) ? "Hide" : "Show",
tooltippedIcon->tip);
}
double ox = oldMousePos.x, oy = oldMousePos.y - LINE_HEIGHT;
ox += 3;
oy -= 3;
int tw = ((int)strlen(str) + 1)*CHAR_WIDTH;
int tw = (str.length() + 1)*CHAR_WIDTH;
ox = min(ox, (double) (width - 25) - tw);
oy = max(oy, 5.0);
@ -465,7 +462,7 @@ void TextWindow::DrawOrHitTestIcons(int how, double mx, double my)
ssglAxisAlignedLineLoop(ox, ox+tw, oy, oy+LINE_HEIGHT);
glColor4d(0, 0, 0, 1);
ssglBitmapText(str, Vector::From(ox+5, oy-3+LINE_HEIGHT, 0));
ssglBitmapText(str.c_str(), Vector::From(ox+5, oy-3+LINE_HEIGHT, 0));
} else {
if(!hoveredIcon ||
(hoveredIcon != tooltippedIcon))
@ -552,10 +549,8 @@ uint8_t *TextWindow::HsvPattern1d(double h, double s) {
}
void TextWindow::ColorPickerDone(void) {
char str[1024];
RgbaColor rgb = editControl.colorPicker.rgb;
sprintf(str, "%.2f, %.2f, %.3f", rgb.redF(), rgb.greenF(), rgb.blueF());
EditControlDone(str);
EditControlDone(ssprintf("%.2f, %.2f, %.3f", rgb.redF(), rgb.greenF(), rgb.blueF()).c_str());
}
bool TextWindow::DrawOrHitTestColorPicker(int how, bool leftDown,

View File

@ -206,21 +206,20 @@ bool GraphicsWindow::ToolbarDrawOrHitTest(int mx, int my,
// Do this last so that nothing can draw over it.
if(toolTip.show) {
ssglInitializeBitmapFont();
char str[1024];
if(strlen(toolTip.str) >= 200) oops();
strcpy(str, toolTip.str);
std::string str { toolTip.str };
for(i = 0; SS.GW.menu[i].level >= 0; i++) {
if(toolbarTooltipped == SS.GW.menu[i].id) {
char accelbuf[40];
if(MakeAcceleratorLabel(SS.GW.menu[i].accel, accelbuf)) {
sprintf(str+strlen(str), " (%s)", accelbuf);
str += ssprintf(" (%s)", accelbuf);
}
break;
}
}
int tw = (int)strlen(str)*SS.TW.CHAR_WIDTH + 10,
int tw = str.length() * SS.TW.CHAR_WIDTH + 10,
th = SS.TW.LINE_HEIGHT + 2;
double ox = toolbarMouseX + 3, oy = toolbarMouseY + 3;
@ -234,7 +233,7 @@ bool GraphicsWindow::ToolbarDrawOrHitTest(int mx, int my,
glPushMatrix();
glTranslated(ox+5, oy+3, 0);
glScaled(1, -1, 1);
ssglBitmapText(str, Vector::From(0, 0, 0));
ssglBitmapText(str.c_str(), Vector::From(0, 0, 0));
glPopMatrix();
}
ssglDepthRangeLockToFront(false);

View File

@ -97,10 +97,8 @@ bool TextWindow::EditControlDoneForView(const char *s) {
SS.GW.projRight = pt;
SS.GW.NormalizeProjectionVectors();
edit.meaning = EDIT_VIEW_PROJ_UP;
char buf[1024];
sprintf(buf, "%.3f, %.3f, %.3f", CO(SS.GW.projUp));
HideEditControl();
ShowEditControl(26, 10, buf);
ShowEditControl(26, 10, ssprintf("%.3f, %.3f, %.3f", CO(SS.GW.projUp)));
edit.showAgain = true;
} else {
SS.GW.projUp = pt;

View File

@ -1183,13 +1183,13 @@ HMENU CreateGraphicsWindowMenus(void)
int subMenu = 0;
for(i = 0; SS.GW.menu[i].level >= 0; i++) {
char label[100] = { '\0' };
std::string label;
if(SS.GW.menu[i].label) {
char accelbuf[40];
const char *sep =
MakeAcceleratorLabel(SS.GW.menu[i].accel, accelbuf) ?
"\t" : "";
sprintf(label, "%s%s%s", SS.GW.menu[i].label, sep, accelbuf);
label = ssprintf("%s%s%s", SS.GW.menu[i].label, sep, accelbuf);
}
if(SS.GW.menu[i].level == 0) {