[vlc-devel] [PATCH] Add open_memstream replacement

Rémi Denis-Courmont remi at remlab.net
Thu Aug 25 05:57:18 CEST 2016


Le keskiviikkona 24. elokuuta 2016, 17.43.03 EEST Derek Buitenhuis a écrit :
> [17:34] < Daemon404> use snprintf, check its return value, realloc if
> needed, continue. [17:38]

That´s called (v)asprintf() and we already use it where it´s reasonable. We 
also sometimes use it where it´s not so reasonable and results in monster 
format strings, that are illegible, have conveyed bugs times and times, and 
presuambly increase the complexity score.

With the current vlc_uri_compose(), I counted 104 different combination of 
formats, and it doesn´t even support URI fragments yet. With the other call 
sites, I can´t even see how you´d use printf() directly as we can´t express 
loops within a single printf() function call.

I only know three string buffers in C, open_memstream/fmemopen (POSIX), 
funopen (BSD) and fopencookie (GNU). The first one is the most standard. And 
perhaps more importantly it is also the least generic, so it can be 
reimplemented with the other two. The reverse is not true.

Now if you know a better C string buffers API, please advise. snprintf() is 
NOT a better C string buffers API, and I don´t suppose you suggest switching 
to C++.

> < Daemon404> (also, fyi, the current
> open_memstream based impl will fail hard if there is an alloc failure,
> since all the writes to the buffer are unchecked)

I fail to see how realloc() will make a difference there. If memory allocation 
fails, the best you can do is hope the process won´t be killed and return an 
error to the caller (which we do, AFAIK).

-- 
Rémi Denis-Courmont
http://www.remlab.net/



More information about the vlc-devel mailing list