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

Skip to content

Few p_getaddrinfo fixes #3567

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

Merged
merged 2 commits into from
Feb 29, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ int p_getaddrinfo(
ai = ainfo;

for (p = 1; ainfo->ai_hostent->h_addr_list[p] != NULL; p++) {
ai->ai_next = malloc(sizeof(struct addrinfo));
memcpy(&ai->ai_next, ainfo, sizeof(struct addrinfo));
if (!(ai->ai_next = malloc(sizeof(struct addrinfo)))) {
Copy link
Member

Choose a reason for hiding this comment

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

We should be using git__malloc() here, or even git__calloc(). And yes, we should just bail out with the macro.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Am 2015-12-31 16:19, schrieb Carlos Martín Nieto:

In src/posix.c [1]:

@@ -62,8 +62,11 @@ int p_getaddrinfo(
ai = ainfo;

 for (p = 1; ainfo->ai_hostent->h_addr_list[p] != NULL; p++) {
  •    ai->ai_next = malloc(sizeof(struct addrinfo));
    
  •    memcpy(&ai->ai_next, ainfo, sizeof(struct addrinfo));
    
  •    if (!(ai->ai_next = malloc(sizeof(struct addrinfo)))) {
    

We should be using git__malloc() here, or even git__calloc(). And yes,
we should just bail out with the macro.

Note however that this is a replacement function for getaddrinfo() that
also doesn't follow libgit2 semantics. For instance, GITERR_CHECK_ALLOC
will not clean up properly while the standard getaddrinfo() is supposed
to cleanup properly in a failure case. So should this really being used?
In posix.c there is quite a mixture of styles so it is not clear what
style should be really used. But if you say git__malloc() and
GITERR_CHECK_ALLOC then I will adjust my changes.

Copy link
Member

Choose a reason for hiding this comment

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

It's not 100% expected that the p_* functions that we write should set library error codes. These are often used as fallbacks when an actual posix function is not available. And - of course - the actual posix functions (like the system's getaddrinfo) will never set the library error codes.

Therefore p_getaddrinfo doesn't need to set the library error code. Instead, it needs to set something that p_gai_strerror can cope with (which is our reimplementation of gai_strerror. Indeed that does so - -1 is treated as an OOM scenario.

As a result, I think this change is correct.

p_freeaddrinfo(ainfo);
return -1;
}
memcpy(ai->ai_next, ainfo, sizeof(struct addrinfo));
memcpy(&ai->ai_next->ai_addr_in.sin_addr,
ainfo->ai_hostent->h_addr_list[p],
ainfo->ai_hostent->h_length);
Expand Down