Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

pks-t
Copy link
Member

@pks-t pks-t commented Nov 12, 2017

Two small cleanups to make code more readable in our config parser.

@pks-t pks-t requested a review from ethomson November 12, 2017 14:12
git__free(var_value);
git__strtolower(name);
git_buf_printf(&buf, "%s.%s", current_section, name);
git__free(name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems disappointing to have to do an extra tiny malloc here for name n every variable that we read and immediately free it. Doing a little bit more manipulation and not doing a printf here might help. eg:

git_buf_puts(&buf, current_section);
git_buf_putc(&buf, '.');

for (c = name, *c; c++)
    git_buf_putc(&buf, git__tolower(*c));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Your solution looks a lot cleaner, thanks!

@pks-t pks-t force-pushed the pks/config-parse-cleanups branch from a0c5029 to 81da513 Compare January 3, 2018 12:39
@pks-t
Copy link
Member Author

pks-t commented Jan 3, 2018

Improved as to @ethomson's proposal


if (git_buf_oom(&buf)) {
git__free(var_value);
git_buf_free(&buf);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to free an OOM'd buffer. If the realloc fails, we free the buffer we had before setting the buffer to the OOM one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, obviously. I've created a new commit for that, as there is also a second occasion where we had the same pattern nearby. Thanks for pointing out.

@pks-t pks-t force-pushed the pks/config-parse-cleanups branch from a2bff86 to a59bb70 Compare January 26, 2018 14:17
@pks-t pks-t force-pushed the pks/config-parse-cleanups branch from a59bb70 to c3b4e1d Compare May 30, 2018 09:03
@pks-t
Copy link
Member Author

pks-t commented May 30, 2018

Rebased to fix conflicts

pks-t added 3 commits June 22, 2018 09:44
The function `git_config_parse` uses several callbacks to pass data
along to the caller as it parses the file. One design shortcoming here
is that strings passed to those callbacks are expected to be freed by
them, which is really confusing.

Fix the issue by changing memory ownership here. Instead of expecting
the `on_variable` callbacks to free memory for `git_config_parse`, just
do it inside of `git_config_parse`. While this obviously requires a bit
more memory allocation churn due to having to copy both name and value
at some places, this shouldn't be too much of a burden.
The `parse_variable` function has two out parameters `var_name` and
`var_value`. Currently, those are not being sanitized to `NULL`. when.
any error happens inside of the `parse_variable` function. Fix that.
While at it, the coding style is improved to match our usual coding
practices more closely.
Buffers which ran out of memory will never have any memory attached to
them. As such, it is not necessary to call `git_buf_free` if the buffer
is out of memory.
@pks-t pks-t force-pushed the pks/config-parse-cleanups branch from c3b4e1d to e1e90dc Compare June 22, 2018 07:48
@pks-t
Copy link
Member Author

pks-t commented Jun 22, 2018

Rebased again to fix conflicts

@ethomson ethomson merged commit b121b7a into libgit2:master Jun 22, 2018
@ethomson
Copy link
Member

🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants