RFC: mblen() return code when n is zero

Rajalakshmi Srinivasaraghavan raji@linux.vnet.ibm.com
Tue Nov 11 05:46:00 GMT 2014


The standard document for mblen() states:

#include <stdlib.h>
int mblen(const char *s, size_t n);

The standard states:
"If s is a null pointer, the mblen function returns a nonzero or zero value, if multibyte character encodings, respectively,
  do or do not have state-dependent encodings. If s is not a null pointer, the mblen function either returns 0
(if s points to the null character), or returns the number of bytes that are contained in the multibyte character
(if the next n or fewer bytes form a valid multibyte character), or returns -1 (if they do not form a valid multibyte character). "


The case n=0 is not explicitly mentioned in the standards document and is also not handled in the code(stdlib/mblen.c)
When compiled on AIX (v6.1 and other releases) mblen() returns -1.
The return code 0 and -1 can both be justified when n=0.
1.For the case *s =='\0' we return just zero and not -1. so if we consider this point, rc 0 holds good for n=0.
2.If we read the standard as "if they do not form a valid multibyte character" rc can be -1 for n=0.

$ cat mblen_t.c
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

main() {
      char sbcs_st[]  = "12345";

      if ( setlocale( LC_ALL, "" ) == NULL ) {
         printf( "setlocale() failed\n" );
      }
      printf("RC is %d\n", mblen(sbcs_st, 0));
}

$ gcc -o mblen_t mblen_t.c;./mblen_t
RC is 0

What should be the correct return code when n=0?

-- 
Thanks
Rajalakshmi S



More information about the Libc-alpha mailing list