From 9fb4e6510faaa9c84b88d4cb567bf3deebf8e0b7 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 4 Nov 2015 18:01:35 +0100 Subject: do not use read() returned value unconditionally A slightly unlikely bug, but lets fix it while slightly reworking this whole function to be slightly saner to look at, even if still not good. Git-Dch: Ignore --- apt-pkg/deb/dpkgpm.cc | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/apt-pkg/deb/dpkgpm.cc b/apt-pkg/deb/dpkgpm.cc index 1446826d6..d253808f2 100644 --- a/apt-pkg/deb/dpkgpm.cc +++ b/apt-pkg/deb/dpkgpm.cc @@ -83,7 +83,7 @@ public: bool stdin_is_dev_null; // the buffer we use for the dpkg status-fd reading char dpkgbuf[1024]; - int dpkgbuf_pos; + size_t dpkgbuf_pos; FILE *term_out; FILE *history_out; string dpkg_error; @@ -754,40 +754,33 @@ void pkgDPkgPM::handleDisappearAction(string const &pkgname) } /*}}}*/ // DPkgPM::DoDpkgStatusFd /*{{{*/ -// --------------------------------------------------------------------- -/* - */ void pkgDPkgPM::DoDpkgStatusFd(int statusfd) { - char *p, *q; - int len; - - len=read(statusfd, &d->dpkgbuf[d->dpkgbuf_pos], sizeof(d->dpkgbuf)-d->dpkgbuf_pos); - d->dpkgbuf_pos += len; + ssize_t const len = read(statusfd, &d->dpkgbuf[d->dpkgbuf_pos], + (sizeof(d->dpkgbuf)/sizeof(d->dpkgbuf[0])) - d->dpkgbuf_pos); if(len <= 0) return; + d->dpkgbuf_pos += (len / sizeof(d->dpkgbuf[0])); - // process line by line if we have a buffer - p = q = d->dpkgbuf; - while((q=(char*)memchr(p, '\n', d->dpkgbuf+d->dpkgbuf_pos-p)) != NULL) + // process line by line from the buffer + char *p = d->dpkgbuf, *q = nullptr; + while((q=(char*)memchr(p, '\n', (d->dpkgbuf + d->dpkgbuf_pos) - p)) != nullptr) { - *q = 0; + *q = '\0'; ProcessDpkgStatusLine(p); - p=q+1; // continue with next line + p = q + 1; // continue with next line } - // now move the unprocessed bits (after the final \n that is now a 0x0) - // to the start and update d->dpkgbuf_pos - p = (char*)memrchr(d->dpkgbuf, 0, d->dpkgbuf_pos); - if(p == NULL) + // check if we stripped the buffer clean + if (p > (d->dpkgbuf + d->dpkgbuf_pos)) + { + d->dpkgbuf_pos = 0; return; + } - // we are interessted in the first char *after* 0x0 - p++; - - // move the unprocessed tail to the start and update pos - memmove(d->dpkgbuf, p, p-d->dpkgbuf); - d->dpkgbuf_pos = d->dpkgbuf+d->dpkgbuf_pos-p; + // otherwise move the unprocessed tail to the start and update pos + memmove(d->dpkgbuf, p, (p - d->dpkgbuf)); + d->dpkgbuf_pos = (d->dpkgbuf + d->dpkgbuf_pos) - p; } /*}}}*/ // DPkgPM::WriteHistoryTag /*{{{*/ -- cgit v1.2.3