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. |
hidmic
left a comment
There was a problem hiding this comment.
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.
@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.
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); |
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.
@brawner probably preexistent, but:
| if (false == is_seq) { | |
| if (!is_seq) { |
? Same below.
There was a problem hiding this comment.
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.
@brawner perhaps unrelated, we can use memcpy instead.
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. |
| 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.
@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.
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.
@brawner if allocator.reallocate fails, cur_ns will leak.
| return RCUTILS_RET_BAD_ALLOC; | ||
| } | ||
| memmove((cur_ns + ns_len), sep_str, sep_len); | ||
| memmove((cur_ns + ns_len + sep_len), name, name_len); |
| } | ||
| 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.
@brawner same about cur_ns leaking on allocator.reallocate failure.
9a09bd2 to
b062da9
Compare
Signed-off-by: Stephen Brawner <brawner@gmail.com>
Signed-off-by: Stephen Brawner <brawner@gmail.com> squash! Reorder parser.c to match parser.h
Signed-off-by: Stephen Brawner <brawner@gmail.com>
Signed-off-by: Stephen Brawner <brawner@gmail.com>
Signed-off-by: Stephen Brawner <brawner@gmail.com>
Signed-off-by: Stephen Brawner <brawner@gmail.com>
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. |
* Fix yaml parser error when meets .nan Signed-off-by: Ada-King <Bingtao.Du@sony.com> Correct code style Signed-off-by: Ada-King <Bingtao.Du@sony.com> Modify as suggested Signed-off-by: Ada-King <Bingtao.Du@sony.com> Improve test Signed-off-by: Ada-King <Bingtao.Du@sony.com> Fix minor flaw Signed-off-by: Ada-King <Bingtao.Du@sony.com> Fix minor flaw again Signed-off-by: Ada-King <Bingtao.Du@sony.com> Satisfy windows CI Signed-off-by: Ada-King <Bingtao.Du@sony.com> Change the match rule for special float Signed-off-by: Ada-King <Bingtao.Du@sony.com> Distinguish +.inf and -.inf Signed-off-by: Ada-King <Bingtao.Du@sony.com> * Remove unnecessary #include change. Signed-off-by: Chris Lalancette <clalancette@openrobotics.org> * Add in two more necessary includes. Signed-off-by: Chris Lalancette <clalancette@openrobotics.org> Co-authored-by: Ada-King <Bingtao.Du@sony.com> Co-authored-by: Chris Lalancette <clalancette@openrobotics.org>
* Refactor rcl_yaml_param_parser for better testability Signed-off-by: Stephen Brawner <brawner@gmail.com> * Reorder parser.c to match parser.h Signed-off-by: Stephen Brawner <brawner@gmail.com> squash! Reorder parser.c to match parser.h * Refactor yaml_variant.c for deduplication Signed-off-by: Stephen Brawner <brawner@gmail.com> * ADD_VALUE_TO_SIMPLE_ARRAY for deduplication Signed-off-by: Stephen Brawner <brawner@gmail.com> * Remove fprintf Signed-off-by: Stephen Brawner <brawner@gmail.com> * PR Fixup Signed-off-by: Stephen Brawner <brawner@gmail.com> * Move headers to src directory Signed-off-by: Stephen Brawner <brawner@gmail.com> * PR Fixup Signed-off-by: Stephen Brawner <brawner@gmail.com> * Rebase #780 Signed-off-by: Stephen Brawner <brawner@gmail.com>
* Fix yaml parser error when meets .nan Signed-off-by: Ada-King <Bingtao.Du@sony.com> Correct code style Signed-off-by: Ada-King <Bingtao.Du@sony.com> Modify as suggested Signed-off-by: Ada-King <Bingtao.Du@sony.com> Improve test Signed-off-by: Ada-King <Bingtao.Du@sony.com> Fix minor flaw Signed-off-by: Ada-King <Bingtao.Du@sony.com> Fix minor flaw again Signed-off-by: Ada-King <Bingtao.Du@sony.com> Satisfy windows CI Signed-off-by: Ada-King <Bingtao.Du@sony.com> Change the match rule for special float Signed-off-by: Ada-King <Bingtao.Du@sony.com> Distinguish +.inf and -.inf Signed-off-by: Ada-King <Bingtao.Du@sony.com> * Remove unnecessary #include change. Signed-off-by: Chris Lalancette <clalancette@openrobotics.org> * Add in two more necessary includes. Signed-off-by: Chris Lalancette <clalancette@openrobotics.org> Co-authored-by: Ada-King <Bingtao.Du@sony.com> Co-authored-by: Chris Lalancette <clalancette@openrobotics.org>
* Refactor rcl_yaml_param_parser for better testability Signed-off-by: Stephen Brawner <brawner@gmail.com> * Reorder parser.c to match parser.h Signed-off-by: Stephen Brawner <brawner@gmail.com> squash! Reorder parser.c to match parser.h * Refactor yaml_variant.c for deduplication Signed-off-by: Stephen Brawner <brawner@gmail.com> * ADD_VALUE_TO_SIMPLE_ARRAY for deduplication Signed-off-by: Stephen Brawner <brawner@gmail.com> * Remove fprintf Signed-off-by: Stephen Brawner <brawner@gmail.com> * PR Fixup Signed-off-by: Stephen Brawner <brawner@gmail.com> * Move headers to src directory Signed-off-by: Stephen Brawner <brawner@gmail.com> * PR Fixup Signed-off-by: Stephen Brawner <brawner@gmail.com> * Rebase #780 Signed-off-by: Stephen Brawner <brawner@gmail.com>
* Refactor parser.c for better testability (#754) * Refactor rcl_yaml_param_parser for better testability Signed-off-by: Stephen Brawner <brawner@gmail.com> * Reorder parser.c to match parser.h Signed-off-by: Stephen Brawner <brawner@gmail.com> squash! Reorder parser.c to match parser.h * Refactor yaml_variant.c for deduplication Signed-off-by: Stephen Brawner <brawner@gmail.com> * ADD_VALUE_TO_SIMPLE_ARRAY for deduplication Signed-off-by: Stephen Brawner <brawner@gmail.com> * Remove fprintf Signed-off-by: Stephen Brawner <brawner@gmail.com> * PR Fixup Signed-off-by: Stephen Brawner <brawner@gmail.com> * Move headers to src directory Signed-off-by: Stephen Brawner <brawner@gmail.com> * PR Fixup Signed-off-by: Stephen Brawner <brawner@gmail.com> * Rebase #780 Signed-off-by: Stephen Brawner <brawner@gmail.com> * Missing includes Signed-off-by: Stephen Brawner <brawner@gmail.com>
* Fix yaml parser error when meets .nan Signed-off-by: Ada-King <Bingtao.Du@sony.com> Correct code style Signed-off-by: Ada-King <Bingtao.Du@sony.com> Modify as suggested Signed-off-by: Ada-King <Bingtao.Du@sony.com> Improve test Signed-off-by: Ada-King <Bingtao.Du@sony.com> Fix minor flaw Signed-off-by: Ada-King <Bingtao.Du@sony.com> Fix minor flaw again Signed-off-by: Ada-King <Bingtao.Du@sony.com> Satisfy windows CI Signed-off-by: Ada-King <Bingtao.Du@sony.com> Change the match rule for special float Signed-off-by: Ada-King <Bingtao.Du@sony.com> Distinguish +.inf and -.inf Signed-off-by: Ada-King <Bingtao.Du@sony.com> * Remove unnecessary #include change. Signed-off-by: Chris Lalancette <clalancette@openrobotics.org> * Add in two more necessary includes. Signed-off-by: Chris Lalancette <clalancette@openrobotics.org> Co-authored-by: Ada-King <Bingtao.Du@sony.com> Co-authored-by: Chris Lalancette <clalancette@openrobotics.org>
* Fix yaml parser error when meets .nan Signed-off-by: Ada-King <Bingtao.Du@sony.com> Correct code style Signed-off-by: Ada-King <Bingtao.Du@sony.com> Modify as suggested Signed-off-by: Ada-King <Bingtao.Du@sony.com> Improve test Signed-off-by: Ada-King <Bingtao.Du@sony.com> Fix minor flaw Signed-off-by: Ada-King <Bingtao.Du@sony.com> Fix minor flaw again Signed-off-by: Ada-King <Bingtao.Du@sony.com> Satisfy windows CI Signed-off-by: Ada-King <Bingtao.Du@sony.com> Change the match rule for special float Signed-off-by: Ada-King <Bingtao.Du@sony.com> Distinguish +.inf and -.inf Signed-off-by: Ada-King <Bingtao.Du@sony.com> * Remove unnecessary #include change. Signed-off-by: Chris Lalancette <clalancette@openrobotics.org> * Add in two more necessary includes. Signed-off-by: Chris Lalancette <clalancette@openrobotics.org> Co-authored-by: Ada-King <Bingtao.Du@sony.com> Co-authored-by: Chris Lalancette <clalancette@openrobotics.org> Signed-off-by: Stephen Brawner <brawner@gmail.com>
* Fix yaml parser error when meets .nan Signed-off-by: Ada-King <Bingtao.Du@sony.com> Correct code style Signed-off-by: Ada-King <Bingtao.Du@sony.com> Modify as suggested Signed-off-by: Ada-King <Bingtao.Du@sony.com> Improve test Signed-off-by: Ada-King <Bingtao.Du@sony.com> Fix minor flaw Signed-off-by: Ada-King <Bingtao.Du@sony.com> Fix minor flaw again Signed-off-by: Ada-King <Bingtao.Du@sony.com> Satisfy windows CI Signed-off-by: Ada-King <Bingtao.Du@sony.com> Change the match rule for special float Signed-off-by: Ada-King <Bingtao.Du@sony.com> Distinguish +.inf and -.inf Signed-off-by: Ada-King <Bingtao.Du@sony.com> * Remove unnecessary #include change. Signed-off-by: Chris Lalancette <clalancette@openrobotics.org> * Add in two more necessary includes. Signed-off-by: Chris Lalancette <clalancette@openrobotics.org> Co-authored-by: Ada-King <Bingtao.Du@sony.com> Co-authored-by: Chris Lalancette <clalancette@openrobotics.org> Signed-off-by: Stephen Brawner <brawner@gmail.com> Co-authored-by: Ada-King <Bingtao.Du@sony.com> Co-authored-by: Chris Lalancette <clalancette@openrobotics.org>
This PR separates out the
parser.cfunctionality 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.cto matchparser.hAddressing PR Feedback commits
srcdirectory