1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-12-23 16:28:50 -05:00

Use standard errno codes in return from _alpm_archive_fgets

This allows us to not require the context (e.g. handle) when calling
this function. Also beef up the checks in the two callers of this
function to bail if the last return code is not ARCHIVE_EOF, which is
the expected value.

This requires a change to one of the pactest return codes and the
overall result of the test, but results in a much safer operating
condition whereby invalid database entries will stop the operation.

Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dan McGee 2011-06-03 14:37:51 -05:00
parent e68f5d9a30
commit cc25576f8b
5 changed files with 34 additions and 18 deletions

View File

@ -137,13 +137,13 @@ static struct pkg_operations *get_file_pkg_ops(void)
* @param archive the archive to read from, pointed at the .PKGINFO entry * @param archive the archive to read from, pointed at the .PKGINFO entry
* @param newpkg an empty pmpkg_t struct to fill with package info * @param newpkg an empty pmpkg_t struct to fill with package info
* *
* @return 0 on success, 1 on error * @return 0 on success, -1 on error
*/ */
static int parse_descfile(struct archive *a, pmpkg_t *newpkg) static int parse_descfile(struct archive *a, pmpkg_t *newpkg)
{ {
char *ptr = NULL; char *ptr = NULL;
char *key = NULL; char *key = NULL;
int linenum = 0; int ret, linenum = 0;
struct archive_read_buffer buf; struct archive_read_buffer buf;
memset(&buf, 0, sizeof(buf)); memset(&buf, 0, sizeof(buf));
@ -151,7 +151,7 @@ static int parse_descfile(struct archive *a, pmpkg_t *newpkg)
buf.max_line_size = 512 * 1024; buf.max_line_size = 512 * 1024;
/* loop until we reach EOF or other error */ /* loop until we reach EOF or other error */
while(_alpm_archive_fgets(a, &buf) == ARCHIVE_OK) { while((ret = _alpm_archive_fgets(a, &buf)) == ARCHIVE_OK) {
char *line = _alpm_strtrim(buf.line); char *line = _alpm_strtrim(buf.line);
linenum++; linenum++;
@ -215,6 +215,10 @@ static int parse_descfile(struct archive *a, pmpkg_t *newpkg)
} }
line[0] = '\0'; line[0] = '\0';
} }
if(ret != ARCHIVE_EOF) {
_alpm_log(PM_LOG_DEBUG, "error parsing package descfile\n");
return -1;
}
return 0; return 0;
} }

View File

@ -294,6 +294,7 @@ static int sync_db_populate(pmdb_t *db)
_alpm_log(PM_LOG_ERROR, _("invalid name for database entry '%s'\n"), _alpm_log(PM_LOG_ERROR, _("invalid name for database entry '%s'\n"),
name); name);
_alpm_pkg_free(pkg); _alpm_pkg_free(pkg);
pkg = NULL;
continue; continue;
} }
@ -301,6 +302,7 @@ static int sync_db_populate(pmdb_t *db)
if(_alpm_pkghash_find(db->pkgcache, pkg->name)) { if(_alpm_pkghash_find(db->pkgcache, pkg->name)) {
_alpm_log(PM_LOG_ERROR, _("duplicated database entry '%s'\n"), pkg->name); _alpm_log(PM_LOG_ERROR, _("duplicated database entry '%s'\n"), pkg->name);
_alpm_pkg_free(pkg); _alpm_pkg_free(pkg);
pkg = NULL;
continue; continue;
} }
@ -316,7 +318,14 @@ static int sync_db_populate(pmdb_t *db)
count++; count++;
} else { } else {
/* we have desc, depends or deltas - parse it */ /* we have desc, depends or deltas - parse it */
sync_db_read(db, archive, entry, pkg); if(sync_db_read(db, archive, entry, pkg) != 0) {
_alpm_log(PM_LOG_ERROR,
_("could not parse package '%s' description file from db '%s'\n"),
pkg->name, db->treename);
_alpm_pkg_free(pkg);
pkg = NULL;
continue;
}
} }
} }
@ -401,7 +410,8 @@ static int sync_db_read(pmdb_t *db, struct archive *archive,
if(strcmp(filename, "desc") == 0 || strcmp(filename, "depends") == 0 if(strcmp(filename, "desc") == 0 || strcmp(filename, "depends") == 0
|| strcmp(filename, "deltas") == 0) { || strcmp(filename, "deltas") == 0) {
while(_alpm_archive_fgets(archive, &buf) == ARCHIVE_OK) { int ret;
while((ret = _alpm_archive_fgets(archive, &buf)) == ARCHIVE_OK) {
char *line = _alpm_strtrim(buf.line); char *line = _alpm_strtrim(buf.line);
if(strcmp(line, "%NAME%") == 0) { if(strcmp(line, "%NAME%") == 0) {
@ -478,6 +488,9 @@ static int sync_db_read(pmdb_t *db, struct archive *archive,
} }
} }
} }
if(ret != ARCHIVE_EOF) {
goto error;
}
} else if(strcmp(filename, "files") == 0) { } else if(strcmp(filename, "files") == 0) {
/* currently do nothing with this file */ /* currently do nothing with this file */
} else { } else {
@ -485,10 +498,11 @@ static int sync_db_read(pmdb_t *db, struct archive *archive,
_alpm_log(PM_LOG_DEBUG, "unknown database file: %s\n", filename); _alpm_log(PM_LOG_DEBUG, "unknown database file: %s\n", filename);
} }
return 0;
error: error:
FREE(pkgname); FREE(pkgname);
/* TODO: return 0 always? */ return -1;
return 0;
} }
static int sync_db_version(pmdb_t UNUSED *db) static int sync_db_version(pmdb_t UNUSED *db)

View File

@ -771,20 +771,19 @@ int _alpm_archive_fgets(struct archive *a, struct archive_read_buffer *b)
/* allocate our buffer, or ensure our existing one is big enough */ /* allocate our buffer, or ensure our existing one is big enough */
if(!b->line) { if(!b->line) {
/* set the initial buffer to the read block_size */ /* set the initial buffer to the read block_size */
CALLOC(b->line, b->block_size + 1, sizeof(char), CALLOC(b->line, b->block_size + 1, sizeof(char), return ENOMEM);
RET_ERR(PM_ERR_MEMORY, -1));
b->line_size = b->block_size + 1; b->line_size = b->block_size + 1;
b->line_offset = b->line; b->line_offset = b->line;
} else { } else {
size_t needed = (size_t)((b->line_offset - b->line) size_t needed = (size_t)((b->line_offset - b->line)
+ (i - b->block_offset) + 1); + (i - b->block_offset) + 1);
if(needed > b->max_line_size) { if(needed > b->max_line_size) {
RET_ERR(PM_ERR_MEMORY, -1); return ERANGE;
} }
if(needed > b->line_size) { if(needed > b->line_size) {
/* need to realloc + copy data to fit total length */ /* need to realloc + copy data to fit total length */
char *new; char *new;
CALLOC(new, needed, sizeof(char), RET_ERR(PM_ERR_MEMORY, -1)); CALLOC(new, needed, sizeof(char), return ENOMEM);
memcpy(new, b->line, b->line_size); memcpy(new, b->line, b->line_size);
b->line_size = needed; b->line_size = needed;
b->line_offset = new + (b->line_offset - b->line); b->line_offset = new + (b->line_offset - b->line);

View File

@ -114,6 +114,7 @@ if __name__ == "__main__":
env.results() env.results()
if env.failed > 0: if env.failed > 0:
print "pacman testing root saved: %s" % root_path
sys.exit(1) sys.exit(1)
if not opts.keeproot: if not opts.keeproot:

View File

@ -10,10 +10,8 @@ self.addpkg(p2)
self.args = "-U %s %s" % (p1.filename(), p2.filename()) self.args = "-U %s %s" % (p1.filename(), p2.filename())
# Note that the current cutoff on line length is 512K, so the first package # We error out when fed a package with an invalid description; the second one
# will succeed while the second one will fail to record the description. # fits the bill in this case as the desc is > 512K
self.addrule("PACMAN_RETCODE=0") self.addrule("PACMAN_RETCODE=1")
self.addrule("PKG_EXIST=pkg1") self.addrule("!PKG_EXIST=pkg1")
self.addrule("PKG_DESC=pkg1|%s" % p1.desc) self.addrule("!PKG_EXIST=pkg1")
self.addrule("PKG_EXIST=pkg1")
self.addrule("!PKG_DESC=pkg1|%s" % p2.desc)