<div dir="ltr"><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_extra"><br><div class="gmail_quote">2018-01-17 17:19 GMT+01:00 James Cowgill <span dir="ltr"><<a href="mailto:jcowgill@debian.org" target="_blank">jcowgill@debian.org</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<span class=""><br>
On 17/01/18 15:42, Jaromír Mikeš wrote:<br>
> Hi,<br>
><br>
> giada unfortunately fail to build on some archs ... I already informed<br>
> upstream but not answer yet.<br>
> Can someone look if fixing this is rather trivial or complicated.<br>
><br>
> <a href="https://buildd.debian.org/status/package.php?p=giada" rel="noreferrer" target="_blank">https://buildd.debian.org/<wbr>status/package.php?p=giada</a><br>
<br>
</span>The entire function for reference:<br>
> std::string gu_format(const char* format, ...)<br>
> {<br>
> va_list args;<br>
><br>
> /* Compute the size of the new expanded string (i.e. with replacement taken<br>
> into account). */<br>
><br>
> size_t size = vsnprintf(nullptr, 0, format, args);<br>
><br>
> /* Create a new temporary char array to hold the new expanded string. */<br>
><br>
> std::unique_ptr<char[]> tmp(new char[size]);<br>
><br>
> /* Fill the temporary string with the formatted data. */<br>
><br>
> va_start(args, format);<br>
> vsprintf(tmp.get(), format, args);<br>
> va_end(args);<br>
><br>
> return string(tmp.get(), tmp.get() + size - 1);<br>
> }<br>
<br>
This line (the one the error complains about) reads the uninitialized<br>
args and invokes undefined behavior:<br>
> size_t size = vsnprintf(nullptr, 0, format, args);<br>
<br>
It needs to be surrounded in va_start, va_end block.<br>
<br>
The second subtle error is that vsnprintf returns the size _excluding<br>
the null byte_. This will cause the vsprintf call to overflow the buffer<br>
by 1 byte.<br>
<br>
This might work (untested):<br>
va_start(args, format);<br>
size_t size = vsnprintf(nullptr, 0, format, args) + 1;<br>
va_end(args);<br>
<br>
Some alternative implementations. The varardic template solution (the<br>
third one) is similar to this code (and the one I like the most):<br>
<a href="https://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf" rel="noreferrer" target="_blank">https://stackoverflow.com/<wbr>questions/2342162/stdstring-<wbr>formatting-like-sprintf</a></blockquote><div><br></div><div class="gmail_default" style="font-size:small">Thank you James! Uploaded! Let's see.</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">mira</div></div><br></div></div>