Skip to content

Commit 208fcc9

Browse files
author
florian
committed
Fix BZ #116002.
Left justification of strings in myvprintf_str was mixed up. Now fixed and %s formats changed accordingly. In function myvprintf_int64: the local buffer was not large enough to hold ULONG_MAX in binary notation. Numbers were truncated at 39 digits. Testcases added. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@14808 a5019735-40e9-0310-863c-91ae7b9d1cf9
1 parent f14e372 commit 208fcc9

File tree

13 files changed

+65
-39
lines changed

13 files changed

+65
-39
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ To see details of a given bug, visit
3939
https://bugs.kde.org/show_bug.cgi?id=XXXXXX
4040
where XXXXXX is the bug number as listed below.
4141

42+
116002 VG_(printf): Problems with justification of strings and integers
4243
155125 avoid cutting away file:lineno after long function name
4344
211926 Avoid compilation warnings in valgrind.h with -pedantic
4445
269360 s390x: Fix addressing mode selection for compare-and-swap

coregrind/m_debuginfo/readdwarf3.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -2018,7 +2018,7 @@ static void trace_DIE(
20182018
DW_FORM form = (DW_FORM)abbv->nf[nf_i].at_form;
20192019
nf_i++;
20202020
if (attr == 0 && form == 0) break;
2021-
VG_(printf)(" %18s: ", ML_(pp_DW_AT)(attr));
2021+
VG_(printf)(" %-18s: ", ML_(pp_DW_AT)(attr));
20222022
/* Get the form contents, so as to print them */
20232023
get_Form_contents( &cts, cc, &c, True, form );
20242024
if (attr == DW_AT_sibling && cts.szB > 0) {
@@ -4451,7 +4451,7 @@ static void trace_debug_abbrev (const DebugInfo* di,
44514451
ULong at_name = get_ULEB128( &abbv );
44524452
ULong at_form = get_ULEB128( &abbv );
44534453
if (at_name == 0 && at_form == 0) break;
4454-
TRACE_D3(" %18s %s\n",
4454+
TRACE_D3(" %-18s %s\n",
44554455
ML_(pp_DW_AT)(at_name), ML_(pp_DW_FORM)(at_form));
44564456
}
44574457
}

coregrind/m_debuginfo/readelf.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -2383,7 +2383,7 @@ Bool ML_(read_elf_debug_info) ( struct _DebugInfo* di )
23832383
nobits = a_shdr.sh_type == SHT_NOBITS; \
23842384
vg_assert(_sec_escn.img != NULL); \
23852385
vg_assert(_sec_escn.ioff != DiOffT_INVALID); \
2386-
TRACE_SYMTAB( "%18s: ioff %llu .. %llu\n", \
2386+
TRACE_SYMTAB( "%-18s: ioff %llu .. %llu\n", \
23872387
_sec_name, (ULong)_sec_escn.ioff, \
23882388
((ULong)_sec_escn.ioff) + _sec_escn.szB - 1); \
23892389
/* SHT_NOBITS sections have zero size in the file. */ \
@@ -2677,7 +2677,7 @@ Bool ML_(read_elf_debug_info) ( struct _DebugInfo* di )
26772677
nobits = a_shdr.sh_type == SHT_NOBITS; \
26782678
vg_assert(_sec_escn.img != NULL); \
26792679
vg_assert(_sec_escn.ioff != DiOffT_INVALID); \
2680-
TRACE_SYMTAB( "%18s: dioff %llu .. %llu\n", \
2680+
TRACE_SYMTAB( "%-18s: dioff %llu .. %llu\n", \
26812681
_sec_name, \
26822682
(ULong)_sec_escn.ioff, \
26832683
((ULong)_sec_escn.ioff) + _sec_escn.szB - 1); \
@@ -2820,7 +2820,7 @@ Bool ML_(read_elf_debug_info) ( struct _DebugInfo* di )
28202820
_sec_escn.szB = a_shdr.sh_size; \
28212821
vg_assert(_sec_escn.img != NULL); \
28222822
vg_assert(_sec_escn.ioff != DiOffT_INVALID); \
2823-
TRACE_SYMTAB( "%18s: aioff %llu .. %llu\n", \
2823+
TRACE_SYMTAB( "%-18s: aioff %llu .. %llu\n", \
28242824
_sec_name, \
28252825
(ULong)_sec_escn.ioff, \
28262826
((ULong)_sec_escn.ioff) + _sec_escn.szB - 1); \

coregrind/m_debuginfo/readexidx.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ typedef
388388
ExtabData;
389389

390390
static void ppExtabData ( const ExtabData* etd ) {
391-
VG_(printf)("ExtabData{%12s 0x%08x}", showExtabCmd(etd->cmd), etd->data);
391+
VG_(printf)("ExtabData{%-12s 0x%08x}", showExtabCmd(etd->cmd), etd->data);
392392
}
393393

394394

coregrind/m_debuglog.c

+6-8
Original file line numberDiff line numberDiff line change
@@ -589,15 +589,15 @@ UInt myvprintf_str ( void(*send)(HChar,void*),
589589
}
590590

591591
extra = width - len;
592-
if (flags & VG_MSG_LJUSTIFY) {
592+
if (! (flags & VG_MSG_LJUSTIFY)) {
593593
ret += extra;
594594
for (i = 0; i < extra; i++)
595595
send(' ', send_arg2);
596596
}
597597
ret += len;
598598
for (i = 0; i < len; i++)
599599
send(MAYBE_TOUPPER(str[i]), send_arg2);
600-
if (!(flags & VG_MSG_LJUSTIFY)) {
600+
if (flags & VG_MSG_LJUSTIFY) {
601601
ret += extra;
602602
for (i = 0; i < extra; i++)
603603
send(' ', send_arg2);
@@ -658,7 +658,10 @@ UInt myvprintf_int64 ( void(*send)(HChar,void*),
658658
Bool capitalised,
659659
ULong p )
660660
{
661-
HChar buf[40];
661+
/* To print an ULong base 2 needs 64 characters. If commas are requested,
662+
add 21. Plus 1 for a possible sign plus 1 for \0. Makes 87 -- so let's
663+
say 90. The size of BUF needs to be max(90, WIDTH + 1) */
664+
HChar buf[width + 1 > 90 ? width + 1 : 90];
662665
Int ind = 0;
663666
Int i, nc = 0;
664667
Bool neg = False;
@@ -693,11 +696,6 @@ UInt myvprintf_int64 ( void(*send)(HChar,void*),
693696

694697
if (width > 0 && !(flags & VG_MSG_LJUSTIFY)) {
695698
for(; ind < width; ind++) {
696-
/* vg_assert(ind < 39); */
697-
if (ind > 39) {
698-
buf[39] = 0;
699-
break;
700-
}
701699
buf[ind] = (flags & VG_MSG_ZJUSTIFY) ? '0': ' ';
702700
}
703701
}

coregrind/m_gdbserver/target.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ struct reg* build_shadow_arch (struct reg *reg_defs, int n) {
116116
new_regs[i*n + r].offset = i*reg_set_len + reg_defs[r].offset;
117117
new_regs[i*n + r].size = reg_defs[r].size;
118118
dlog(1,
119-
"%10s Nr %d offset(bit) %d offset(byte) %d size(bit) %d\n",
119+
"%-10s Nr %d offset(bit) %d offset(byte) %d size(bit) %d\n",
120120
new_regs[i*n + r].name, i*n + r, new_regs[i*n + r].offset,
121121
(new_regs[i*n + r].offset) / 8, new_regs[i*n + r].size);
122122
}

coregrind/m_mallocfree.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ void VG_(print_all_arena_stats) ( void )
626626
for (i = 0; i < VG_N_ARENAS; i++) {
627627
Arena* a = arenaId_to_ArenaP(i);
628628
VG_(message)(Vg_DebugMsg,
629-
"%8s: %8lu/%8lu max/curr mmap'd, "
629+
"%-8s: %8lu/%8lu max/curr mmap'd, "
630630
"%llu/%llu unsplit/split sb unmmap'd, "
631631
"%8lu/%8lu max/curr, "
632632
"%10llu/%10llu totalloc-blocks/bytes,"
@@ -1358,7 +1358,7 @@ static void sanity_check_malloc_arena ( ArenaId aid )
13581358

13591359
if (VG_(clo_verbosity) > 2)
13601360
VG_(message)(Vg_DebugMsg,
1361-
"%8s: %2d sbs, %5d bs, %2d/%-2d free bs, "
1361+
"%-8s: %2d sbs, %5d bs, %2d/%-2d free bs, "
13621362
"%7ld mmap, %7ld loan\n",
13631363
a->name,
13641364
superblockctr,

coregrind/m_redir.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1694,7 +1694,7 @@ static void handle_require_text_symbols ( const DebugInfo* di )
16941694
static void show_spec ( const HChar* left, const Spec* spec )
16951695
{
16961696
VG_(message)( Vg_DebugMsg,
1697-
"%s%25s %30s %s-> (%04d.%d) 0x%08llx\n",
1697+
"%s%-25s %-30s %s-> (%04d.%d) 0x%08llx\n",
16981698
left,
16991699
spec->from_sopatt, spec->from_fnpatt,
17001700
spec->isWrap ? "W" : "R",
@@ -1717,7 +1717,7 @@ static void show_active ( const HChar* left, const Active* act )
17171717
ok = VG_(get_fnname_w_offset)(act->to_addr, &name2);
17181718
if (!ok) name2 = "???";
17191719

1720-
VG_(message)(Vg_DebugMsg, "%s0x%08llx (%20s) %s-> (%04d.%d) 0x%08llx %s\n",
1720+
VG_(message)(Vg_DebugMsg, "%s0x%08llx (%-20s) %s-> (%04d.%d) 0x%08llx %s\n",
17211721
left,
17221722
(ULong)act->from_addr, name1,
17231723
act->isWrap ? "W" : "R",

coregrind/m_seqmatch.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ Bool VG_(string_match) ( const HChar* patt, const HChar* input )
227227
//{
228228
// Test* t;
229229
// for (t = tests; t->patt; t++) {
230-
// printf("%10s %6s %s\n",
230+
// printf("%-10s %-6s %s\n",
231231
// t->patt, t->input,
232232
// match_string_all((UChar*)t->patt,(UChar*)t->input,True)
233233
// == t->xres

lackey/lk_main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ static void print_details ( void )
388388
VG_(umsg)(" Type Loads Stores AluOps\n");
389389
VG_(umsg)(" -------------------------------------------\n");
390390
for (typeIx = 0; typeIx < N_TYPES; typeIx++) {
391-
VG_(umsg)(" %4s %'12llu %'12llu %'12llu\n",
391+
VG_(umsg)(" %-4s %'12llu %'12llu %'12llu\n",
392392
nameOfTypeIndex( typeIx ),
393393
detailCounts[OpLoad ][typeIx],
394394
detailCounts[OpStore][typeIx],

memcheck/mc_leakcheck.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,7 @@ static void print_results(ThreadId tid, LeakCheckParams* lcp)
14671467
for (i = 0; i < N_LEAK_CHECK_HEURISTICS; i++)
14681468
if (old_blocks_heuristically_reachable[i] > 0
14691469
|| MC_(blocks_heuristically_reachable)[i] > 0)
1470-
VG_(umsg)(" %19s: "
1470+
VG_(umsg)(" %-19s: "
14711471
"%'lu%s bytes in %'lu%s blocks\n",
14721472
pp_heuristic(i),
14731473
MC_(bytes_heuristically_reachable)[i],

none/tests/unit_debuglog.c

+22-17
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
void run(const char *format, ...)
2020
{
21-
int n, num_stars, i1, i2;
21+
int n, num_stars;
2222
const char *p;
2323
printf_buf buf;
2424
va_list vargs;
@@ -30,20 +30,7 @@ void run(const char *format, ...)
3030

3131
va_start(vargs, format);
3232
fprintf(stderr, "%s\tprintf = ", format);
33-
switch (num_stars) {
34-
case 0:
35-
n = fprintf(stderr, format, va_arg(vargs, Double));
36-
break;
37-
case 1:
38-
i1 = va_arg(vargs, int);
39-
n = fprintf(stderr, format, i1, va_arg(vargs, Double));
40-
break;
41-
case 2:
42-
i1 = va_arg(vargs, int);
43-
i2 = va_arg(vargs, int);
44-
n = fprintf(stderr, format, i1, i2, va_arg(vargs, Double));
45-
break;
46-
}
33+
n = vfprintf(stderr, format, vargs);
4734
fprintf(stderr, "\twrote %3d chars\n", n);
4835
va_end(vargs);
4936

@@ -59,7 +46,6 @@ void run(const char *format, ...)
5946
fprintf(stderr, "\twrote %3d chars\n", n);
6047
}
6148

62-
6349
int main(int argc, char *argv[])
6450
{
6551
double value;
@@ -138,11 +124,30 @@ int main(int argc, char *argv[])
138124
run("|%*.*f|", 20, 5, value);
139125
run("|%*.*f|", 1, 4, value);
140126

141-
142127
fprintf(stderr, "\n");
143128
fprintf(stderr, "...testing left justification\n");
144129
value = 3.1415;
145130
run("|%10f|", value);
146131
run("|%-10f|", value);
132+
133+
fprintf(stderr, "\n");
134+
fprintf(stderr, "...testing strings\n");
135+
const char *str = "abcd";
136+
run("|%s|", str);
137+
run("|%9s|", str);
138+
run("|%-9s|", str);
139+
run("|%*s|", 6, str);
140+
141+
fprintf(stderr, "\n");
142+
fprintf(stderr, "...testing integers\n");
143+
long long ival = -1004005;
144+
run("|%lld|", ival);
145+
// runint("|%'lld|", ival); // locale specific (LC_NUMERIC)
146+
run("|%15lld|", ival);
147+
run("|%-15lld|", ival);
148+
// runint("|%'-15lld|", ival); // locale specific (LC_NUMERIC)
149+
run("|%100lld|", ival);
150+
run("|%*lld|", 13, ival);
151+
147152
return 0;
148153
}

none/tests/unit_debuglog.stderr.exp

+22
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,25 @@
105105
|%10f| debuglog = | 3.141500| wrote 12 chars
106106
|%-10f| printf = |3.141500 | wrote 12 chars
107107
|%-10f| debuglog = |3.141500 | wrote 12 chars
108+
109+
...testing strings
110+
|%s| printf = |abcd| wrote 6 chars
111+
|%s| debuglog = |abcd| wrote 6 chars
112+
|%9s| printf = | abcd| wrote 11 chars
113+
|%9s| debuglog = | abcd| wrote 11 chars
114+
|%-9s| printf = |abcd | wrote 11 chars
115+
|%-9s| debuglog = |abcd | wrote 11 chars
116+
|%*s| printf = | abcd| wrote 8 chars
117+
|%*s| debuglog = | abcd| wrote 8 chars
118+
119+
...testing integers
120+
|%lld| printf = |-1004005| wrote 10 chars
121+
|%lld| debuglog = |-1004005| wrote 10 chars
122+
|%15lld| printf = | -1004005| wrote 17 chars
123+
|%15lld| debuglog = | -1004005| wrote 17 chars
124+
|%-15lld| printf = |-1004005 | wrote 17 chars
125+
|%-15lld| debuglog = |-1004005 | wrote 17 chars
126+
|%100lld| printf = | -1004005| wrote 102 chars
127+
|%100lld| debuglog = | -1004005| wrote 102 chars
128+
|%*lld| printf = | -1004005| wrote 15 chars
129+
|%*lld| debuglog = | -1004005| wrote 15 chars

0 commit comments

Comments
 (0)