I have a char pointer that points to a buffer of unknown length:
int N = <something>;
if (N != 0)
You can use
memchr to look for the zero byte, and limit the search to N bytes:
size_t N = ...; char* pos = memchr(buffer, '\0', N); text.assign(buffer, pos ? pos - buffer : N);
If the byte is not found, the length of the output string should be N. If it's found, then
memstr returns the pointer to it. Subtracting the original pointer will yield the desired length.
This doesn't handle the case of N = 0 in any special manner - that is, if N is 0, the output will be empty. If you want other behavior, make a special-case for it.