Skip to content

Commit d5afff8

Browse files
author
Mark Wielaard
committed
ar: Fix GCC7 -Wformat-length issues.
GCC7 adds warnings for snprintf formatting into too small buffers. Fix the two issues pointed out by the new warning. The ar header fields are fixed length containing left-justified strings without zero terminator. snprintf always adds a '\0' char at the end (which we then don't copy into the ar header field) and numbers are decimal strings of fixed 10 chars (-Wformat-length thinks formatting them as size_t might overflow the buffer on 64bit arches). Signed-off-by: Mark Wielaard <[email protected]>
1 parent 09ec02e commit d5afff8

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

src/ChangeLog

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2016-11-10 Mark Wielaard <[email protected]>
2+
3+
* ar.c (write_member): Make sure tmpbuf is large enough to contain
4+
a starting '/' and ending '\0' char.
5+
(do_oper_insert): Likewise.
6+
* arlib.c (arlib_finalize): Format tmpbuf as PRId32 decimal.
7+
18
2016-11-02 Mark Wielaard <[email protected]>
29

310
* addr2line.c (handle_address): Add fallthrough comment.

src/ar.c

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Create, modify, and extract from archives.
2-
Copyright (C) 2005-2012 Red Hat, Inc.
2+
Copyright (C) 2005-2012, 2016 Red Hat, Inc.
33
This file is part of elfutils.
44
Written by Ulrich Drepper <[email protected]>, 2005.
55
@@ -853,7 +853,10 @@ write_member (struct armem *memb, off_t *startp, off_t *lenp, Elf *elf,
853853
off_t end_off, int newfd)
854854
{
855855
struct ar_hdr arhdr;
856-
char tmpbuf[sizeof (arhdr.ar_name) + 1];
856+
/* The ar_name is not actually zero teminated, but we need that for
857+
snprintf. Also if the name is too long, then the string starts
858+
with '/' plus an index off number (decimal). */
859+
char tmpbuf[sizeof (arhdr.ar_name) + 2];
857860

858861
bool changed_header = memb->long_name_off != -1;
859862
if (changed_header)
@@ -1455,7 +1458,11 @@ do_oper_insert (int oper, const char *arfname, char **argv, int argc,
14551458

14561459
/* Create the header. */
14571460
struct ar_hdr arhdr;
1458-
char tmpbuf[sizeof (arhdr.ar_name) + 1];
1461+
/* The ar_name is not actually zero teminated, but we
1462+
need that for snprintf. Also if the name is too
1463+
long, then the string starts with '/' plus an index
1464+
off number (decimal). */
1465+
char tmpbuf[sizeof (arhdr.ar_name) + 2];
14591466
if (all->long_name_off == -1)
14601467
{
14611468
size_t namelen = strlen (all->name);
@@ -1465,7 +1472,7 @@ do_oper_insert (int oper, const char *arfname, char **argv, int argc,
14651472
}
14661473
else
14671474
{
1468-
snprintf (tmpbuf, sizeof (arhdr.ar_name) + 1, "/%-*ld",
1475+
snprintf (tmpbuf, sizeof (tmpbuf), "/%-*ld",
14691476
(int) sizeof (arhdr.ar_name), all->long_name_off);
14701477
memcpy (arhdr.ar_name, tmpbuf, sizeof (arhdr.ar_name));
14711478
}

src/arlib.c

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Functions to handle creation of Linux archives.
2-
Copyright (C) 2007-2012 Red Hat, Inc.
2+
Copyright (C) 2007-2012, 2016 Red Hat, Inc.
33
This file is part of elfutils.
44
Written by Ulrich Drepper <[email protected]>, 2007.
55
@@ -23,6 +23,7 @@
2323
#include <assert.h>
2424
#include <error.h>
2525
#include <gelf.h>
26+
#include <inttypes.h>
2627
#include <libintl.h>
2728
#include <stdio.h>
2829
#include <stdlib.h>
@@ -107,6 +108,9 @@ arlib_init (void)
107108
void
108109
arlib_finalize (void)
109110
{
111+
/* Note that the size is stored as decimal string in 10 chars,
112+
without zero terminator (we add + 1 here only so snprintf can
113+
put it at the end, we then don't use it when we memcpy it). */
110114
char tmpbuf[sizeof (((struct ar_hdr *) NULL)->ar_size) + 1];
111115

112116
symtab.longnameslen = obstack_object_size (&symtab.longnamesob);
@@ -121,9 +125,9 @@ arlib_finalize (void)
121125

122126
symtab.longnames = obstack_finish (&symtab.longnamesob);
123127

124-
int s = snprintf (tmpbuf, sizeof (tmpbuf), "%-*zu",
128+
int s = snprintf (tmpbuf, sizeof (tmpbuf), "%-*" PRIu32 "",
125129
(int) sizeof (((struct ar_hdr *) NULL)->ar_size),
126-
symtab.longnameslen - sizeof (struct ar_hdr));
130+
(uint32_t) (symtab.longnameslen - sizeof (struct ar_hdr)));
127131
memcpy (&((struct ar_hdr *) symtab.longnames)->ar_size, tmpbuf, s);
128132
}
129133

@@ -169,10 +173,10 @@ arlib_finalize (void)
169173

170174
/* See comment for ar_date above. */
171175
memcpy (&((struct ar_hdr *) symtab.symsoff)->ar_size, tmpbuf,
172-
snprintf (tmpbuf, sizeof (tmpbuf), "%-*zu",
176+
snprintf (tmpbuf, sizeof (tmpbuf), "%-*" PRIu32 "",
173177
(int) sizeof (((struct ar_hdr *) NULL)->ar_size),
174-
symtab.symsofflen + symtab.symsnamelen
175-
- sizeof (struct ar_hdr)));
178+
(uint32_t) (symtab.symsofflen + symtab.symsnamelen
179+
- sizeof (struct ar_hdr))));
176180
}
177181

178182

0 commit comments

Comments
 (0)