Skip to content

Commit a621f16

Browse files
authored
Fix up some cppcheck warnings (#168)
1 parent 14f2edf commit a621f16

1 file changed

Lines changed: 39 additions & 29 deletions

File tree

pdfgen.c

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,7 @@ struct pdf_object {
549549
int font_file_index; // index of stream object with embedded font
550550
} font_descriptor;
551551
struct {
552-
struct pdf_object *page; /* Page containing link */
553-
float llx; /* Clickable rectangle */
552+
float llx; /* Clickable rectangle */
554553
float lly;
555554
float urx;
556555
float ury;
@@ -741,7 +740,7 @@ static ssize_t dstr_ensure(struct dstr *str, size_t len)
741740
return 0;
742741
if (!str->data && len <= sizeof(str->static_data))
743742
str->alloc_len = len;
744-
else if (str->alloc_len < len) {
743+
else {
745744
size_t new_len;
746745

747746
new_len = len + 4096;
@@ -768,7 +767,7 @@ static ssize_t dstr_ensure(struct dstr *str, size_t len)
768767
// This breaks the PDF output, so we force a 'safe' locale.
769768
static void force_locale(char *buf, int len)
770769
{
771-
char *saved_locale = setlocale(LC_ALL, NULL);
770+
const char *saved_locale = setlocale(LC_ALL, NULL);
772771

773772
if (!saved_locale) {
774773
*buf = '\0';
@@ -780,7 +779,7 @@ static void force_locale(char *buf, int len)
780779
setlocale(LC_NUMERIC, "POSIX");
781780
}
782781

783-
static void restore_locale(char *buf)
782+
static void restore_locale(const char *buf)
784783
{
785784
setlocale(LC_ALL, buf);
786785
}
@@ -889,7 +888,7 @@ void pdf_clear_err(struct pdf_doc *pdf)
889888
pdf->errval = 0;
890889
}
891890

892-
static int pdf_get_errval(struct pdf_doc *pdf)
891+
static int pdf_get_errval(const struct pdf_doc *pdf)
893892
{
894893
if (!pdf)
895894
return 0;
@@ -1043,7 +1042,7 @@ struct pdf_doc *pdf_create(float width, float height,
10431042
time_t now = time(NULL);
10441043
struct tm tm;
10451044
#ifdef _WIN32
1046-
struct tm *tmp;
1045+
const struct tm *tmp;
10471046
tmp = localtime(&now);
10481047
tm = *tmp;
10491048
#else
@@ -1589,7 +1588,7 @@ static int pdf_save_object(struct pdf_doc *pdf, FILE *fp, int index)
15891588
break;
15901589
}
15911590
case OBJ_info: {
1592-
struct pdf_info *info = object->info;
1591+
const struct pdf_info *info = object->info;
15931592

15941593
fprintf(fp, "<<\r\n");
15951594
if (info->creator[0]) {
@@ -1627,7 +1626,8 @@ static int pdf_save_object(struct pdf_doc *pdf, FILE *fp, int index)
16271626
}
16281627

16291628
case OBJ_page: {
1630-
struct pdf_object *pages = pdf_find_first_object(pdf, OBJ_pages);
1629+
const struct pdf_object *pages =
1630+
pdf_find_first_object(pdf, OBJ_pages);
16311631
bool printed_xobjects = false;
16321632

16331633
fprintf(fp,
@@ -1669,8 +1669,9 @@ static int pdf_save_object(struct pdf_doc *pdf, FILE *fp, int index)
16691669

16701670
fprintf(fp, " /Contents [\r\n");
16711671
for (int i = 0; i < flexarray_size(&object->page.children); i++) {
1672-
struct pdf_object *child =
1673-
(struct pdf_object *)flexarray_get(&object->page.children, i);
1672+
const struct pdf_object *child =
1673+
(const struct pdf_object *)flexarray_get(
1674+
&object->page.children, i);
16741675
fprintf(fp, "%d 0 R\r\n", child->index);
16751676
}
16761677
fprintf(fp, "]\r\n");
@@ -1679,8 +1680,9 @@ static int pdf_save_object(struct pdf_doc *pdf, FILE *fp, int index)
16791680
fprintf(fp, " /Annots [\r\n");
16801681
for (int i = 0; i < flexarray_size(&object->page.annotations);
16811682
i++) {
1682-
struct pdf_object *child = (struct pdf_object *)flexarray_get(
1683-
&object->page.annotations, i);
1683+
const struct pdf_object *child =
1684+
(const struct pdf_object *)flexarray_get(
1685+
&object->page.annotations, i);
16841686
fprintf(fp, "%d 0 R\r\n", child->index);
16851687
}
16861688
fprintf(fp, "]\r\n");
@@ -1691,7 +1693,7 @@ static int pdf_save_object(struct pdf_doc *pdf, FILE *fp, int index)
16911693
}
16921694

16931695
case OBJ_bookmark: {
1694-
struct pdf_object *parent, *other;
1696+
const struct pdf_object *parent, *other;
16951697

16961698
parent = object->bookmark.parent;
16971699
if (!parent)
@@ -1708,14 +1710,16 @@ static int pdf_save_object(struct pdf_doc *pdf, FILE *fp, int index)
17081710
fprintf(fp, ")\r\n");
17091711
int nchildren = flexarray_size(&object->bookmark.children);
17101712
if (nchildren > 0) {
1711-
struct pdf_object *f, *l;
1712-
f = (struct pdf_object *)flexarray_get(&object->bookmark.children,
1713-
0);
1714-
l = (struct pdf_object *)flexarray_get(&object->bookmark.children,
1715-
nchildren - 1);
1713+
const struct pdf_object *f, *l;
1714+
f = (const struct pdf_object *)flexarray_get(
1715+
&object->bookmark.children, 0);
1716+
l = (const struct pdf_object *)flexarray_get(
1717+
&object->bookmark.children, nchildren - 1);
17161718
fprintf(fp, " /First %d 0 R\r\n", f->index);
17171719
fprintf(fp, " /Last %d 0 R\r\n", l->index);
1718-
fprintf(fp, " /Count %d\r\n", pdf_get_bookmark_count(object));
1720+
fprintf(
1721+
fp, " /Count %d\r\n",
1722+
pdf_get_bookmark_count((const struct pdf_object *)object));
17191723
}
17201724
// Find the previous bookmark with the same parent
17211725
for (other = object->prev;
@@ -1736,7 +1740,7 @@ static int pdf_save_object(struct pdf_doc *pdf, FILE *fp, int index)
17361740
}
17371741

17381742
case OBJ_outline: {
1739-
struct pdf_object *first, *last, *cur;
1743+
const struct pdf_object *first, *last, *cur;
17401744
first = pdf_find_first_object(pdf, OBJ_bookmark);
17411745
last = pdf_find_last_object(pdf, OBJ_bookmark);
17421746

@@ -1846,7 +1850,8 @@ static int pdf_save_object(struct pdf_doc *pdf, FILE *fp, int index)
18461850
fprintf(fp, "<<\r\n"
18471851
" /Type /Pages\r\n"
18481852
" /Kids [ ");
1849-
for (struct pdf_object *page = pdf_find_first_object(pdf, OBJ_page);
1853+
for (const struct pdf_object *page =
1854+
pdf_find_first_object(pdf, OBJ_page);
18501855
page; page = page->next) {
18511856
npages++;
18521857
fprintf(fp, "%d 0 R ", page->index);
@@ -1858,8 +1863,10 @@ static int pdf_save_object(struct pdf_doc *pdf, FILE *fp, int index)
18581863
}
18591864

18601865
case OBJ_catalog: {
1861-
struct pdf_object *outline = pdf_find_first_object(pdf, OBJ_outline);
1862-
struct pdf_object *pages = pdf_find_first_object(pdf, OBJ_pages);
1866+
const struct pdf_object *outline =
1867+
pdf_find_first_object(pdf, OBJ_outline);
1868+
const struct pdf_object *pages =
1869+
pdf_find_first_object(pdf, OBJ_pages);
18631870

18641871
fprintf(fp, "<<\r\n"
18651872
" /Type /Catalog\r\n");
@@ -1914,7 +1921,7 @@ static uint64_t hash(uint64_t hash, const void *data, size_t len)
19141921

19151922
int pdf_save_file(struct pdf_doc *pdf, FILE *fp)
19161923
{
1917-
struct pdf_object *obj;
1924+
const struct pdf_object *obj;
19181925
int xref_offset;
19191926
int xref_count = 0;
19201927
uint64_t id1, id2;
@@ -1980,10 +1987,11 @@ int pdf_save(struct pdf_doc *pdf, const char *filename)
19801987

19811988
e = pdf_save_file(pdf, fp);
19821989

1983-
if (fp != stdout)
1984-
if (fclose(fp) != 0 && e >= 0)
1990+
if (fp != stdout) {
1991+
if (fclose(fp) != 0)
19851992
return pdf_set_err(pdf, -errno, "Unable to close '%s': %s",
19861993
filename, strerror(errno));
1994+
}
19871995

19881996
return e;
19891997
}
@@ -2765,7 +2773,6 @@ int pdf_add_text_wrap(struct pdf_doc *pdf, struct pdf_object *page,
27652773
const char *new_end = find_word_break(end + 1);
27662774
float line_width;
27672775
int output = 0;
2768-
float xoff_align = xoff;
27692776
int e;
27702777

27712778
end = new_end;
@@ -2813,6 +2820,8 @@ int pdf_add_text_wrap(struct pdf_doc *pdf, struct pdf_object *page,
28132820
if (output) {
28142821
int len = end - start;
28152822
float char_spacing = 0;
2823+
float xoff_align = xoff;
2824+
28162825
if (len >= (int)sizeof(line))
28172826
len = (int)sizeof(line) - 1;
28182827
strncpy(line, start, len);
@@ -4408,7 +4417,8 @@ static int parse_jpeg_header(struct pdf_img_info *info, const uint8_t *data,
44084417

44094418
static int pdf_add_jpeg_data(struct pdf_doc *pdf, struct pdf_object *page,
44104419
float x, float y, float display_width,
4411-
float display_height, struct pdf_img_info *info,
4420+
float display_height,
4421+
const struct pdf_img_info *info,
44124422
const uint8_t *jpeg_data, size_t len)
44134423
{
44144424
struct pdf_object *obj;

0 commit comments

Comments
 (0)