Use mbstowcs
with NULL destination pointer:
If dest is NULL, n is ignored, and the conversion proceeds as above, except that the converted wide characters are not written out to memory, and that no length limit exists.
....
- An invalid multibyte sequence has been encountered. In this case (size_t) -1 is returned.
(from mbstowcs man page).
That is, the result of (size_t)-1
means there was an invalid multibyte sequence.