Skip to content

Commit 7fa5299

Browse files
Eolien55aligrudi
authored andcommitted
pdf: replace long by long long
On some platforms (eg, GCC x86), long is 32 bits. This is a problem in pdfpos00, because calculations such as `uh * 1000 * 72` (in pdfunit) are likely to overlow, causing important bugs. This is especially the case when using high DPI values, such as 720000.
1 parent 9e4232b commit 7fa5299

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

pdf.c

+17-17
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ static void pfont_write(struct pfont *ps)
163163
pdfout(" /LastChar %d\n", ps->gend % 256);
164164
pdfout(" /Widths [");
165165
for (i = ps->gbeg; i <= ps->gend; i++)
166-
pdfout(" %d", (long) font_glget(fn, i)->wid * 100 * 72 / dev_res);
166+
pdfout(" %d", (long long) font_glget(fn, i)->wid * 100 * 72 / dev_res);
167167
pdfout(" ]\n");
168168
pdfout(" /FontDescriptor %d 0 R\n", ps->des);
169169
pdfout(" /Encoding %d 0 R\n", enc_obj);
@@ -205,7 +205,7 @@ static void pfont_writecid(struct pfont *ps)
205205
gcnt++;
206206
pdfout(" /W [ %d [", ps->gbeg);
207207
for (i = ps->gbeg; i <= ps->gend; i++)
208-
pdfout(" %d", (long) font_glget(fn, i)->wid * 100 * 72 / dev_res);
208+
pdfout(" %d", (long long) font_glget(fn, i)->wid * 100 * 72 / dev_res);
209209
pdfout(" ] ]\n");
210210
pdfout(">>\n");
211211
obj_end();
@@ -358,8 +358,8 @@ static int o_loadfont(struct glyph *g)
358358
static char *pdfpos00(int uh, int uv)
359359
{
360360
static char buf[64];
361-
int h = (long) uh * 72 / dev_res;
362-
int v = (long) pdf_height * 100 - (long) uv * 72 / dev_res;
361+
int h = (long long) uh * 72 / dev_res;
362+
int v = (long long) pdf_height * 100 - (long long) uv * 72 / dev_res;
363363
sprintf(buf, "%s%d.%02d %s%d.%02d",
364364
h < 0 ? "-" : "", abs(h) / 100, abs(h) % 100,
365365
v < 0 ? "-" : "", abs(v) / 100, abs(v) % 100);
@@ -376,7 +376,7 @@ static char *pdfpos(int uh, int uv)
376376
static char *pdfunit(int uh, int sz)
377377
{
378378
static char buf[64];
379-
int h = (long) uh * 1000 * 72 / sz / dev_res;
379+
int h = (long long) uh * 1000 * 72 / sz / dev_res;
380380
sprintf(buf, "%s%d", h < 0 ? "-" : "", abs(h));
381381
return buf;
382382
}
@@ -664,9 +664,9 @@ static int pdfext(char *pdf, int len, int hwid, int vwid)
664664
bbox = pdf_dval_val(pdf, len, pages, "/MediaBox");
665665
if (bbox >= 0 && !pdfbbox100(pdf, len, bbox, dim)) {
666666
if (hwid > 0)
667-
hzoom = (long) hwid * (100 * 7200 / dev_res) / (dim[2] - dim[0]);
667+
hzoom = (long long) hwid * (100 * 7200 / dev_res) / (dim[2] - dim[0]);
668668
if (vwid > 0)
669-
vzoom = (long) vwid * (100 * 7200 / dev_res) / (dim[3] - dim[1]);
669+
vzoom = (long long) vwid * (100 * 7200 / dev_res) / (dim[3] - dim[1]);
670670
if (vwid <= 0)
671671
vzoom = hzoom;
672672
if (hwid <= 0)
@@ -915,16 +915,16 @@ void drawl(int h, int v)
915915
/* draw circle/ellipse quadrant */
916916
static void drawquad(int ch, int cv)
917917
{
918-
long b = 551915;
919-
long x0 = o_h * 1000;
920-
long y0 = o_v * 1000;
921-
long x3 = x0 + ch * 1000 / 2;
922-
long y3 = y0 + cv * 1000 / 2;
923-
long x1 = x0;
924-
long y1 = y0 + cv * b / 1000 / 2;
925-
long x2 = x3 - ch * b / 1000 / 2;
926-
long y2 = y3;
927-
if (ch * cv < 0) {
918+
long long b = 551915;
919+
long long x0 = o_h * 1000;
920+
long long y0 = o_v * 1000;
921+
long long x3 = x0 + ch * 1000 / 2;
922+
long long y3 = y0 + cv * 1000 / 2;
923+
long long x1 = x0;
924+
long long y1 = y0 + cv * b / 1000 / 2;
925+
long long x2 = x3 - ch * b / 1000 / 2;
926+
long long y2 = y3;
927+
if ((ch > 0 && cv < 0) || (ch < 0 && cv > 0)) {
928928
x1 = x0 + ch * b / 1000 / 2;
929929
y1 = y0;
930930
x2 = x3;

0 commit comments

Comments
 (0)