Skip to content

Commit 8fd365f

Browse files
committed
Read font info files incrementally
The previous text-mode reading had a problem where the extractable character count of a file opened in non-binary (text) mode is subject to the actual contents of the file due to newline translation and EOF characters, and can only be determined by actually scanning over the content. GetLen yields the binary size of the file and thus tends to overallocate the buffer, leading to the actual read attempting to read more data than can actually be extracted. This puts the code at the mercy of what garbage the target string contains from the allocator and how much data could actually be read.
1 parent 0669d70 commit 8fd365f

1 file changed

Lines changed: 17 additions & 23 deletions

File tree

engine/render/r_font.cpp

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@
66

77
#include "r_local.h"
88

9+
#include <fstream>
10+
#include <string>
11+
912
// =======
1013
// Classes
1114
// =======
1215

1316
// Glyph parameters
1417
struct f_glyph_s {
15-
double tcLeft;
16-
double tcRight;
17-
double tcTop;
18-
double tcBottom;
19-
int width;
20-
int spLeft;
21-
int spRight;
18+
double tcLeft = 0.0;
19+
double tcRight = 0.0;
20+
double tcTop = 0.0;
21+
double tcBottom = 0.0;
22+
int width = 0;
23+
int spLeft = 0;
24+
int spRight = 0;
2225
};
2326

2427
// Font height info
@@ -45,27 +48,20 @@ r_font_c::r_font_c(r_renderer_c* renderer, const char* fontName)
4548
// Open info file
4649
char tgfName[260];
4750
sprintf_s(tgfName, 260, "%s.tgf", fileNameBase);
48-
fileInputStream_c tgf;
49-
if (tgf.FileOpen(tgfName, false)) {
51+
std::ifstream tgf(tgfName);
52+
if (!tgf) {
5053
renderer->sys->con->Warning("font \"%s\" not found", fontName);
5154
return;
5255
}
5356

54-
// Read contents of info file
55-
size_t tgfLen = tgf.GetLen();
56-
char* tgfDat = AllocStringLen(tgfLen);
57-
tgf.Read(tgfDat, tgfLen);
58-
tgf.FileClose();
59-
6057
maxHeight = 0;
6158
f_fontHeight_s* fh = NULL;
6259

6360
// Parse info file
64-
char* tk_context;
65-
char* sub = strtok_s(tgfDat, "\n", &tk_context);
66-
do {
61+
std::string sub;
62+
while (std::getline(tgf, sub)) {
6763
int h, x, y, w, sl, sr;
68-
if (sscanf_s(sub, "HEIGHT %u;", &h) == 1) {
64+
if (sscanf_s(sub.c_str(), "HEIGHT %u;", &h) == 1) {
6965
// New height
7066
fh = new f_fontHeight_s;
7167
fontHeights[numFontHeight++] = fh;
@@ -77,7 +73,7 @@ r_font_c::r_font_c(r_renderer_c* renderer, const char* fontName)
7773
maxHeight = h;
7874
}
7975
fh->numGlyph = 0;
80-
} else if (fh && sscanf_s(sub, "GLYPH %u %u %u %d %d;", &x, &y, &w, &sl, &sr) == 5) {
76+
} else if (fh && sscanf_s(sub.c_str(), "GLYPH %u %u %u %d %d;", &x, &y, &w, &sl, &sr) == 5) {
8177
// Add glyph
8278
if (fh->numGlyph >= 128) continue;
8379
f_glyph_s* glyph = &fh->glyphs[fh->numGlyph++];
@@ -89,9 +85,7 @@ r_font_c::r_font_c(r_renderer_c* renderer, const char* fontName)
8985
glyph->spLeft = sl;
9086
glyph->spRight = sr;
9187
}
92-
} while (sub = strtok_s(NULL, "\n", &tk_context));
93-
94-
FreeString(tgfDat);
88+
}
9589

9690
// Generate mapping of text height to font height
9791
fontHeightMap = new int[maxHeight + 1];

0 commit comments

Comments
 (0)