summaryrefslogtreecommitdiff
path: root/apt-pkg/deb
diff options
context:
space:
mode:
authorDavid Kalnischkies <david@kalnischkies.de>2015-11-04 18:01:35 +0100
committerDavid Kalnischkies <david@kalnischkies.de>2015-11-05 12:21:33 +0100
commit9fb4e6510faaa9c84b88d4cb567bf3deebf8e0b7 (patch)
tree50e36213aa7f10dcfe3975a80817f92e11c1e57b /apt-pkg/deb
parent23e64f6d0facf9610c1042326ad9850e071e8349 (diff)
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
Diffstat (limited to 'apt-pkg/deb')
-rw-r--r--apt-pkg/deb/dpkgpm.cc41
1 files 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 /*{{{*/