Skip to content

Commit

Permalink
Fix ArgumentError: NULL pointer given
Browse files Browse the repository at this point in the history
  • Loading branch information
spk committed Jul 9, 2023
1 parent 2505e70 commit 94fef47
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
15 changes: 11 additions & 4 deletions ext/apt_pkg/pkgcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,32 @@ is_multi_arch(VALUE self)
*
**/
static VALUE
packages(int argc, VALUE *argv, VALUE self)
packages(VALUE self)
{
if (!config_system_initialized()) {
rb_raise(e_mDebianAptPkgInitError, "System not initialized");
}
VALUE result = rb_ary_new();
pkgCacheFile CacheFile;
if (CacheFile.GetPkgCache() == 0) {
pkgCache *Cache = CacheFile.GetPkgCache();
if (Cache == NULL) {
return Qnil;
}
for (pkgCache::PkgIterator Pkg = CacheFile.GetPkgCache()->PkgBegin(); not Pkg.end(); ++Pkg) {
for (pkgCache::PkgIterator Pkg = Cache->PkgBegin(); not Pkg.end(); ++Pkg) {
VALUE current_version;
VALUE current_version_section;
if (Pkg->CurrentVer == 0) {
current_version = Qnil;
} else {
if (Pkg.CurrentVer().Section() != NULL) {
current_version_section = rb_str_new2(Pkg.CurrentVer().Section());
} else {
current_version_section = Qnil;
}
current_version = rb_struct_new(rb_cVersion,
rb_str_new2(Pkg.CurrentVer().ParentPkg().Name()),
rb_str_new2(Pkg.CurrentVer().VerStr()),
rb_str_new2(Pkg.CurrentVer().Section()),
current_version_section,
rb_str_new2(Pkg.CurrentVer().Arch()),
INT2FIX(Pkg.CurrentVer()->Size),
INT2FIX(Pkg.CurrentVer()->InstalledSize),
Expand Down
40 changes: 21 additions & 19 deletions test/debian/apt_pkg/pkg_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,24 @@
if packages.first
_(packages.first).must_be_kind_of Debian::AptPkg::Package
libapt = packages.find { |pkg| pkg.name == 'libapt-pkg-dev' }
_(libapt.id).must_be_kind_of Numeric
_(libapt.name).must_equal 'libapt-pkg-dev'
_(libapt.full_name).must_match(/libapt-pkg-dev:(\w)/)
_(libapt.arch).must_match(/(\w)/)
_([true, false]).must_include libapt.essential
_([true, false]).must_include libapt.important
_(libapt.current_version).must_be_kind_of Debian::AptPkg::Version
_(libapt.current_version.parent_package_name).must_equal libapt.name
_(libapt.current_version.version_string).must_be_kind_of String
_(libapt.current_version.section).must_equal 'libdevel'
_(libapt.current_version.arch).must_equal libapt.arch
_(libapt.current_version.size).must_be_kind_of Numeric
_(libapt.current_version.installed_size).must_be_kind_of Numeric
_(libapt.current_version.hash).must_be_kind_of Numeric
_(libapt.current_version.id).must_be_kind_of Numeric
_(libapt.current_version.priority).must_be_kind_of Numeric
if libapt
_(libapt.id).must_be_kind_of Numeric
_(libapt.name).must_equal 'libapt-pkg-dev'
_(libapt.full_name).must_match(/libapt-pkg-dev:(\w)/)
_(libapt.arch).must_match(/(\w)/)
_([true, false]).must_include libapt.essential
_([true, false]).must_include libapt.important
_(libapt.current_version).must_be_kind_of Debian::AptPkg::Version
_(libapt.current_version.parent_package_name).must_equal libapt.name
_(libapt.current_version.version_string).must_be_kind_of String
_(libapt.current_version.section).must_equal 'libdevel'
_(libapt.current_version.arch).must_equal libapt.arch
_(libapt.current_version.size).must_be_kind_of Numeric
_(libapt.current_version.installed_size).must_be_kind_of Numeric
_(libapt.current_version.hash).must_be_kind_of Numeric
_(libapt.current_version.id).must_be_kind_of Numeric
_(libapt.current_version.priority).must_be_kind_of Numeric
end
end
end
end
Expand Down Expand Up @@ -97,11 +99,11 @@

it 'be filtered' do
capture_io do
search = Debian::AptPkg::PkgCache.pkg_names('vim')
search = Debian::AptPkg::PkgCache.pkg_names('libapt-pkg-dev')
# CI specific cache can not be present
unless search.nil? || search.empty?
_(search).must_include 'vim'
_(search).wont_include 'emacs'
_(search).must_include 'libapt-pkg-dev'
_(search).wont_include 'apt'
end
end
end
Expand Down

0 comments on commit 94fef47

Please sign in to comment.