Pointer to invalid multibyte sequence in mbsnrtowcs()

Igor Liferenko igor.liferenko@gmail.com
Tue Dec 20 01:34:00 GMT 2016


Hi Michael,

It seems that *src is not changed to point to invalid multibyte
sequence only when dest is NULL.

There are two possible cases:

1) incomplete multibyte character is at the end of input buffer *)
2) incomplete multibyte character is not at the end of input buffer *)

*) - end of buffer is determined by nms argument.

The examples 1.1 and 2.1 demonstrate that when dest is NULL, *src is
not changed.
The examples 1.2 and 2.2 demenstrate that *src is correctly changed
when dest is not NULL.

It is unclear whether it is POSIX behavior (and thus mbsnrtowcs(3)
should be corrected), or it is a bug.

Example 1.1 (at the end of buffer, dest is NULL):

    #include <locale.h>
    #include <wchar.h>
    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
      setlocale(LC_CTYPE, "en_US.UTF-8");
      char *s = "\321\216\321";
      const char *x = s;
      printf("status: %d\n", mbsnrtowcs(NULL,&x,strlen(s),0,NULL));
      perror(NULL);
      printf("ori=%p\nnew=%p\n",(void *)s,(void *)x);
      return 0;
    }

Output 1.1:

    status: 1
    Success
    ori=0x56337c86d910
    new=0x56337c86d910


Example 2.1 (not at the end of buffer, dest is NULL):

    #include <locale.h>
    #include <wchar.h>
    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
      setlocale(LC_CTYPE, "en_US.UTF-8");
      char *s = "\321\216\321\321\215";
      const char *x = s;
      printf("status: %d\n", mbsnrtowcs(NULL,&x,strlen(s),0,NULL));
      perror(NULL);
      printf("ori=%p\nnew=%p\n",(void *)s,(void *)x);
      return 0;
    }

Output 2.1:

    status: -1
    Invalid or incomplete multibyte or wide character
    ori=0x55ad82792910
    new=0x55ad82792910

Example 1.2 (at the end of buffer, dest is not NULL):

    #include <locale.h>
    #include <wchar.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int main(void)
    {
      setlocale(LC_CTYPE, "en_US.UTF-8");
      char *s = "\321\216\321";
      const char *x = s;
      wchar_t *wcs = calloc(strlen(s) + 1, sizeof(wchar_t));
      printf("status: %d\n", mbsnrtowcs(wcs,&x,strlen(s),strlen(s),NULL));
      perror(NULL);
      printf("ori=%p\nnew=%p\n",(void *)s,(void *)x);
      return 0;
    }

Output 1.2:

    status: 1
    Success
    ori=0x556497c29980
    new=0x556497c29983


Example 2.2 (not at the end of buffer, dest is not NULL):

    #include <locale.h>
    #include <wchar.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int main(void)
    {
      setlocale(LC_CTYPE, "en_US.UTF-8");
      char *s = "\321\216\321\321\215";
      const char *x = s;
      wchar_t *wcs = calloc(strlen(s) + 1, sizeof(wchar_t));
      printf("status: %d\n", mbsnrtowcs(wcs,&x,strlen(s),strlen(s),NULL));
      perror(NULL);
      printf("ori=%p\nnew=%p\n",(void *)s,(void *)x);
      return 0;
    }

Output 2.2:

    status: -1
    Invalid or incomplete multibyte or wide character
    ori=0x55bb1aa98980
    new=0x55bb1aa98982


Thanks,
Igor



More information about the Libc-help mailing list