From a72575d04e254c984e070909d6fa90dac4b49f61 Mon Sep 17 00:00:00 2001 From: Daniel Richard G Date: Mon, 26 Aug 2013 16:09:15 -0400 Subject: [PATCH] Use casts to bridge mismatches in integer-type sizes and signedness The compiler gets nervous when we (for example) pass in a size_t as an int parameter, or assign an int to a char, or assign -1 to an unsigned type. By adding appropriate casts, we inform the compiler that, yes, we know what we're doing. This change also upgrades a va_arg() type from char to int, as char is always promoted to int when passed through '...'. --- export.cpp | 2 +- exportvector.cpp | 24 ++++++++++++------------ file.cpp | 6 +++--- groupmesh.cpp | 2 +- mouse.cpp | 2 +- srf/boolean.cpp | 2 +- srf/surfinter.cpp | 2 +- style.cpp | 8 ++++---- textwin.cpp | 5 +++-- toolbar.cpp | 2 +- ttf.cpp | 36 ++++++++++++++++++------------------ util.cpp | 8 ++++---- win32/freeze.cpp | 2 +- win32/w32main.cpp | 8 ++++---- 14 files changed, 55 insertions(+), 54 deletions(-) diff --git a/export.cpp b/export.cpp index 6a41422..bcc9565 100644 --- a/export.cpp +++ b/export.cpp @@ -503,7 +503,7 @@ void VectorFileWriter::Output(SBezierLoopSetSet *sblss, SMesh *sm) { b = sbl->l.First(); if(!b || !Style::Exportable(b->auxA)) continue; - hStyle hs = { b->auxA }; + hStyle hs = { (DWORD)b->auxA }; Style *stl = Style::Get(hs); double lineWidth = Style::WidthMm(b->auxA)*s; DWORD strokeRgb = Style::Color(hs, true); diff --git a/exportvector.cpp b/exportvector.cpp index 15d9254..4f7d74f 100644 --- a/exportvector.cpp +++ b/exportvector.cpp @@ -277,7 +277,7 @@ void PdfFileWriter::StartFile(void) { "%%%c%c%c%c\r\n", 0xe2, 0xe3, 0xcf, 0xd3); - xref[1] = ftell(f); + xref[1] = (DWORD)ftell(f); fprintf(f, "1 0 obj\r\n" " << /Type /Catalog\r\n" @@ -286,7 +286,7 @@ void PdfFileWriter::StartFile(void) { " >>\r\n" "endobj\r\n"); - xref[2] = ftell(f); + xref[2] = (DWORD)ftell(f); fprintf(f, "2 0 obj\r\n" " << /Type /Outlines\r\n" @@ -294,7 +294,7 @@ void PdfFileWriter::StartFile(void) { " >>\r\n" "endobj\r\n"); - xref[3] = ftell(f); + xref[3] = (DWORD)ftell(f); fprintf(f, "3 0 obj\r\n" " << /Type /Pages\r\n" @@ -303,7 +303,7 @@ void PdfFileWriter::StartFile(void) { " >>\r\n" "endobj\r\n"); - xref[4] = ftell(f); + xref[4] = (DWORD)ftell(f); fprintf(f, "4 0 obj\r\n" " << /Type /Page\r\n" @@ -318,35 +318,35 @@ void PdfFileWriter::StartFile(void) { MmToPts(ptMax.x - ptMin.x), MmToPts(ptMax.y - ptMin.y)); - xref[5] = ftell(f); + xref[5] = (DWORD)ftell(f); fprintf(f, "5 0 obj\r\n" " << /Length 6 0 R >>\r\n" "stream\r\n"); - bodyStart = ftell(f); + bodyStart = (DWORD)ftell(f); } void PdfFileWriter::FinishAndCloseFile(void) { - DWORD bodyEnd = ftell(f); + DWORD bodyEnd = (DWORD)ftell(f); fprintf(f, "endstream\r\n" "endobj\r\n"); - xref[6] = ftell(f); + xref[6] = (DWORD)ftell(f); fprintf(f, "6 0 obj\r\n" " %d\r\n" "endobj\r\n", bodyEnd - bodyStart); - xref[7] = ftell(f); + xref[7] = (DWORD)ftell(f); fprintf(f, "7 0 obj\r\n" " [/PDF /Text]\r\n" "endobj\r\n"); - xref[8] = ftell(f); + xref[8] = (DWORD)ftell(f); fprintf(f, "8 0 obj\r\n" " << /Type /Font\r\n" @@ -357,13 +357,13 @@ void PdfFileWriter::FinishAndCloseFile(void) { " >>\r\n" "endobj\r\n"); - xref[9] = ftell(f); + xref[9] = (DWORD)ftell(f); fprintf(f, "9 0 obj\r\n" " << /Creator (SolveSpace)\r\n" " >>\r\n"); - DWORD xrefStart = ftell(f); + DWORD xrefStart = (DWORD)ftell(f); fprintf(f, "xref\r\n" "0 10\r\n" diff --git a/file.cpp b/file.cpp index c168756..7ee35e2 100644 --- a/file.cpp +++ b/file.cpp @@ -382,7 +382,7 @@ void SolveSpace::LoadUsingTable(char *key, char *val) { for(;;) { EntityMap em; char line2[1024]; - fgets(line2, sizeof(line2), fh); + fgets(line2, (int)sizeof(line2), fh); if(sscanf(line2, "%d %x %d", &(em.h.v), &(em.input.v), &(em.copyNumber)) == 3) { @@ -420,7 +420,7 @@ bool SolveSpace::LoadFromFile(char *filename) { sv.g.scale = 1; // default is 1, not 0; so legacy files need this char line[1024]; - while(fgets(line, sizeof(line), fh)) { + while(fgets(line, (int)sizeof(line), fh)) { char *s = strchr(line, '\n'); if(s) *s = '\0'; // We should never get files with \r characters in them, but mailers @@ -502,7 +502,7 @@ bool SolveSpace::LoadEntitiesFromFile(char *file, EntityList *le, memset(&sv, 0, sizeof(sv)); char line[1024]; - while(fgets(line, sizeof(line), fh)) { + while(fgets(line, (int)sizeof(line), fh)) { char *s = strchr(line, '\n'); if(s) *s = '\0'; // We should never get files with \r characters in them, but mailers diff --git a/groupmesh.cpp b/groupmesh.cpp index c485039..d89918b 100644 --- a/groupmesh.cpp +++ b/groupmesh.cpp @@ -545,7 +545,7 @@ void Group::DrawFilledPaths(void) { // In an assembled loop, all the styles should be the same; so doesn't // matter which one we grab. SBezier *sb = &(sbls->l.elem[0].l.elem[0]); - hStyle hs = { sb->auxA }; + hStyle hs = { (DWORD)sb->auxA }; Style *s = Style::Get(hs); if(s->filled) { // This is a filled loop, where the user specified a fill color. diff --git a/mouse.cpp b/mouse.cpp index ba9f398..ed7fbd5 100644 --- a/mouse.cpp +++ b/mouse.cpp @@ -990,7 +990,7 @@ void GraphicsWindow::MouseLeftDown(double mx, double my) { } Entity e; - if(r->extraPoints >= arraylen(e.point) - 4) { + if(r->extraPoints >= (int)arraylen(e.point) - 4) { ClearPending(); break; } diff --git a/srf/boolean.cpp b/srf/boolean.cpp index d7beee8..7167958 100644 --- a/srf/boolean.cpp +++ b/srf/boolean.cpp @@ -171,7 +171,7 @@ void SSurface::TrimFromEdgeList(SEdgeList *el, bool asUv) { merged = false; for(se = el->l.First(); se; se = el->l.NextAfter(se)) { if(se->tag) continue; - if(se->auxA != stb.curve.v) continue; + if(se->auxA != (int)stb.curve.v) continue; if(( se->auxB && !stb.backwards) || (!se->auxB && stb.backwards)) continue; diff --git a/srf/surfinter.cpp b/srf/surfinter.cpp index 0c0a4c0..a2681df 100644 --- a/srf/surfinter.cpp +++ b/srf/surfinter.cpp @@ -334,7 +334,7 @@ void SSurface::IntersectAgainst(SSurface *b, SShell *agnstA, SShell *agnstB, if(lsi.n == 0) continue; // Find the other surface that this curve trims. - hSCurve hsc = { se->auxA }; + hSCurve hsc = { (DWORD)se->auxA }; SCurve *sc = shA->curve.FindById(hsc); hSSurface hother = (sc->surfA.v == srfA->h.v) ? sc->surfB : sc->surfA; diff --git a/style.cpp b/style.cpp index 27033e5..65ac5bf 100644 --- a/style.cpp +++ b/style.cpp @@ -45,7 +45,7 @@ char *Style::CnfPrefixToName(const char *prefix) { if(isupper(prefix[i]) && i != 0) { name[j++] = '-'; } - name[j++] = tolower(prefix[i]); + name[j++] = (char)tolower(prefix[i]); i++; } name[j++] = '\0'; @@ -198,11 +198,11 @@ Style *Style::Get(hStyle h) { // hStyle or with the integer corresponding to that hStyle.v. //----------------------------------------------------------------------------- DWORD Style::Color(int s, bool forExport) { - hStyle hs = { s }; + hStyle hs = { (DWORD)s }; return Color(hs, forExport); } float Style::Width(int s) { - hStyle hs = { s }; + hStyle hs = { (DWORD)s }; return Width(hs); } @@ -290,7 +290,7 @@ double Style::TextHeight(hStyle hs) { // if it's both shown and exportable. //----------------------------------------------------------------------------- bool Style::Exportable(int si) { - hStyle hs = { si }; + hStyle hs = { (DWORD)si }; Style *s = Get(hs); return (s->exportable) && (s->visible); } diff --git a/textwin.cpp b/textwin.cpp index 4840152..8174ec6 100644 --- a/textwin.cpp +++ b/textwin.cpp @@ -181,7 +181,8 @@ void TextWindow::Printf(bool halfLine, const char *fmt, ...) { break; } case 'c': { - char v = va_arg(vl, char); + // 'char' is promoted to 'int' when passed through '...' + int v = va_arg(vl, int); if(v == 0) { strcpy(buf, ""); } else { @@ -436,7 +437,7 @@ void TextWindow::DrawOrHitTestIcons(int how, double mx, double my) double ox = oldMousePos.x, oy = oldMousePos.y - LINE_HEIGHT; ox += 3; oy -= 3; - int tw = (strlen(str) + 1)*CHAR_WIDTH; + int tw = ((int)strlen(str) + 1)*CHAR_WIDTH; ox = min(ox, (width - 25) - tw); oy = max(oy, 5); diff --git a/toolbar.cpp b/toolbar.cpp index 8f3ed25..f4b2983 100644 --- a/toolbar.cpp +++ b/toolbar.cpp @@ -227,7 +227,7 @@ bool GraphicsWindow::ToolbarDrawOrHitTest(int mx, int my, } } - int tw = strlen(str)*SS.TW.CHAR_WIDTH + 10, + int tw = (int)strlen(str)*SS.TW.CHAR_WIDTH + 10, th = SS.TW.LINE_HEIGHT + 2; double ox = toolbarMouseX + 3, oy = toolbarMouseY + 3; diff --git a/ttf.cpp b/ttf.cpp index 6f52afc..e6f9960 100644 --- a/ttf.cpp +++ b/ttf.cpp @@ -256,14 +256,14 @@ bool TtfFont::LoadFontFromFile(bool nameOnly) { // Now load the Table Directory; our goal in doing this will be to // find the addresses of the tables that we will need. - DWORD glyfAddr = -1, glyfLen; - DWORD cmapAddr = -1, cmapLen; - DWORD headAddr = -1, headLen; - DWORD locaAddr = -1, locaLen; - DWORD maxpAddr = -1, maxpLen; - DWORD nameAddr = -1, nameLen; - DWORD hmtxAddr = -1, hmtxLen; - DWORD hheaAddr = -1, hheaLen; + DWORD glyfAddr = (DWORD)-1, glyfLen; + DWORD cmapAddr = (DWORD)-1, cmapLen; + DWORD headAddr = (DWORD)-1, headLen; + DWORD locaAddr = (DWORD)-1, locaLen; + DWORD maxpAddr = (DWORD)-1, maxpLen; + DWORD nameAddr = (DWORD)-1, nameLen; + DWORD hmtxAddr = (DWORD)-1, hmtxLen; + DWORD hheaAddr = (DWORD)-1, hheaLen; for(i = 0; i < numTables; i++) { char tag[5] = "xxxx"; @@ -302,9 +302,9 @@ bool TtfFont::LoadFontFromFile(bool nameOnly) { } } - if(glyfAddr == -1 || cmapAddr == -1 || headAddr == -1 || - locaAddr == -1 || maxpAddr == -1 || hmtxAddr == -1 || - nameAddr == -1 || hheaAddr == -1) + if(glyfAddr == (DWORD)-1 || cmapAddr == (DWORD)-1 || headAddr == (DWORD)-1 || + locaAddr == (DWORD)-1 || maxpAddr == (DWORD)-1 || hmtxAddr == (DWORD)-1 || + nameAddr == (DWORD)-1 || hheaAddr == (DWORD)-1) { throw "missing table addr"; } @@ -343,7 +343,7 @@ bool TtfFont::LoadFontFromFile(bool nameOnly) { int c = 0; for(i = 0; i < displayNameLength; i++) { BYTE b = GetBYTE(); - if(b && c < (sizeof(name.str) - 2)) { + if(b && c < ((int)sizeof(name.str) - 2)) { name.str[c++] = b; } } @@ -449,7 +449,7 @@ bool TtfFont::LoadFontFromFile(bool nameOnly) { // glyphs. fseek(fh, cmapAddr, SEEK_SET); - DWORD usedTableAddr = -1; + DWORD usedTableAddr = (DWORD)-1; WORD cmapVersion = GetWORD(); WORD cmapTableCount = GetWORD(); @@ -464,7 +464,7 @@ bool TtfFont::LoadFontFromFile(bool nameOnly) { } } - if(usedTableAddr == -1) { + if(usedTableAddr == (DWORD)-1) { throw "no used table addr"; } @@ -504,14 +504,14 @@ bool TtfFont::LoadFontFromFile(bool nameOnly) { idDelta[i] = GetWORD(); } for(i = 0; i < segCount; i++) { - filePos[i] = ftell(fh); + filePos[i] = (DWORD)ftell(fh); idRangeOffset[i] = GetWORD(); } // So first, null out the glyph table in our in-memory representation // of the font; any character for which cmap does not provide a glyph // corresponds to -1 - for(i = 0; i < arraylen(useGlyph); i++) { + for(i = 0; i < (int)arraylen(useGlyph); i++) { useGlyph[i] = 0; } @@ -520,7 +520,7 @@ bool TtfFont::LoadFontFromFile(bool nameOnly) { if(idRangeOffset[i] == 0) { int j; for(j = startChar[i]; j <= endChar[i]; j++) { - if(j > 0 && j < arraylen(useGlyph)) { + if(j > 0 && j < (int)arraylen(useGlyph)) { // Don't create a reference to a glyph that we won't // store because it's bigger than the table. if((WORD)(j + v) < glyphs) { @@ -532,7 +532,7 @@ bool TtfFont::LoadFontFromFile(bool nameOnly) { } else { int j; for(j = startChar[i]; j <= endChar[i]; j++) { - if(j > 0 && j < arraylen(useGlyph)) { + if(j > 0 && j < (int)arraylen(useGlyph)) { int fp = filePos[i]; fp += (j - startChar[i])*sizeof(WORD); fp += idRangeOffset[i]; diff --git a/util.cpp b/util.cpp index bf65299..2b661c7 100644 --- a/util.cpp +++ b/util.cpp @@ -15,12 +15,12 @@ void MakePathRelative(const char *basep, char *pathp) // Convert everything to lowercase p = basep; for(i = 0; *p; p++) { - base[i++] = tolower(*p); + base[i++] = (char)tolower(*p); } base[i++] = '\0'; p = pathp; for(i = 0; *p; p++) { - path[i++] = tolower(*p); + path[i++] = (char)tolower(*p); } path[i++] = '\0'; @@ -71,7 +71,7 @@ void MakePathAbsolute(const char *basep, char *pathp) { // Chop off the filename int i; - for(i = strlen(out) - 1; i >= 0; i--) { + for(i = (int)strlen(out) - 1; i >= 0; i--) { if(out[i] == '\\' || out[i] == '/') break; } if(i < 0) return; // base is not an absolute path, or something? @@ -96,7 +96,7 @@ bool StringAllPrintable(const char *str) bool StringEndsIn(const char *str, const char *ending) { - int i, ls = strlen(str), le = strlen(ending); + int i, ls = (int)strlen(str), le = (int)strlen(ending); if(ls < le) return false; diff --git a/win32/freeze.cpp b/win32/freeze.cpp index 17042ea..b60c574 100644 --- a/win32/freeze.cpp +++ b/win32/freeze.cpp @@ -180,7 +180,7 @@ void FreezeStringF(const char *val, const char *subKey, const char *name) if(RegCreateKeyEx(software, subKey, 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &sub, NULL) != ERROR_SUCCESS) return; - if(RegSetValueEx(sub, name, 0, REG_SZ, (const BYTE *)val, strlen(val)+1) != ERROR_SUCCESS) + if(RegSetValueEx(sub, name, 0, REG_SZ, (const BYTE *)val, (DWORD)strlen(val)+1) != ERROR_SUCCESS) return; } diff --git a/win32/w32main.cpp b/win32/w32main.cpp index 6b5e597..1097a2a 100644 --- a/win32/w32main.cpp +++ b/win32/w32main.cpp @@ -478,7 +478,7 @@ LRESULT CALLBACK TextWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) (r.bottom - r.top), TRUE); // If the window is growing, then the scrollbar position may // be moving, so it's as if we're dragging the scrollbar. - HandleTextWindowScrollBar(-1, -1); + HandleTextWindowScrollBar((WPARAM)-1, -1); InvalidateRect(TextWnd, NULL, FALSE); break; } @@ -551,7 +551,7 @@ static BOOL ProcessKeyDown(WPARAM wParam) case VK_F9: case VK_F10: case VK_F11: - case VK_F12: c = (wParam - VK_F1) + 0xf1; break; + case VK_F12: c = ((int)wParam - VK_F1) + 0xf1; break; // These overlap with some character codes that I'm using, so // don't let them trigger by accident. @@ -563,7 +563,7 @@ static BOOL ProcessKeyDown(WPARAM wParam) case VK_RWIN: return FALSE; default: - c = wParam; + c = (int)wParam; break; } if(GetAsyncKeyState(VK_SHIFT) & 0x8000) c |= 0x100; @@ -890,7 +890,7 @@ void LoadAllFontFiles(void) ZERO(&tf); char fullPath[MAX_PATH]; - GetWindowsDirectory(fullPath, MAX_PATH - (30 + strlen(wfd.cFileName))); + GetWindowsDirectory(fullPath, MAX_PATH - (30 + (UINT)strlen(wfd.cFileName))); strcat(fullPath, "\\fonts\\"); strcat(fullPath, wfd.cFileName);