...
In this noncompliant code example, the function utf8_to_ucswcs()
attempts to convert a sequence of UTF-8 characters to the Universal Character Set (UCS) wide characters. It first invokes setlocale()
to set the global locale to
but does not check for failure. The "
en_US.UTF-8"setlocale()
function will fail by returning a null pointer, for example, when the locale is not installed. The function may fail for other reasons as well, such as the lack of resources. Depending on the sequence of characters pointed to by utf8
, the subsequent call to mbstowcs()
may fail or result in the function storing an unexpected sequence of wide characters in the supplied buffer ucs
wcs
.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <locale.h> #include <stdlib.h> int utf8_to_ucswcs(wchar_t *ucswcs, size_t n, const char *utf8, size_t *size) { if (0 == size) { return -1; } setlocale(LC_CTYPE, "en_US.UTF-8"); *size = mbstowcs(ucswcs, utf8, n); return 0; } |
Compliant Solution (setlocale()
)
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <locale.h> #include <stdlib.h> int utf8_to_ucswcs(wchar_t *ucswcs, size_t n, const char *utf8, size_t *size) { if (0 == size) { return -1; } const char *save = setlocale(LC_CTYPE, "en_US.UTF-8"); if (NULL == save) { return -1; } *size = mbstowcs(ucswcs, utf8, n); if (NULL == setlocale(LC_CTYPE, save)) { return -1; } return 0; } |
...