Boost logo

Boost :

From: Alexander Grund (alexander.grund_at_[hidden])
Date: 2019-12-03 14:06:27


in my work on Boost.Nowide I encountered a valid NULL string and am
unsure how to handle it.

Context: Every wrapper functions is basically implemented like:
int fFOO(const char* s){
 Â  wstackstring const ws(s); // Converts UTF-8 to wchar-buffer/string
 Â  return _wfFoo(ws.c_str());

Similar functions like `std::wstring widen(string-or-const-char-ptr)`
are provided which can be used like `return _wfFoo(widen(s).c_str());`
in the example above.

All was fine, because (IIRC) calling e.g. `fopen(NULL)` is not allowed

However `freopen` DOES allow a NULL for the string. So now I'm left with
a couple options and would like some opinions on them:

1. Make (w)stackstring aware of NULL input and if default constructed or
with NULL return NULL for c_str().
 Â Â Â  Makes it easy to use as one could even say `return
_wfFoo(wstackstring(s).c_str());` w/o any checks but would be
inconsistent with the `widen` functions which convert the passed pointer
into a std::(w)string and hence require != NULL
2. Disallow NULL (via assert) for all conversions and require manual
checking. Best usage would then probably be to ditch the temporary and
do `return _wfFoo(s ? wstackstring(s).c_str() : NULL);`
3. Just like 1. but rename (w)stackstring to e.g. (w)cstring (or
ncstring/wcstring, or narrow_cstring/wide_cstring) to highlight that it
acts like a C-String (and can be NULL as opposed to empty but never NULL)
4. Ditch stackstring entirely. It was complained about by Zach during
the review although defended by the author as "stackstring is barely
on-stack buffer optimization - it isn't general string
and never intended to be so. It isn't in details because it can actually
be useful outside library scope." So this would worsen performance a bit
as usual SSO are to small for common filenames.

Thanks for reading and any insight is welcome!


Boost list run by bdawes at, gregod at, cpdaniel at, john at