Skip to content

Fix Issue #4047 Check return codes and free objects#4059

Closed
ghost wants to merge 1 commit into
masterfrom
unknown repository
Closed

Fix Issue #4047 Check return codes and free objects#4059
ghost wants to merge 1 commit into
masterfrom
unknown repository

Conversation

@ghost

@ghost ghost commented Jan 1, 2017

Copy link
Copy Markdown

Prior to this fix config_files() in examples/general.c produced random / incorrect values for "Autocorrect" in its output.

Prior to this fix config_files() did not free its config objects.

Comment thread examples/general.c
git_config_get_string(&email, snap_cfg, "user.email");
printf("Email: %s\n", email);
error_code = git_config_get_string(&email, snap_cfg, "user.email");
switch (error_code)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Instead of copy/pasting this, a macro might clean this up nicely...?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I did cut and paste tge fragments starting with the "switch". A macro would be a nice way to avoid the duplication, but it will have to take a number of parameters:

  1. The displayed name of the variable (Autocorrect or Email).
  2. The variable's type (%d or %s).
  3. The value of the variable.
  4. The error message for check_error().
  5. Maybe "error_code" so the user of the macro could use any name for this variable.

I'll be glad to add a macro if you think that's nicer.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I was curious how your proposed macro would look so I coded it. I enclose below the macro and its invocations. Is this better than the original?

#define GIT_CFG_ERR_CHK(display_name, format, variable, error_msg, error_code) \
    switch (error_code) \
    { \
        case 0: \
            printf(display_name ": " format "\n", variable); \
            break; \
        case GIT_ENOTFOUND: \
            printf(display_name ": Undefined\n"); \
            break; \
        default: \
            check_error(error_code, error_msg); \
	}

    error_code = git_config_get_int32(&j, cfg, "help.autocorrect");
    GIT_CFG_ERR_CHK("Autocorrect", "%d", j, "get_int32 failed", error_code)

    error_code = git_config_get_string(&email, snap_cfg, "user.email");
    GIT_CFG_ERR_CHK("Email", "%s", email, "get_string failed", error_code)

@ghost ghost mentioned this pull request Jan 12, 2017
@ethomson

ethomson commented Oct 7, 2017

Copy link
Copy Markdown
Member

Sorry, I should have merged this long ago. I don't love the copypasta, but it's superior to the macro for code clarity in an example, I think.

Thanks for the contribution!

@ethomson

ethomson commented Oct 7, 2017

Copy link
Copy Markdown
Member

Merged via #4370

@ethomson ethomson closed this Oct 7, 2017
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.

1 participant