aclements / libelfin

C++11 ELF/DWARF parser
MIT License
317 stars 99 forks source link

Non-portable printf() in examples/dump-lines.cc #20

Closed petterreinholdtsen closed 7 years ago

petterreinholdtsen commented 7 years ago

The printf() statement in examples/dump-lines.cc try to print a uint64_t value using %lx. This do not work on all architectures. I worked around the issue by changing %lx to %llx and casting the value to long long, but suspect it isn't the best solution. What is your view on this?

The Debian patch can be found in https://anonscm.debian.org/cgit/pkg-coz/libelfin.git/tree/debian/patches/0002-portable-uint64-print.patch .

llvilanova commented 7 years ago

The portable way is to include inttypes.h and use "%"PRIx64 as the printing format. More info: http://en.cppreference.com/w/c/types/integer

petterreinholdtsen commented 7 years ago

Just to be clear, the "do not work" statement mean the build fail when the default compiler flags are used in Debian. https://buildd.debian.org/status/fetch.php?pkg=libelfin&arch=mipsel&ver=0.2-4&stamp=1479216536 show the kind of build failure we got on some autobuilders:

dump-lines.cc: In function 'void dump_line_table(const dwarf::line_table&)':
dump-lines.cc:16:55: error: format '%lx' expects argument of type 'long unsigned int', but argument 4 has type 'dwarf::taddr {aka long long unsigned int}' [-Werror=format=]
                                line.line, line.address);
                                                       ^
cc1plus: all warnings being treated as errors
aclements commented 7 years ago

As a bonus, I just cleaned up all of the places where I was lazy and cast arguments to printf to unsigned long so they now use the inttypes.h macros.