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) { void TextWindow::ScreenChangePasteTransformed(int link, uint32_t v) {
char str[300];
switch(link) { switch(link) {
case 't': case 't':
sprintf(str, "%d", SS.TW.shown.paste.times); SS.TW.ShowEditControl(10, 13, ssprintf("%d", SS.TW.shown.paste.times));
SS.TW.ShowEditControl(10, 13, str);
SS.TW.edit.meaning = EDIT_PASTE_TIMES_REPEATED; SS.TW.edit.meaning = EDIT_PASTE_TIMES_REPEATED;
break; break;
case 'r': case 'r':
sprintf(str, "%.3f", SS.TW.shown.paste.theta*180/PI); SS.TW.ShowEditControl(12, 13, ssprintf("%.3f", SS.TW.shown.paste.theta*180/PI));
SS.TW.ShowEditControl(12, 13, str);
SS.TW.edit.meaning = EDIT_PASTE_ANGLE; SS.TW.edit.meaning = EDIT_PASTE_ANGLE;
break; break;
case 's': case 's':
sprintf(str, "%.3f", SS.TW.shown.paste.scale); SS.TW.ShowEditControl(18, 13, ssprintf("%.3f", SS.TW.shown.paste.scale));
SS.TW.ShowEditControl(18, 13, str);
SS.TW.edit.meaning = EDIT_PASTE_SCALE; SS.TW.edit.meaning = EDIT_PASTE_SCALE;
break; break;
} }

View File

@ -7,17 +7,13 @@
#include "solvespace.h" #include "solvespace.h"
void TextWindow::ScreenChangeLightDirection(int link, uint32_t v) { void TextWindow::ScreenChangeLightDirection(int link, uint32_t v) {
char str[1024]; SS.TW.ShowEditControl(29+2*v, 8, ssprintf("%.2f, %.2f, %.2f", CO(SS.lightDir[v])));
sprintf(str, "%.2f, %.2f, %.2f", CO(SS.lightDir[v]));
SS.TW.ShowEditControl(29+2*v, 8, str);
SS.TW.edit.meaning = EDIT_LIGHT_DIRECTION; SS.TW.edit.meaning = EDIT_LIGHT_DIRECTION;
SS.TW.edit.i = v; SS.TW.edit.i = v;
} }
void TextWindow::ScreenChangeLightIntensity(int link, uint32_t v) { void TextWindow::ScreenChangeLightIntensity(int link, uint32_t v) {
char str[1024]; SS.TW.ShowEditControl(29+2*v, 31, ssprintf("%.2f", SS.lightIntensity[v]));
sprintf(str, "%.2f", SS.lightIntensity[v]);
SS.TW.ShowEditControl(29+2*v, 31, str);
SS.TW.edit.meaning = EDIT_LIGHT_INTENSITY; SS.TW.edit.meaning = EDIT_LIGHT_INTENSITY;
SS.TW.edit.i = v; 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) { void TextWindow::ScreenChangeChordTolerance(int link, uint32_t v) {
char str[1024]; SS.TW.ShowEditControl(37, 3, ssprintf("%.2f", SS.chordTol));
sprintf(str, "%.2f", SS.chordTol);
SS.TW.ShowEditControl(37, 3, str);
SS.TW.edit.meaning = EDIT_CHORD_TOLERANCE; SS.TW.edit.meaning = EDIT_CHORD_TOLERANCE;
} }
void TextWindow::ScreenChangeMaxSegments(int link, uint32_t v) { void TextWindow::ScreenChangeMaxSegments(int link, uint32_t v) {
char str[1024]; SS.TW.ShowEditControl(41, 3, ssprintf("%d", SS.maxSegments));
sprintf(str, "%d", SS.maxSegments);
SS.TW.ShowEditControl(41, 3, str);
SS.TW.edit.meaning = EDIT_MAX_SEGMENTS; SS.TW.edit.meaning = EDIT_MAX_SEGMENTS;
} }
void TextWindow::ScreenChangeCameraTangent(int link, uint32_t v) { void TextWindow::ScreenChangeCameraTangent(int link, uint32_t v) {
char str[1024]; SS.TW.ShowEditControl(47, 3, ssprintf("%.3f", 1000*SS.cameraTangent));
sprintf(str, "%.3f", 1000*SS.cameraTangent);
SS.TW.ShowEditControl(47, 3, str);
SS.TW.edit.meaning = EDIT_CAMERA_TANGENT; 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) { void TextWindow::ScreenChangeDigitsAfterDecimal(int link, uint32_t v) {
char buf[128]; SS.TW.ShowEditControl(55, 3, ssprintf("%d", SS.UnitDigitsAfterDecimal()));
sprintf(buf, "%d", SS.UnitDigitsAfterDecimal());
SS.TW.ShowEditControl(55, 3, buf);
SS.TW.edit.meaning = EDIT_DIGITS_AFTER_DECIMAL; SS.TW.edit.meaning = EDIT_DIGITS_AFTER_DECIMAL;
} }
void TextWindow::ScreenChangeExportScale(int link, uint32_t v) { void TextWindow::ScreenChangeExportScale(int link, uint32_t v) {
char str[1024]; SS.TW.ShowEditControl(61, 5, ssprintf("%.3f", (double)SS.exportScale));
sprintf(str, "%.3f", (double)SS.exportScale);
SS.TW.ShowEditControl(61, 5, str);
SS.TW.edit.meaning = EDIT_EXPORT_SCALE; 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, void SvgFileWriter::FinishPath(RgbaColor strokeRgb, double lineWidth,
bool filled, RgbaColor fillRgb) bool filled, RgbaColor fillRgb)
{ {
char fill[100]; std::string fill;
if(filled) { if(filled) {
sprintf(fill, "#%02x%02x%02x", fill = ssprintf("#%02x%02x%02x",
fillRgb.red, fillRgb.green, fillRgb.blue); fillRgb.red, fillRgb.green, fillRgb.blue);
} else { } else {
strcpy(fill, "none"); fill = "none";
} }
fprintf(f, "' stroke-width='%.3f' stroke='#%02x%02x%02x' " fprintf(f, "' stroke-width='%.3f' stroke='#%02x%02x%02x' "
"stroke-linecap='round' stroke-linejoin='round' " "stroke-linecap='round' stroke-linejoin='round' "
"fill='%s' />\r\n", "fill='%s' />\r\n",
lineWidth, strokeRgb.red, strokeRgb.green, strokeRgb.blue, lineWidth, strokeRgb.red, strokeRgb.green, strokeRgb.blue,
fill); fill.c_str());
} }
void SvgFileWriter::MaybeMoveTo(Vector st, Vector fi) { void SvgFileWriter::MaybeMoveTo(Vector st, Vector fi) {

View File

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

View File

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

View File

@ -417,10 +417,8 @@ err:
} }
void TextWindow::ScreenChangeBackgroundImageScale(int link, uint32_t v) { 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.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) { 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) { void TextWindow::ScreenOpacity(int link, uint32_t v) {
Group *g = SK.GetGroup(SS.TW.shown.group); Group *g = SK.GetGroup(SS.TW.shown.group);
char str[1024]; SS.TW.ShowEditControl(22, 11, ssprintf("%.2f", g->color.alphaF()));
sprintf(str, "%.2f", g->color.alphaF());
SS.TW.ShowEditControl(22, 11, str);
SS.TW.edit.meaning = EDIT_GROUP_OPACITY; SS.TW.edit.meaning = EDIT_GROUP_OPACITY;
SS.TW.edit.group.v = g->h.v; 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. // There's an extra line for the skipFirst parameter in one-sided groups.
int r = (g->subtype == Group::ONE_SIDED) ? 16 : 14; int r = (g->subtype == Group::ONE_SIDED) ? 16 : 14;
char str[1024]; SS.TW.ShowEditControl(r, 10, ssprintf("%d", (int)g->valA));
sprintf(str, "%d", (int)g->valA);
SS.TW.ShowEditControl(r, 10, str);
SS.TW.edit.meaning = EDIT_TIMES_REPEATED; SS.TW.edit.meaning = EDIT_TIMES_REPEATED;
SS.TW.edit.group.v = v; 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) 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(); SS.ScheduleShowTW();
editControl.colorPicker.show = true; editControl.colorPicker.show = true;
@ -101,7 +98,7 @@ void TextWindow::ShowEditControlWithColorPicker(int halfRow, int col, RgbaColor
editControl.colorPicker.h = 0; editControl.colorPicker.h = 0;
editControl.colorPicker.s = 0; editControl.colorPicker.s = 0;
editControl.colorPicker.v = 1; 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) { void TextWindow::ClearScreen(void) {
@ -437,23 +434,23 @@ void TextWindow::DrawOrHitTestIcons(int how, double mx, double my)
if(tooltippedIcon) { if(tooltippedIcon) {
if(how == PAINT) { if(how == PAINT) {
char str[1024]; std::string str;
if(tooltippedIcon->icon == Icon_faces) { if(tooltippedIcon->icon == Icon_faces) {
if(SS.GW.showFaces) { if(SS.GW.showFaces) {
strcpy(str, "Don't make faces selectable with mouse"); str = "Don't make faces selectable with mouse";
} else { } else {
strcpy(str, "Make faces selectable with mouse"); str = "Make faces selectable with mouse";
} }
} else { } else {
sprintf(str, "%s %s", *(tooltippedIcon->var) ? "Hide" : "Show", str = ssprintf("%s %s", *(tooltippedIcon->var) ? "Hide" : "Show",
tooltippedIcon->tip); tooltippedIcon->tip);
} }
double ox = oldMousePos.x, oy = oldMousePos.y - LINE_HEIGHT; double ox = oldMousePos.x, oy = oldMousePos.y - LINE_HEIGHT;
ox += 3; ox += 3;
oy -= 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); ox = min(ox, (double) (width - 25) - tw);
oy = max(oy, 5.0); 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); ssglAxisAlignedLineLoop(ox, ox+tw, oy, oy+LINE_HEIGHT);
glColor4d(0, 0, 0, 1); 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 { } else {
if(!hoveredIcon || if(!hoveredIcon ||
(hoveredIcon != tooltippedIcon)) (hoveredIcon != tooltippedIcon))
@ -552,10 +549,8 @@ uint8_t *TextWindow::HsvPattern1d(double h, double s) {
} }
void TextWindow::ColorPickerDone(void) { void TextWindow::ColorPickerDone(void) {
char str[1024];
RgbaColor rgb = editControl.colorPicker.rgb; RgbaColor rgb = editControl.colorPicker.rgb;
sprintf(str, "%.2f, %.2f, %.3f", rgb.redF(), rgb.greenF(), rgb.blueF()); EditControlDone(ssprintf("%.2f, %.2f, %.3f", rgb.redF(), rgb.greenF(), rgb.blueF()).c_str());
EditControlDone(str);
} }
bool TextWindow::DrawOrHitTestColorPicker(int how, bool leftDown, 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. // Do this last so that nothing can draw over it.
if(toolTip.show) { if(toolTip.show) {
ssglInitializeBitmapFont(); ssglInitializeBitmapFont();
char str[1024];
if(strlen(toolTip.str) >= 200) oops(); 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++) { for(i = 0; SS.GW.menu[i].level >= 0; i++) {
if(toolbarTooltipped == SS.GW.menu[i].id) { if(toolbarTooltipped == SS.GW.menu[i].id) {
char accelbuf[40]; char accelbuf[40];
if(MakeAcceleratorLabel(SS.GW.menu[i].accel, accelbuf)) { if(MakeAcceleratorLabel(SS.GW.menu[i].accel, accelbuf)) {
sprintf(str+strlen(str), " (%s)", accelbuf); str += ssprintf(" (%s)", accelbuf);
} }
break; 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; th = SS.TW.LINE_HEIGHT + 2;
double ox = toolbarMouseX + 3, oy = toolbarMouseY + 3; double ox = toolbarMouseX + 3, oy = toolbarMouseY + 3;
@ -234,7 +233,7 @@ bool GraphicsWindow::ToolbarDrawOrHitTest(int mx, int my,
glPushMatrix(); glPushMatrix();
glTranslated(ox+5, oy+3, 0); glTranslated(ox+5, oy+3, 0);
glScaled(1, -1, 1); glScaled(1, -1, 1);
ssglBitmapText(str, Vector::From(0, 0, 0)); ssglBitmapText(str.c_str(), Vector::From(0, 0, 0));
glPopMatrix(); glPopMatrix();
} }
ssglDepthRangeLockToFront(false); ssglDepthRangeLockToFront(false);

View File

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

View File

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