GNOME Bugzilla – Bug 767839
GLib format strings for int64 values don't require "ll" modifier - causes segfault on 32-bit arches
Last modified: 2018-05-22 15:36:34 UTC
When formatting an int64 value using GLib format strings (e.g. say using GLib.debug), valac is not complaining if the correct length modifier is not set, i.e. "ll". While valac does not complain about this, gcc does on the translated C file, but that's basically impossible to spot amounts all of the warnings gcc spits out unsless you go looking for it. For example, valac will compile this fine, without warning: > int64 x = 0; > debug("%d", x); Valac should instead require "%lld" as the format string, as gcc points out. This will cause programs to segfault on 32-bit arches.
With your two lines, valac 0.32.0 doesn't just warn, it reports an error: > test.vala:3.14-3.14: error: Argument 2: Cannot convert from `int64' to `int' > debug("%d", x); > ^ > Compilation failed: 1 error(s), 0 warning(s) Please provide a full test case where this issue can be reproduced.
Created attachment 330049 [details] Minimal testcase Turns out the problem occurs when the format string is formed by concatenating two (or presumably more) strings: > int64 x = 0; > debug(" " + "%d", x); In my case, the format string was rather long and hence split over multiple lines.
Actually, gcc will also warn when using int64 and %lld. Because on 64bit platforms, %lld is long long while int64 is long. For int64 the correct format is G_INT64_FORMAT.
(In reply to Luca Bruno from comment #3) > Actually, gcc will also warn when using int64 and %lld. Because on 64bit > platforms, %lld is long long while int64 is long. For int64 the correct > format is G_INT64_FORMAT. Yes, however, %lld is safe on all common 32-bit and 64-bit platforms, to my knowledge. It's unfortunate that int64_t is typically defined as long instead of long long on 64-bit systems. People use %ld for 64-bit values because of that even though %lld is more portable.
Also, it's more concerning that valac doesn't report an error, especially since it leads to crashes on 32bit arches.
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/543.