Skip to content

Commit

Permalink
Correctly handle return of getgrgid_r, check for NULL in gEnt
Browse files Browse the repository at this point in the history
fixes #51
  • Loading branch information
mgmarino authored and ljanyst committed Nov 27, 2013
1 parent afee627 commit 8341828
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/XrdOuc/XrdOucUtils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,12 @@ int XrdOucUtils::GroupName(gid_t gID, char *gName, int gNsz)
struct group *gEnt, gStruct;
char gBuff[1024], *gBp = gBuff;
int glen, gBsz = sizeof(gBuff), aOK = 1;
int retVal = 0;

// Get the the group struct. If we don't have a large enough buffer, get a
// larger one and try again up to the maximum buffer we will tolerate.
//
while((getgrgid_r(gID, &gStruct, gBp, gBsz, &gEnt) != 0) && errno == ERANGE)
while(( retVal = getgrgid_r(gID, &gStruct, gBp, gBsz, &gEnt) ) == ERANGE)
{if (gBsz >= maxgBsz) {aOK = 0; break;}
if (gBsz > addGsz) free(gBp);
gBsz += addGsz;
Expand All @@ -256,7 +257,7 @@ int XrdOucUtils::GroupName(gid_t gID, char *gName, int gNsz)

// Return a group name if all went well
//
if (aOK)
if (aOK && retVal == 0 && gEnt != NULL)
{glen = strlen(gEnt->gr_name);
if (glen >= gNsz) glen = 0;
else strcpy(gName, gEnt->gr_name);
Expand Down

0 comments on commit 8341828

Please sign in to comment.