safeclib/vwscanf_s.c

55 lines
1.3 KiB
C

#include <wchar.h>
#include <stdarg.h>
#include <errno.h>
EXPORT int vwscanf_s(const wchar_t *restrict fmt, va_list ap) {
#if defined(HAVE_WCSSTR)
wchar_t *p;
#endif
int ret;
if (unlikely(fmt == NULL)) {
invoke_safe_str_constraint_handler(L"vwscanf_s: fmt is null", NULL, ESNULLP);
errno = ESNULLP;
return EOF;
}
#if defined(HAVE_WCSSTR)
if (unlikely((p = wcsstr(fmt, L"%n")))) {
if ((p - fmt == 0) || *(p - 1) != L'%') {
invoke_safe_str_constraint_handler(L"vwscanf_s: illegal %n", NULL, EINVAL);
errno = EINVAL;
return EOF;
}
}
#endif
errno = 0;
ret = vwscanf(fmt, ap);
if (unlikely(ret < 0)) {
wchar_t errstr[128] = L"vwscanf_s: ";
wcscat(errstr, _wcserror(errno));
invoke_safe_str_constraint_handler(errstr, NULL, errno);
}
return ret;
}
int wrapped_vwscanf_s(const wchar_t *format, ...) {
va_list args;
va_start(args, format);
int result = vwscanf_s(format, args);
va_end(args);
return result;
}
void test_vwscanf_s() {
wprintf(L"Please type 'world' followed by '1234':\n");
wchar_t buf[10];
int num;
assert(wrapped_vwscanf_s(L"%9ls %d", buf, sizeof(buf)/sizeof(wchar_t), &num) == 2);
assert(wcscmp(buf, L"world") == 0);
assert(num == 1234);
}