From 99d00350b7aea245c014926d0f7883162eefb11a Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Fri, 13 Jan 2012 16:21:11 +0200 Subject: [PATCH] Change fsmReadLink() to take "normal" arguments - This doesn't need access to the entire fsm, it just needs a buffer to place the results in / return errors. Currently the "out" buffer is (ab)used for the results, this just forces that to stand out and should make it easier to sanitize later. --- lib/fsm.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/lib/fsm.c b/lib/fsm.c index a465410..fe787c1 100644 --- a/lib/fsm.c +++ b/lib/fsm.c @@ -830,19 +830,22 @@ exit: return rc; } -static int fsmReadLink(FSM_t fsm) +static int fsmReadLink(const char *path, + char *buf, size_t bufsize, size_t *linklen) { - int rc; - /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */ - rc = readlink(fsm->path, fsm->rdbuf, fsm->rdsize - 1); - if (_fsm_debug && (FSM_READLINK & FSM_SYSCALL)) - rpmlog(RPMLOG_DEBUG, " %8s (%s, rdbuf, %d) %s\n", fileStageString(FSM_READLINK), - fsm->path, (int)(fsm->rdsize -1), (rc < 0 ? strerror(errno) : "")); - if (rc < 0) rc = CPIOERR_READLINK_FAILED; - else { - fsm->rdnb = rc; - fsm->rdbuf[fsm->rdnb] = '\0'; - rc = 0; + ssize_t llen = readlink(path, buf, bufsize - 1); + int rc = CPIOERR_READLINK_FAILED; + + if (_fsm_debug && (FSM_READLINK & FSM_SYSCALL)) { + rpmlog(RPMLOG_DEBUG, " %8s (%s, rdbuf, %d) %s\n", + fileStageString(FSM_READLINK), + path, (int)(bufsize -1), (llen < 0 ? strerror(errno) : "")); + } + + if (llen >= 0) { + buf[llen] = '\0'; + rc = 0; + *linklen = llen; } return rc; } @@ -873,7 +876,7 @@ static int writeFile(FSM_t fsm, int writeData) * I don't think that's a specified standard. */ /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */ - rc = fsmReadLink(fsm); + rc = fsmReadLink(fsm->path, fsm->rdbuf, fsm->rdsize, &fsm->rdnb); if (rc) goto exit; st->st_size = fsm->rdnb; rstrcat(&symbuf, fsm->rdbuf); /* XXX save readlink return. */ @@ -1507,7 +1510,7 @@ static int fsmVerify(FSM_t fsm) } else if (S_ISLNK(st->st_mode)) { if (S_ISLNK(ost->st_mode)) { /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */ - rc = fsmReadLink(fsm); + rc = fsmReadLink(fsm->path, fsm->rdbuf, fsm->rdsize, &fsm->rdnb); errno = saveerrno; if (rc) return rc; if (rstreq(fsm->opath, fsm->rdbuf)) return 0; -- 2.7.4