From 66cfc0fd0d9daadc2dac5a783b69b4050c0b1722 Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Tue, 26 Sep 2017 11:28:04 +0930 Subject: [PATCH] PR21732, String-concatenated message can not be translated Preprocessor tokens like DWARF_VMA_FMT and BFD_VMA_FMT are not understood by gettext, and cause the translation string to be truncated. Even if it were not truncated, we wouldn't want a value that changes depending on host or configure options to be put in a translation string. PR 21732 * dwarf.c (READ_ULEB): Don't use DWARF_VMA_FMT in translated string. (READ_SLEB): Likewise. * readelf.c (bfd_vmatoa): New function. (get_data, get_dynamic_data): Use it for printing error messages. --- binutils/ChangeLog | 8 ++++++ binutils/dwarf.c | 10 ++++--- binutils/readelf.c | 72 +++++++++++++++++++++++++++++----------------- 3 files changed, 60 insertions(+), 30 deletions(-) diff --git a/binutils/ChangeLog b/binutils/ChangeLog index f7b8d7076cf..2b50930ee3d 100644 --- a/binutils/ChangeLog +++ b/binutils/ChangeLog @@ -1,3 +1,11 @@ +2017-09-26 Alan Modra + + PR 21732 + * dwarf.c (READ_ULEB): Don't use DWARF_VMA_FMT in translated string. + (READ_SLEB): Likewise. + * readelf.c (bfd_vmatoa): New function. + (get_data, get_dynamic_data): Use it for printing error messages. + 2017-09-25 H.J. Lu PR binutils/22203 diff --git a/binutils/dwarf.c b/binutils/dwarf.c index af8732bb804..d4156e480ad 100644 --- a/binutils/dwarf.c +++ b/binutils/dwarf.c @@ -353,8 +353,9 @@ read_uleb128 (unsigned char * data, \ (var) = _val = read_uleb128 (start, &length_return, end); \ if ((var) != _val) \ - error (_("Internal error: %s%d: LEB value (%#" DWARF_VMA_FMT "x) too large for containing variable\n"), \ - __FILE__, __LINE__, _val); \ + error (_("Internal error: %s:%d: LEB value (%s) " \ + "too large for containing variable\n"), \ + __FILE__, __LINE__, dwarf_vmatoa ("u", _val)); \ start += length_return; \ } \ while (0) @@ -366,8 +367,9 @@ read_uleb128 (unsigned char * data, \ (var) = _val = read_sleb128 (start, &length_return, end); \ if ((var) != _val) \ - error (_("Internal error: %s%d: LEB value (%#" DWARF_VMA_FMT "x) too large for containing variable\n"), \ - __FILE__, __LINE__, _val); \ + error (_("Internal error: %s:%d: LEB value (%s) " \ + "too large for containing variable\n"), \ + __FILE__, __LINE__, dwarf_vmatoa ("d", _val)); \ start += length_return; \ } \ while (0) diff --git a/binutils/readelf.c b/binutils/readelf.c index 9a8226fa611..3e5ad0e4c5c 100644 --- a/binutils/readelf.c +++ b/binutils/readelf.c @@ -321,6 +321,30 @@ static const char * get_symbol_version_string } \ while (0) +/* Print a BFD_VMA to an internal buffer, for use in error messages. + BFD_FMA_FMT can't be used in translated strings. */ + +static const char * +bfd_vmatoa (char *fmtch, bfd_vma value) +{ + /* bfd_vmatoa is used more then once in a printf call for output. + Cycle through an array of buffers. */ + static int buf_pos = 0; + static struct bfd_vmatoa_buf + { + char place[64]; + } buf[4]; + char *ret; + char fmt[32]; + + ret = buf[buf_pos++].place; + buf_pos %= ARRAY_SIZE (buf); + + sprintf (fmt, "%%%s%s", BFD_VMA_FMT, fmtch); + snprintf (ret, sizeof (buf[0].place), fmt, value); + return ret; +} + /* Retrieve NMEMB structures, each SIZE bytes long from FILE starting at OFFSET + the offset of the current archive member, if we are examining an archive. Put the retrieved data into VAR, if it is not NULL. Otherwise allocate a buffer @@ -347,9 +371,9 @@ get_data (void * var, FILE * file, unsigned long offset, bfd_size_type size, || (bfd_size_type) ((size_t) nmemb) != nmemb)) { if (reason) - error (_("Size truncation prevents reading 0x%" BFD_VMA_FMT "x" - " elements of size 0x%" BFD_VMA_FMT "x for %s\n"), - nmemb, size, reason); + error (_("Size truncation prevents reading %s" + " elements of size %s for %s\n"), + bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason); return NULL; } @@ -357,9 +381,9 @@ get_data (void * var, FILE * file, unsigned long offset, bfd_size_type size, if (amt < nmemb) { if (reason) - error (_("Size overflow prevents reading 0x%" BFD_VMA_FMT "x" - " elements of size 0x%" BFD_VMA_FMT "x for %s\n"), - nmemb, size, reason); + error (_("Size overflow prevents reading %s" + " elements of size %s for %s\n"), + bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason); return NULL; } @@ -369,9 +393,8 @@ get_data (void * var, FILE * file, unsigned long offset, bfd_size_type size, || offset + archive_file_offset + amt > current_file_size) { if (reason) - error (_("Reading 0x%" BFD_VMA_FMT "x" - " bytes extends past end of file for %s\n"), - amt, reason); + error (_("Reading %s bytes extends past end of file for %s\n"), + bfd_vmatoa ("u", amt), reason); return NULL; } @@ -394,9 +417,8 @@ get_data (void * var, FILE * file, unsigned long offset, bfd_size_type size, if (mvar == NULL) { if (reason) - error (_("Out of memory allocating 0x%" BFD_VMA_FMT "x" - " bytes for %s\n"), - amt, reason); + error (_("Out of memory allocating %s bytes for %s\n"), + bfd_vmatoa ("u", amt), reason); return NULL; } @@ -406,8 +428,8 @@ get_data (void * var, FILE * file, unsigned long offset, bfd_size_type size, if (fread (mvar, (size_t) size, (size_t) nmemb, file) != nmemb) { if (reason) - error (_("Unable to read in 0x%" BFD_VMA_FMT "x bytes of %s\n"), - amt, reason); + error (_("Unable to read in %s bytes of %s\n"), + bfd_vmatoa ("u", amt), reason); if (mvar != var) free (mvar); return NULL; @@ -10957,9 +10979,8 @@ get_dynamic_data (FILE * file, bfd_size_type number, unsigned int ent_size) if (sizeof (size_t) < sizeof (bfd_size_type) && (bfd_size_type) ((size_t) number) != number) { - error (_("Size truncation prevents reading %" BFD_VMA_FMT "u" - " elements of size %u\n"), - number, ent_size); + error (_("Size truncation prevents reading %s elements of size %u\n"), + bfd_vmatoa ("u", number), ent_size); return NULL; } @@ -10967,23 +10988,23 @@ get_dynamic_data (FILE * file, bfd_size_type number, unsigned int ent_size) attempting to allocate memory when the read is bound to fail. */ if (ent_size * number > current_file_size) { - error (_("Invalid number of dynamic entries: %" BFD_VMA_FMT "u\n"), - number); + error (_("Invalid number of dynamic entries: %s\n"), + bfd_vmatoa ("u", number)); return NULL; } e_data = (unsigned char *) cmalloc ((size_t) number, ent_size); if (e_data == NULL) { - error (_("Out of memory reading %" BFD_VMA_FMT "u dynamic entries\n"), - number); + error (_("Out of memory reading %s dynamic entries\n"), + bfd_vmatoa ("u", number)); return NULL; } if (fread (e_data, ent_size, (size_t) number, file) != number) { - error (_("Unable to read in %" BFD_VMA_FMT "u bytes of dynamic data\n"), - number * ent_size); + error (_("Unable to read in %s bytes of dynamic data\n"), + bfd_vmatoa ("u", number * ent_size)); free (e_data); return NULL; } @@ -10991,9 +11012,8 @@ get_dynamic_data (FILE * file, bfd_size_type number, unsigned int ent_size) i_data = (bfd_vma *) cmalloc ((size_t) number, sizeof (*i_data)); if (i_data == NULL) { - error (_("Out of memory allocating space for %" BFD_VMA_FMT "u" - " dynamic entries\n"), - number); + error (_("Out of memory allocating space for %s dynamic entries\n"), + bfd_vmatoa ("u", number)); free (e_data); return NULL; } -- 2.30.2