posix: fix confstr

* Off-by-one error copying the string.
* As per the spec, return the buffer length for the string when passed a
  null pointer and 0 length.

Fixes https://github.com/haikuports/haikuports/issues/5821

Change-Id: Ic421f26db00f9820c6a617375e39f7341cd5ebc1
Reviewed-on: https://review.haiku-os.org/c/haiku/+/4110
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
Reviewed-by: Adrien Destugues <pulkomandy@gmail.com>
This commit is contained in:
Máximo Castañeda 2021-06-23 17:49:04 +02:00 committed by Adrien Destugues
parent 208a3535b0
commit 4c23dfb3a3

View File

@ -384,14 +384,8 @@ pathconf(const char *path, int name)
size_t
confstr(int name, char *buffer, size_t length)
{
size_t stringLength = 0;
const char *string = "";
if (!length || !buffer) {
__set_errno(EINVAL);
return 0;
}
switch (name) {
case _CS_PATH:
string = kSystemNonpackagedBinDirectory ":" kGlobalBinDirectory ":"
@ -402,13 +396,10 @@ confstr(int name, char *buffer, size_t length)
return 0;
}
if (buffer != NULL) {
stringLength = strlen(string) + 1;
strlcpy(buffer, string,
min_c(length - 1, stringLength));
}
if (buffer != NULL)
strlcpy(buffer, string, length);
return stringLength;
return strlen(string) + 1;
}