Handle readlink() failure in genCpioListAndHeader() correctly

It might be a rare condition but it /can/ happen, and previously that would cause '\0' written out of bounds (at negative array offset). Also leave room for the terminating '\0' at the end of buffer when calling readlink(), previously a link exactly the size of buffer would've been silently truncated.

(cherry picked from rpm.org commit c499a0b912629eeeabc3554d3e3a95c088e96422)
This commit is contained in:
Panu Matilainen 2011-05-27 09:02:38 +03:00 committed by Dmitry V. Levin
parent 7bae988692
commit 308bc34ea9

View File

@ -1183,17 +1183,25 @@ static void genCpioListAndHeader(Spec spec, /*@partial@*/ FileList fl,
buf[0] = '\0';
if (S_ISLNK(flp->fl_mode)) {
buf[Readlink(flp->diskURL, buf, BUFSIZ)] = '\0';
if (fl->buildRootURL) {
const char * buildRoot;
(void) urlPath(fl->buildRootURL, &buildRoot);
ssize_t llen = readlink(flp->diskURL, buf, BUFSIZ-1);
if (llen < 0) {
rpmError(RPMERR_BADFILENAME,
_("reading symlink %s failed: %s\n"),
flp->diskURL, strerror(errno));
fl->processingFailed = 1;
} else {
buf[llen] = '\0';
if (fl->buildRootURL) {
const char * buildRoot;
(void) urlPath(fl->buildRootURL, &buildRoot);
if (buf[0] == '/' && strcmp(buildRoot, "/") &&
!strncmp(buf, buildRoot, strlen(buildRoot))) {
rpmError(RPMERR_BADSPEC,
_("Symlink points to BuildRoot: %s -> %s\n"),
flp->fileURL, buf);
fl->processingFailed = 1;
if (buf[0] == '/' && strcmp(buildRoot, "/") &&
!strncmp(buf, buildRoot, strlen(buildRoot))) {
rpmError(RPMERR_BADSPEC,
_("Symlink points to BuildRoot: %s -> %s\n"),
flp->fileURL, buf);
fl->processingFailed = 1;
}
}
}
}