matteo_c
matteo_c

Reputation: 2260

Is this implementation of MSVCRT's wcscpy_s correct?

Is this implementation of wcscpy_s from MSVCRT (reference) correct?

errno_t wcscpy_s(
   wchar_t *dest,
   rsize_t dest_size,
   const wchar_t *src
)
{
    if (dest == NULL) {
        return EINVAL;
    }
    if (src == NULL) {
        dest[0] = L'\0';
        return EINVAL;
    }

    size_t src_size = wcsnlen(src, min(dest_size, RSIZE_MAX) - 1) + 1;
    if (dest_size < src_size) {
        dest[0] = L'\0';
        return ERANGE;
    }

    wcsncpy(dest, src, src_size);
    return 0;
}

Assume wcsnlen and wcsncpy to be POSIX conforming. Assume also macros and types to be correctly defined.

The -1) + 1 is present because wcslen expects sizes to not include the terminating null wide character, while wcscpy_s and wcsncpy sizes do.

Any correction, feedback, or suggestion is appreciated.

Upvotes: 1

Views: 148

Answers (0)

Related Questions