-
Notifications
You must be signed in to change notification settings - Fork 167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor parser.c for better testability #754
Conversation
@anup-pem do you have time to review this pull request since you originally wrote this? It would help us make sure it doesn’t change the code in a way that you didn’t intend or that might cause apex issues in the future. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall LGTM, though I didn't try to mix and match new and old lines. I'll try again next week, but I guess our best chance of not introducing regressions lies with tests.
RCUTILS_SAFE_FWRITE_TO_STDERR("Error allocating mem\n"); \ | ||
return RCUTILS_RET_BAD_ALLOC; \ | ||
} \ | ||
memmove(val_array->values, tmp_arr, (val_array->size * sizeof(value_type))); \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brawner perhaps unrelated to this PR, but we can use memcpy
instead of memmove
. There's no way val_array->values
and tmp_arr
can overlap.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Switched to memcpy
rcl_yaml_param_parser/src/parse.c
Outdated
|
||
rcl_variant_t * param_value = &(params_st->params[node_idx].parameter_values[parameter_idx]); | ||
|
||
// param_value->string_value = rcutils_strdup(value, allocator); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brawner why keep this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
rcl_yaml_param_parser/src/parse.c
Outdated
ret = RCUTILS_RET_ERROR; | ||
break; | ||
case DATA_TYPE_BOOL: | ||
if (false == is_seq) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brawner probably preexistent, but:
if (false == is_seq) { | |
if (!is_seq) { |
? Same below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely preexistent. Switched
rcl_yaml_param_parser/src/parse.c
Outdated
break; | ||
} | ||
|
||
memmove(param_name, parameter_ns, params_ns_len); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brawner perhaps unrelated, we can use memcpy
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Switched to memcpy
84661d0
to
28a8568
Compare
28a8568
to
3ff206f
Compare
Rebased this onto master after recent PR merges. I updated the commits in the description at the top of this PR if the reviews would rather step through the changes that way. They make it easier to see what changes and what doesn't. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, comments are minor or can be addressed in another PR.
if (NULL == val_array->values) { \ | ||
val_array->values = tmp_arr; \ | ||
RCUTILS_SAFE_FWRITE_TO_STDERR("Error allocating mem\n"); \ | ||
return RCUTILS_RET_BAD_ALLOC; \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brawner nit: if ADD_VALUE_TO_SIMPLE_ARRAY
took ownership of value
and deallocated it in failure cases, add_val_to_*
API callers wouldn't have to add a trailing nullity check and deallocation every time one of those functions fails.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The callers already have to handle deallocation of value in several instances. The logic is complicated and this change will lead to larger changes, which I think are out of scope for just adding coverage to this package.
|
||
tot_len = ns_len + sep_len + name_len + 1U; | ||
|
||
cur_ns = allocator.reallocate(cur_ns, tot_len, allocator.state); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brawner if allocator.reallocate
fails, cur_ns
will leak.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moving to #780
return RCUTILS_RET_BAD_ALLOC; | ||
} | ||
memmove((cur_ns + ns_len), sep_str, sep_len); | ||
memmove((cur_ns + ns_len + sep_len), name, name_len); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brawner same about memcpy
being cheaper.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
} | ||
if (NULL != last_idx) { | ||
tot_len = ((size_t)(last_idx - cur_ns) + 1U); | ||
cur_ns = allocator.reallocate(cur_ns, tot_len, allocator.state); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brawner same about cur_ns
leaking on allocator.reallocate
failure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
9a09bd2
to
b062da9
Compare
Signed-off-by: Stephen Brawner <[email protected]>
Signed-off-by: Stephen Brawner <[email protected]> squash! Reorder parser.c to match parser.h
Signed-off-by: Stephen Brawner <[email protected]>
Signed-off-by: Stephen Brawner <[email protected]>
Signed-off-by: Stephen Brawner <[email protected]>
Signed-off-by: Stephen Brawner <[email protected]>
Signed-off-by: Stephen Brawner <[email protected]>
Signed-off-by: Stephen Brawner <[email protected]>
Signed-off-by: Stephen Brawner <[email protected]>
b062da9
to
8d72dc4
Compare
Rebasing onto master after merging #780. Testing one last time just because it changes a lot of stuff |
All ci.ros2.org tests pass, and tests pass with rpr job. The failures in the Rpr job are unrelated to this PR and have already been addressed in PRs to rcl I believe. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm (post merge)
* Fix yaml parser error when meets .nan Signed-off-by: Ada-King <[email protected]> Correct code style Signed-off-by: Ada-King <[email protected]> Modify as suggested Signed-off-by: Ada-King <[email protected]> Improve test Signed-off-by: Ada-King <[email protected]> Fix minor flaw Signed-off-by: Ada-King <[email protected]> Fix minor flaw again Signed-off-by: Ada-King <[email protected]> Satisfy windows CI Signed-off-by: Ada-King <[email protected]> Change the match rule for special float Signed-off-by: Ada-King <[email protected]> Distinguish +.inf and -.inf Signed-off-by: Ada-King <[email protected]> * Remove unnecessary #include change. Signed-off-by: Chris Lalancette <[email protected]> * Add in two more necessary includes. Signed-off-by: Chris Lalancette <[email protected]> Co-authored-by: Ada-King <[email protected]> Co-authored-by: Chris Lalancette <[email protected]>
* Refactor rcl_yaml_param_parser for better testability Signed-off-by: Stephen Brawner <[email protected]> * Reorder parser.c to match parser.h Signed-off-by: Stephen Brawner <[email protected]> squash! Reorder parser.c to match parser.h * Refactor yaml_variant.c for deduplication Signed-off-by: Stephen Brawner <[email protected]> * ADD_VALUE_TO_SIMPLE_ARRAY for deduplication Signed-off-by: Stephen Brawner <[email protected]> * Remove fprintf Signed-off-by: Stephen Brawner <[email protected]> * PR Fixup Signed-off-by: Stephen Brawner <[email protected]> * Move headers to src directory Signed-off-by: Stephen Brawner <[email protected]> * PR Fixup Signed-off-by: Stephen Brawner <[email protected]> * Rebase #780 Signed-off-by: Stephen Brawner <[email protected]>
* Fix yaml parser error when meets .nan Signed-off-by: Ada-King <[email protected]> Correct code style Signed-off-by: Ada-King <[email protected]> Modify as suggested Signed-off-by: Ada-King <[email protected]> Improve test Signed-off-by: Ada-King <[email protected]> Fix minor flaw Signed-off-by: Ada-King <[email protected]> Fix minor flaw again Signed-off-by: Ada-King <[email protected]> Satisfy windows CI Signed-off-by: Ada-King <[email protected]> Change the match rule for special float Signed-off-by: Ada-King <[email protected]> Distinguish +.inf and -.inf Signed-off-by: Ada-King <[email protected]> * Remove unnecessary #include change. Signed-off-by: Chris Lalancette <[email protected]> * Add in two more necessary includes. Signed-off-by: Chris Lalancette <[email protected]> Co-authored-by: Ada-King <[email protected]> Co-authored-by: Chris Lalancette <[email protected]>
* Refactor rcl_yaml_param_parser for better testability Signed-off-by: Stephen Brawner <[email protected]> * Reorder parser.c to match parser.h Signed-off-by: Stephen Brawner <[email protected]> squash! Reorder parser.c to match parser.h * Refactor yaml_variant.c for deduplication Signed-off-by: Stephen Brawner <[email protected]> * ADD_VALUE_TO_SIMPLE_ARRAY for deduplication Signed-off-by: Stephen Brawner <[email protected]> * Remove fprintf Signed-off-by: Stephen Brawner <[email protected]> * PR Fixup Signed-off-by: Stephen Brawner <[email protected]> * Move headers to src directory Signed-off-by: Stephen Brawner <[email protected]> * PR Fixup Signed-off-by: Stephen Brawner <[email protected]> * Rebase #780 Signed-off-by: Stephen Brawner <[email protected]>
* Refactor parser.c for better testability (#754) * Refactor rcl_yaml_param_parser for better testability Signed-off-by: Stephen Brawner <[email protected]> * Reorder parser.c to match parser.h Signed-off-by: Stephen Brawner <[email protected]> squash! Reorder parser.c to match parser.h * Refactor yaml_variant.c for deduplication Signed-off-by: Stephen Brawner <[email protected]> * ADD_VALUE_TO_SIMPLE_ARRAY for deduplication Signed-off-by: Stephen Brawner <[email protected]> * Remove fprintf Signed-off-by: Stephen Brawner <[email protected]> * PR Fixup Signed-off-by: Stephen Brawner <[email protected]> * Move headers to src directory Signed-off-by: Stephen Brawner <[email protected]> * PR Fixup Signed-off-by: Stephen Brawner <[email protected]> * Rebase #780 Signed-off-by: Stephen Brawner <[email protected]> * Missing includes Signed-off-by: Stephen Brawner <[email protected]>
* Fix yaml parser error when meets .nan Signed-off-by: Ada-King <[email protected]> Correct code style Signed-off-by: Ada-King <[email protected]> Modify as suggested Signed-off-by: Ada-King <[email protected]> Improve test Signed-off-by: Ada-King <[email protected]> Fix minor flaw Signed-off-by: Ada-King <[email protected]> Fix minor flaw again Signed-off-by: Ada-King <[email protected]> Satisfy windows CI Signed-off-by: Ada-King <[email protected]> Change the match rule for special float Signed-off-by: Ada-King <[email protected]> Distinguish +.inf and -.inf Signed-off-by: Ada-King <[email protected]> * Remove unnecessary #include change. Signed-off-by: Chris Lalancette <[email protected]> * Add in two more necessary includes. Signed-off-by: Chris Lalancette <[email protected]> Co-authored-by: Ada-King <[email protected]> Co-authored-by: Chris Lalancette <[email protected]>
* Fix yaml parser error when meets .nan Signed-off-by: Ada-King <[email protected]> Correct code style Signed-off-by: Ada-King <[email protected]> Modify as suggested Signed-off-by: Ada-King <[email protected]> Improve test Signed-off-by: Ada-King <[email protected]> Fix minor flaw Signed-off-by: Ada-King <[email protected]> Fix minor flaw again Signed-off-by: Ada-King <[email protected]> Satisfy windows CI Signed-off-by: Ada-King <[email protected]> Change the match rule for special float Signed-off-by: Ada-King <[email protected]> Distinguish +.inf and -.inf Signed-off-by: Ada-King <[email protected]> * Remove unnecessary #include change. Signed-off-by: Chris Lalancette <[email protected]> * Add in two more necessary includes. Signed-off-by: Chris Lalancette <[email protected]> Co-authored-by: Ada-King <[email protected]> Co-authored-by: Chris Lalancette <[email protected]> Signed-off-by: Stephen Brawner <[email protected]>
* Fix yaml parser error when meets .nan Signed-off-by: Ada-King <[email protected]> Correct code style Signed-off-by: Ada-King <[email protected]> Modify as suggested Signed-off-by: Ada-King <[email protected]> Improve test Signed-off-by: Ada-King <[email protected]> Fix minor flaw Signed-off-by: Ada-King <[email protected]> Fix minor flaw again Signed-off-by: Ada-King <[email protected]> Satisfy windows CI Signed-off-by: Ada-King <[email protected]> Change the match rule for special float Signed-off-by: Ada-King <[email protected]> Distinguish +.inf and -.inf Signed-off-by: Ada-King <[email protected]> * Remove unnecessary #include change. Signed-off-by: Chris Lalancette <[email protected]> * Add in two more necessary includes. Signed-off-by: Chris Lalancette <[email protected]> Co-authored-by: Ada-King <[email protected]> Co-authored-by: Chris Lalancette <[email protected]> Signed-off-by: Stephen Brawner <[email protected]> Co-authored-by: Ada-King <[email protected]> Co-authored-by: Chris Lalancette <[email protected]>
This PR separates out the
parser.c
functionality into separate source files with their own headers so additional unit tests can be added to target specific functions. I still need to create the additional unit tests, but this current refactor passes the tests that currently exist, which has about 88% coverage.Because this PR is so large, I've separated the changes into logical commits. Reviewing each commit individually might be the easiest way to scan through this PR as they illustrate the copy-paste changes I did. They could be their own PRs if that's desired by the reviewers.
Main commits
parser.c
to matchparser.h
Addressing PR Feedback commits
src
directory