Update the rcutils_find* APIs to return SIZE_MAX on invalid arguments.#74
Update the rcutils_find* APIs to return SIZE_MAX on invalid arguments.#74clalancette merged 6 commits intomasterfrom
Conversation
Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
dhood
left a comment
There was a problem hiding this comment.
there's inconsistency when the string is valid but empty. In most cases string_length is returned if the delimiter is not found, but in the case of "" as the input string, SIZE_MAX is being returned when the delimiter is not found. from #58 it sounds like SIZE_MAX should be returned in all cases
src/find.c
Outdated
| { | ||
| if (!str) { | ||
| return 0; | ||
| if (!str || strlen(str) == 0) { |
There was a problem hiding this comment.
I don't think this change makes sense for findn or find_lastn below because it accepts strings that aren't null terminated
There was a problem hiding this comment.
Oh yeah, you are definitely right about that. That should be a check if string_length == 0. I'll fix that.
There was a problem hiding this comment.
Nit: please use 0 == string_length when updating
|
btw if we update to return Line 473 in 77a7e9e |
|
Dismissing my review, and +1ing both @dhood's review about non-null terminated strings.
Covered by previous review |
That was sort of intentional; I was trying to make it so that you could determine whether the inputs were valid (SIZE_MAX if not), and whether you found the delimiter (offset of the delimiter, or length of entire string if not). Now that you mention it, though, an "empty" string could be considered a special case of "can't find delimiter". I'll take another look and see what happens if we change to returning SIZE_MAX for can't find delimiter, in all cases.
So I actually did audit these callers. I opted not to make any changes in both of those cases in |
Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
In particular, have it go looking from the end, not the beginning, which will cause it to get out early once it has found something. Note that we have to have a separate check for the 0th element, since we can't do a <= 0 inside of the for loop with an unsigned variable. Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
|
It looks like it is reasonable to use SIZE_MAX for all cases of "not found", so I've changed to that. Let me know what you think. |
| size_t ret0 = test_findn("", '/', 0, 0); | ||
| LOG((size_t)0, ret0); | ||
| size_t ret0 = test_findn("", '/', 0, SIZE_MAX); | ||
| LOG((size_t)SIZE_MAX, ret0); |
There was a problem hiding this comment.
the size_t casts are unnecessary here and below now yeah?
There was a problem hiding this comment.
Yeah, for the SIZE_MAX checks, that's probably right. I'll make that fix and submit a CI just to be sure.
There was a problem hiding this comment.
I wasn't aware of this but judging from the warnings on OS X SIZE_MAX may not fit into size_t.. this probably means we'll need to reconsider the return type of the find functions if we want to take this approach
There was a problem hiding this comment.
I wasn't aware of this but judging from the warnings on OS X SIZE_MAX may not fit into size_t.. this probably means we'll need to reconsider the return type of the find functions if we want to take this approach
Arg, that is annoying. I'll do a little more investigating to see what I can find here.
There was a problem hiding this comment.
I see what is going on. The warning that clang is throwing is very misleading. On x86_64 macOS, size_t is a long unsigned int, which is 64-bit unsigned. SIZE_MAX is an unsigned long long, which is also 64-bit unsigned. The problem is that the LOG statement is using %zu to print out the values, and an unsigned long long is not technically a size_t, even though they are compatible. The solution is to make sure we still cast SIZE_MAX to a size_t before passing it to LOG (like what was there before). I'll fix that, and put a comment in there for the next hapless soul who tries to remove it.
Yep you're right (I think I'd remembered they'd have issues if we returned -1 when it was originally suggested, but they are indeed good as is) |
Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
Otherwise, macOS complains. Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
|
Thanks everyone for the reviews. I'm going to merge this now. |
Signed-off-by: Chris Lalancette clalancette@openrobotics.org
fixes #58
CI: