Repository navigation
Use ?: to chain comparison functions (for now, in lldp only) - #11140
Conversation
why a macro? could be a regular function. memcmp_shorter_wins() or so? sounds useful to have. I like |
hmm, actually plain |
|
Trying to figure out why the test case has a memleak in it... Any ideas? I was staring at the code but still can't see it... |
+1
How about the following? |
|
My idea was: Which should be a bit safer if by any chance it's passed integers-as-booleans... Also, to the reader, it's clear that a comparison of booleans was actually needed. Yes, function for |
|
No, I don't think that's it: I think a neighbor may end up not being actually free'd if it's not |
|
I ended up going with The memory leak is probably still there... Not really sure what's going on there. Running this test under valgrind repeatedly shows a different LLDP neighbor (or sometimes more than one) being leaked each time, so maybe there's a race condition somewhere? |
poettering
left a comment
There was a problem hiding this comment.
Looks great. One minor nitpick though.
…ghbors In particular, check that the order of the results is consistent. This test coverage will be useful in order to refactor the compare_func used while sorting the results. Tested: ninja -C build/ test
The ?: operator is very useful for chaining comparison functions (strcmp, memcmp, CMP), since its behavior is to return the result of the comparison function call if non-zero, or continue evaluating the chain of comparison functions. This simplifies the code in that using a temporary `r` variable to store the function results is no longer necessary and the checks for non-zero to return are no longer needed either, resulting in a typical three-fold reduction to the number of lines in the code. Introduce a new memcmp_nn() to compare two memory buffers in lexicographic order, taking length in consideration. Tested: $ ninja -C build/ test All test cases pass. In particular, test_multiple_neighbors_sorted() in test-lldp would catch regressions introduced by this commit.
|
Still looking at the memory leak... It seems there's a bug somewhere in hashmap implementation, at least I can see that |
|
Ok I figured out the memory leak... The problem is that I'll update the destructor to use a function that does all but call |
|
Fix for the memory leak in #11163. I'll come back to this one (keeping just the memcmp_nn part) after that one is in. |
|
@poettering I'm wondering why you keep merging PRs that break the CI. |
|
My bad, I should have marked this as "do-not-merge" until #11163 was resolved... Let's work on that one to fix the memory leak. |
|
I'm sorry. I didn't mean to be grumpy. Actually I'm glad you added the test that uncovered the memory leak. Thank you! |
|
@evverx You do have a point though! In this case, this test being in means all further commits would break for that same reason. I just filed #11172 to revert the test case until the memory leak is fixed (keeping the I think part of the problem is sometimes it's hard to tell real failures from flakes... Maybe something we need to address. I'll open a separate issue about that. Thanks! |
|
@filbranden thank you! I've just restarted all the jobs that failed manually. https://travis-ci.org/systemd/systemd/pull_requests should become less noisy soon.
That's true but it's usually applicable to Ubuntu CI (which seems to be abandoned). Travis CI and CentOS CI are more or less maintained actively (that is the tests that are flaky are skipped there if they can't be fixed for some reason: systemd/systemd-centos-ci#28, failed jobs are restarted and so on). |
|
Interestingly, the |
This is in some ways a follow up to #9789 and part of the discussion we had there.
It turns out the
?:operator in C does just what we want. It keeps the result if non-zero, otherwise keeps evaluating the next calls on the chain.I started with this simple PR that just uses it in one place, but if this looks acceptable, I'd like to refactor more of the compare_func's to use this.
Another idea is to simplify
memcmp()+ aCMP()for the lengths into a single macro that takes two pointers and two lengths (not sure about the name...BINCMP?BSTRCMP?BUFCMP?MEMCMP)Also, one for booleans
BOOLCMP()would make sense, to convert code like this:into:
(I'm not 100% whether
!=or==is better here, but you get the idea...)Anyways, starting with a simple one to get the ball rolling, we can get the next ones done later.
Cheers,
Filipe