Skip to content

Commit 37e6ca1

Browse files
committed
Fixed write out of bounds for zero file size. Fixes #23.
1 parent 14845fc commit 37e6ca1

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/SConstruct

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
env = Environment(CFLAGS=['-Wall','-g'])
1+
env = Environment()
22
env.ParseConfig('pkg-config --cflags --libs pangocairo pangoft2')
3-
3+
env.Append(CFLAGS=['-Wall','-g'],
4+
LINKFLAGS=[#'-fsanitize=address', # Use for fsanitize
5+
]
6+
)
47
env.Program('paps',
58
['paps.c'],
6-
env['LIBS'])
9+
)
710

src/paps.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ static cairo_status_t paps_cairo_write_func(void *closure G_GNUC_UNUSED,
443443

444444
int main(int argc, char *argv[])
445445
{
446-
gboolean do_landscape = FALSE, do_rtl = FALSE, do_justify = FALSE, do_draw_header = FALSE;
446+
gboolean do_landscape = FALSE, do_rtl = FALSE, do_justify = FALSE, do_draw_header = FALSE, do_draw_footer=FALSE;
447447
gboolean do_stretch_chars = FALSE;
448448
gboolean do_use_markup = FALSE;
449449
gboolean do_show_wrap = FALSE; /* Whether to show wrap characters */
@@ -493,6 +493,8 @@ int main(int argc, char *argv[])
493493
N_("Set left margin. (Default: 36)"), "NUM"},
494494
{"header", 0, 0, G_OPTION_ARG_NONE, &do_draw_header,
495495
N_("Draw page header for each page."), NULL},
496+
{"footer", 0, 0, G_OPTION_ARG_NONE, &do_draw_footer,
497+
"Draw page footer for each page.", NULL},
496498
{"title", 0, 0, G_OPTION_ARG_STRING, &htitle,
497499
N_("Title string for page header (Default: filename/stdin)."), "TITLE"},
498500
{"markup", 0, 0, G_OPTION_ARG_NONE, &do_use_markup,
@@ -710,9 +712,9 @@ int main(int argc, char *argv[])
710712
page_layout.scale_x = 1.0L;
711713
page_layout.scale_y = 1.0L;
712714
if (do_draw_header)
713-
page_layout.header_sep = header_sep;
715+
page_layout.header_sep = 0; // header_sep;
714716
else
715-
page_layout.header_sep = 0;
717+
page_layout.header_sep = 0;
716718

717719
page_layout.column_height = (int)page_height
718720
- page_layout.top_margin
@@ -863,7 +865,7 @@ read_file (FILE *file,
863865
fclose (file);
864866

865867
/* Add a trailing new line if it is missing */
866-
if (inbuf->str[inbuf->len-1] != '\n')
868+
if (inbuf->len && inbuf->str[inbuf->len-1] != '\n')
867869
g_string_append(inbuf, "\n");
868870

869871
text = inbuf->str;
@@ -1181,7 +1183,7 @@ output_pages(cairo_surface_t *surface,
11811183
start_page(surface, cr, page_layout);
11821184

11831185
if (need_header)
1184-
draw_page_header_line_to_page(cr, FALSE, page_layout, pango_context, page_idx);
1186+
draw_page_header_line_to_page(cr, TRUE, page_layout, pango_context, page_idx);
11851187

11861188
while(pango_lines)
11871189
{
@@ -1204,7 +1206,7 @@ output_pages(cairo_surface_t *surface,
12041206
start_page(surface, cr, page_layout);
12051207

12061208
if (need_header)
1207-
draw_page_header_line_to_page(cr, FALSE, page_layout, pango_context, page_idx);
1209+
draw_page_header_line_to_page(cr, TRUE, page_layout, pango_context, page_idx);
12081210
}
12091211
else
12101212
{
@@ -1419,6 +1421,7 @@ draw_page_header_line_to_page(cairo_t *cr,
14191421
gdouble line_pos;
14201422

14211423
/* Reset gravity?? */
1424+
#if 0
14221425
header = g_strdup_printf("<span font_desc=\"%s\">%s</span>\n"
14231426
"<span font_desc=\"%s\">%s</span>\n"
14241427
"<span font_desc=\"%s\">%d</span>",
@@ -1428,6 +1431,11 @@ draw_page_header_line_to_page(cairo_t *cr,
14281431
get_date(date, 255),
14291432
page_layout->header_font_desc,
14301433
page);
1434+
#endif
1435+
header = g_strdup_printf("<span font_desc=\"%s\">%d</span>\n",
1436+
page_layout->header_font_desc,
1437+
page);
1438+
14311439
pango_layout_set_markup(layout, header, -1);
14321440
g_free(header);
14331441

@@ -1436,13 +1444,13 @@ draw_page_header_line_to_page(cairo_t *cr,
14361444
pango_layout_line_get_extents(line,
14371445
&ink_rect,
14381446
&logical_rect);
1439-
x_pos = page_layout->left_margin;
1447+
x_pos = page_layout->left_margin + (page_layout->page_width-page_layout->left_margin-page_layout->right_margin)*0.5 - 0.5*logical_rect.width/PANGO_SCALE;
14401448
height = logical_rect.height / PANGO_SCALE /3.0;
14411449

14421450
/* The header is placed right after the margin */
14431451
if (is_footer)
14441452
{
1445-
y_pos = page_layout->page_height - page_layout->bottom_margin;
1453+
y_pos = page_layout->page_height - page_layout->bottom_margin*0.5;
14461454
page_layout->footer_height = height;
14471455
}
14481456
else
@@ -1477,11 +1485,13 @@ draw_page_header_line_to_page(cairo_t *cr,
14771485
g_object_unref(layout);
14781486

14791487
/* header separator */
1488+
#if 0
14801489
line_pos = page_layout->top_margin + page_layout->header_height + page_layout->header_sep / 2;
14811490
cairo_move_to(cr, page_layout->left_margin, line_pos);
14821491
cairo_line_to(cr,page_layout->page_width - page_layout->right_margin, line_pos);
14831492
cairo_set_line_width(cr,0.1); // TBD
14841493
cairo_stroke(cr);
1494+
#endif
14851495

14861496
return logical_rect.height;
14871497
}

0 commit comments

Comments
 (0)