Skip to content

Commit

Permalink
CID 702036: catattr: close file descriptor after usage
Browse files Browse the repository at this point in the history
  • Loading branch information
stpere committed Jul 2, 2015
1 parent bb4ab43 commit e2a8ef1
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/bin/catattr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ catAttr(const char *attribute, const char *fileName, bool keepRaw,
return errno;

attr_info info;
if (fs_stat_attr(fd, attribute, &info) < 0)
if (fs_stat_attr(fd, attribute, &info) < 0) {
close(fd);
return errno;
}

// limit size of the attribute, only the first 64k will make it on screen
off_t size = info.size;
Expand All @@ -156,19 +158,22 @@ catAttr(const char *attribute, const char *fileName, bool keepRaw,
char* buffer = (char*)malloc(size);
if (!buffer) {
fprintf(stderr, "Could not allocate read buffer!\n");
close(fd);
return B_NO_MEMORY;
}

ssize_t bytesRead = fs_read_attr(fd, attribute, info.type, 0, buffer, size);
if (bytesRead < 0) {
free(buffer);
close(fd);
return errno;
}

if (bytesRead != size) {
fprintf(stderr, "Could only read %ld bytes from attribute!\n",
bytesRead);
free(buffer);
close(fd);
return B_ERROR;
}

Expand Down Expand Up @@ -210,6 +215,7 @@ catAttr(const char *attribute, const char *fileName, bool keepRaw,
free(buffer);
if (written > 0)
written = B_OK;
close(fd);
return written;
}

Expand Down Expand Up @@ -275,6 +281,7 @@ catAttr(const char *attribute, const char *fileName, bool keepRaw,
}

free(buffer);
close(fd);
return B_OK;
}

Expand Down

0 comments on commit e2a8ef1

Please sign in to comment.