This is an archive of the discontinued LLVM Phabricator instance.

[libc] Add strlcpy
ClosedPublic

Authored by abrachet on May 17 2022, 9:31 AM.

Diff Detail

Event Timeline

abrachet created this revision.May 17 2022, 9:31 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 17 2022, 9:31 AM
abrachet requested review of this revision.May 17 2022, 9:31 AM

Overall the patch looks good, I left some comments for the main code but the build system changes are good.

libc/src/string/strlcpy.cpp
24

it doesn't seem like defining a min function is necessary here since it's only used once, I think it would be cleaner to write

size_t len = internal::string_length(src)
size_t n = len < size - 1 ? len : size - 1;

This also helps for the return value.

27

we're trying to keep entrypoints independent, meaning that they shouldn't include each other. In this case using inline_memset(reinterpret_cast<char *>(dst + n), 0, size - n); would be preferred.

28

The return value of strlcpy is "the total length of the string they tried to create. For strlcpy() that means the length of src."
This means that the return value should be len and not n.

abrachet updated this revision to Diff 430212.May 17 2022, 5:20 PM
abrachet marked 3 inline comments as done.
This revision is now accepted and ready to land.May 18 2022, 10:05 AM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptMay 18 2022, 10:45 AM