Date: Fri, 7 May 2010 12:52:34 GMT From: Garrett Cooper <gcooper@FreeBSD.org> To: Perforce Change Reviews <perforce@FreeBSD.org> Subject: PERFORCE change 177900 for review Message-ID: <201005071252.o47CqYMW007324@repoman.freebsd.org>
next in thread | raw e-mail | index | archive | help
http://p4web.freebsd.org/@@177900?ac=10 Change 177900 by gcooper@gcooper-bayonetta on 2010/05/07 12:51:53 1. Make code more robust by dealing with potential error cases when archive_read_new(3) fails. 2. Also, free the archive object after the read is done, if it was properly initialized. Affected files ... .. //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/lib/libpkg/file.c#4 edit Differences ... ==== //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/lib/libpkg/file.c#4 (text+ko) ==== @@ -389,31 +389,38 @@ printf("%s: will extract %s from %s\n", __func__, file, pkg_name_humanized); - archive = archive_read_new(); - archive_read_support_compression_all(archive); - archive_read_support_format_tar(archive); + if ((archive = archive_read_new()) != NULL) { + + archive_read_support_compression_all(archive); + archive_read_support_format_tar(archive); + + /* + * Avoid potential race conditions with + * archive_read_open_filename(3), by opening the file + * beforehand. + */ + if (pkg == NULL) + archive_fd = fileno(stdin); + else + archive_fd = open(pkg, O_RDONLY); - /* - * Avoid potential race conditions with archive_read_open_filename(3), - * by opening the file beforehand. - */ - if (pkg == NULL) - archive_fd = fileno(stdin); - else - archive_fd = open(pkg, O_RDONLY); + } + if (archive == NULL) { + error = archive_error_string(archive); + warnx("%s: unable to open the package from %s: %s", + __func__, pkg_name_humanized, error); + } /* The initial open failed */ - if (archive_fd == -1) + else if (archive_fd == -1) warn("%s: unable to open the package from %s", __func__, pkg_name_humanized); /* archive(3) failed to open the file. */ else if (archive_read_open_fd(archive, archive_fd, ARCHIVE_DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) { - error = archive_error_string(archive); warnx("%s: unable to open the package from %s: %s", __func__, pkg_name_humanized, error); - } else while (error == NULL && found_match == FALSE && @@ -452,8 +459,9 @@ } - archive_read_finish(archive); + if (archive != NULL) + archive_read_finish(archive); /* Close any open descriptors. */ if (0 <= archive_fd) close(archive_fd); @@ -499,21 +507,30 @@ pkg_name_humanized, __func__, file_expr); } - archive = archive_read_new(); - archive_read_support_compression_all(archive); - archive_read_support_format_tar(archive); + if ((archive = archive_read_new()) != NULL) { + + archive_read_support_compression_all(archive); + archive_read_support_format_tar(archive); + + /* + * Avoid potential race conditions with + * archive_read_open_filename(3), by opening the file + * beforehand. + */ + if (pkg == NULL) + archive_fd = fileno(stdin); + else + archive_fd = open(pkg, O_RDONLY); - /* - * Avoid potential race conditions with archive_read_open_filename(3), - * by opening the file beforehand. - */ - if (pkg == NULL) - archive_fd = fileno(stdin); - else - archive_fd = open(pkg, O_RDONLY); + } + if (archive == NULL) { + error = archive_error_string(archive); + warnx("%s: unable to open the package from %s: %s", + __func__, pkg_name_humanized, error); + } /* The initial open failed */ - if (archive_fd == -1) + else if (archive_fd == -1) warn("%s: unable to open the package from %s", __func__, pkg_name_humanized); else if (archive_read_open_fd(archive, archive_fd, @@ -556,8 +573,8 @@ } serrno = errno; - archive_read_finish(archive); - + if (archive != NULL) + archive_read_finish(archive); /* Close any open descriptors. */ if (0 <= archive_fd) close(archive_fd);
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201005071252.o47CqYMW007324>