From e8afd16892e87a6e2f17c1019ee455f5583387c2 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 17 Jun 2015 00:14:10 +0200 Subject: apply various style suggestions by cppcheck Some of them modify the ABI, but given that we prepare a big one already, these few hardly count for much. Git-Dch: Ignore --- apt-pkg/tagfile.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'apt-pkg/tagfile.cc') diff --git a/apt-pkg/tagfile.cc b/apt-pkg/tagfile.cc index 5ff495fbd..130aef19d 100644 --- a/apt-pkg/tagfile.cc +++ b/apt-pkg/tagfile.cc @@ -59,7 +59,7 @@ public: unsigned int StartValue; unsigned int NextInBucket; - TagData(unsigned int const StartTag) : StartTag(StartTag), EndTag(0), StartValue(0), NextInBucket(0) {} + explicit TagData(unsigned int const StartTag) : StartTag(StartTag), EndTag(0), StartValue(0), NextInBucket(0) {} }; std::vector Tags; }; -- cgit v1.2.3 From 6c55f07a5fa3612a5d59c61a17da5fe640eadc8b Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 17 Jun 2015 09:29:00 +0200 Subject: make all d-pointer * const pointers Doing this disables the implicit copy assignment operator (among others) which would cause hovac if used on the classes as it would just copy the pointer, not the data the d-pointer points to. For most of the classes we don't need a copy assignment operator anyway and in many classes it was broken before as many contain a pointer of some sort. Only for our Cacheset Container interfaces we define an explicit copy assignment operator which could later be implemented to copy the data from one d-pointer to the other if we need it. Git-Dch: Ignore --- apt-pkg/tagfile.cc | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) (limited to 'apt-pkg/tagfile.cc') diff --git a/apt-pkg/tagfile.cc b/apt-pkg/tagfile.cc index 130aef19d..4c5505bf3 100644 --- a/apt-pkg/tagfile.cc +++ b/apt-pkg/tagfile.cc @@ -32,13 +32,22 @@ using std::string; class pkgTagFilePrivate { public: - pkgTagFilePrivate(FileFd *pFd, unsigned long long Size) : Fd(*pFd), Buffer(NULL), - Start(NULL), End(NULL), - Done(false), iOffset(0), - Size(Size) + void Reset(FileFd * const pFd, unsigned long long const pSize) { + Fd = pFd; + Buffer = NULL; + Start = NULL; + End = NULL; + Done = false; + iOffset = 0; + Size = pSize; } - FileFd &Fd; + + pkgTagFilePrivate(FileFd * const pFd, unsigned long long const Size) + { + Reset(pFd, Size); + } + FileFd * Fd; char *Buffer; char *Start; char *End; @@ -83,27 +92,21 @@ static unsigned long AlphaHash(const char *Text, size_t Length) /*{{{*/ // TagFile::pkgTagFile - Constructor /*{{{*/ // --------------------------------------------------------------------- /* */ -pkgTagFile::pkgTagFile(FileFd *pFd,unsigned long long Size) - : d(NULL) +pkgTagFile::pkgTagFile(FileFd * const pFd,unsigned long long const Size) + : d(new pkgTagFilePrivate(pFd, Size + 4)) { Init(pFd, Size); } - -void pkgTagFile::Init(FileFd *pFd,unsigned long long Size) +void pkgTagFile::Init(FileFd * const pFd,unsigned long long Size) { /* The size is increased by 4 because if we start with the Size of the filename we need to try to read 1 char more to see an EOF faster, 1 char the end-pointer can be on and maybe 2 newlines need to be added to the end of the file -> 4 extra chars */ Size += 4; - if(d != NULL) - { - free(d->Buffer); - delete d; - } - d = new pkgTagFilePrivate(pFd, Size); + d->Reset(pFd, Size); - if (d->Fd.IsOpen() == false) + if (d->Fd->IsOpen() == false) d->Start = d->End = d->Buffer = 0; else d->Buffer = (char*)malloc(sizeof(char) * Size); @@ -184,7 +187,7 @@ bool pkgTagFile::Step(pkgTagSection &Tag) if (Resize() == false) return _error->Error(_("Unable to parse package file %s (%d)"), - d->Fd.Name().c_str(), 1); + d->Fd->Name().c_str(), 1); } while (Tag.Scan(d->Start,d->End - d->Start, false) == false); } @@ -213,7 +216,7 @@ bool pkgTagFile::Fill() { // See if only a bit of the file is left unsigned long long const dataSize = d->Size - ((d->End - d->Buffer) + 1); - if (d->Fd.Read(d->End, dataSize, &Actual) == false) + if (d->Fd->Read(d->End, dataSize, &Actual) == false) return false; if (Actual != dataSize) d->Done = true; @@ -268,7 +271,7 @@ bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long long Offset) // Reposition and reload.. d->iOffset = Offset; d->Done = false; - if (d->Fd.Seek(Offset) == false) + if (d->Fd->Seek(Offset) == false) return false; d->End = d->Start = d->Buffer; @@ -283,7 +286,7 @@ bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long long Offset) return false; if (Tag.Scan(d->Start, d->End - d->Start, false) == false) - return _error->Error(_("Unable to parse package file %s (%d)"),d->Fd.Name().c_str(), 2); + return _error->Error(_("Unable to parse package file %s (%d)"),d->Fd->Name().c_str(), 2); return true; } @@ -293,9 +296,8 @@ bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long long Offset) /* */ APT_IGNORE_DEPRECATED_PUSH pkgTagSection::pkgTagSection() - : Section(0), d(NULL), Stop(0) + : Section(0), d(new pkgTagSectionPrivate()), Stop(0) { - d = new pkgTagSectionPrivate(); #if APT_PKG_ABI < 413 TagCount = 0; memset(&Indexes, 0, sizeof(Indexes)); -- cgit v1.2.3 From 3d8232bf97ce11818fb07813a71136484ea1a44a Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 18 Jun 2015 17:33:15 +0200 Subject: fix memory leaks reported by -fsanitize Various small leaks here and there. Nothing particularily big, but still good to fix. Found by the sanitizers while running our testcases. Reported-By: gcc -fsanitize Git-Dch: Ignore --- apt-pkg/tagfile.cc | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'apt-pkg/tagfile.cc') diff --git a/apt-pkg/tagfile.cc b/apt-pkg/tagfile.cc index 4c5505bf3..6d7d8185b 100644 --- a/apt-pkg/tagfile.cc +++ b/apt-pkg/tagfile.cc @@ -34,8 +34,10 @@ class pkgTagFilePrivate public: void Reset(FileFd * const pFd, unsigned long long const pSize) { - Fd = pFd; + if (Buffer != NULL) + free(Buffer); Buffer = NULL; + Fd = pFd; Start = NULL; End = NULL; Done = false; @@ -43,7 +45,7 @@ public: Size = pSize; } - pkgTagFilePrivate(FileFd * const pFd, unsigned long long const Size) + pkgTagFilePrivate(FileFd * const pFd, unsigned long long const Size) : Buffer(NULL) { Reset(pFd, Size); } @@ -54,6 +56,12 @@ public: bool Done; unsigned long long iOffset; unsigned long long Size; + + ~pkgTagFilePrivate() + { + if (Buffer != NULL) + free(Buffer); + } }; class pkgTagSectionPrivate @@ -127,7 +135,6 @@ void pkgTagFile::Init(FileFd * const pFd,unsigned long long Size) /* */ pkgTagFile::~pkgTagFile() { - free(d->Buffer); delete d; } /*}}}*/ -- cgit v1.2.3 From 81460e32961bb0b9922bf8a1a27d87705d8c3e51 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sun, 21 Jun 2015 23:12:24 +0200 Subject: bring back deb822 sources.list entries as .sources Having two different formats in the same file is very dirty and causes external tools to fail hard trying to parse them. It is probably not a good idea for them to parse them in the first place, but they do and we shouldn't break them if there is a better way. So we solve this issue for now by giving our deb822 format a new filename extension ".sources" which unsupporting applications are likely to ignore an can begin gradually moving forward rather than waiting for the unknown applications to catch up. Currently and for the forseeable future apt is going to support both with the same feature set as documented in the manpage, with the longtime plan of adopting the 'new' format as default, but that is a long way to go and might get going more from having an easier time setting options than from us pushing it explicitely. --- apt-pkg/tagfile.cc | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'apt-pkg/tagfile.cc') diff --git a/apt-pkg/tagfile.cc b/apt-pkg/tagfile.cc index 6d7d8185b..cc63b213f 100644 --- a/apt-pkg/tagfile.cc +++ b/apt-pkg/tagfile.cc @@ -775,6 +775,14 @@ bool pkgTagSection::Write(FileFd &File, char const * const * const Order, std::v } /*}}}*/ +void pkgUserTagSection::TrimRecord(bool /*BeforeRecord*/, const char* &End)/*{{{*/ +{ + for (; Stop < End && (Stop[0] == '\n' || Stop[0] == '\r' || Stop[0] == '#'); Stop++) + if (Stop[0] == '#') + Stop = (const char*) memchr(Stop,'\n',End-Stop); +} + /*}}}*/ + #include "tagfile-order.c" // TFRewrite - Rewrite a control record /*{{{*/ -- cgit v1.2.3 From 4dc77823d360158d6870a5710cc8c17064f1308f Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 15 Jul 2015 13:21:21 +0200 Subject: remove the compatibility markers for 4.13 abi We aren't and we will not be really compatible again with the previous stable abi, so lets drop these markers (which never made it into a released version) for good as they have outlived their intend already. Git-Dch: Ignore --- apt-pkg/tagfile.cc | 28 ---------------------------- 1 file changed, 28 deletions(-) (limited to 'apt-pkg/tagfile.cc') diff --git a/apt-pkg/tagfile.cc b/apt-pkg/tagfile.cc index cc63b213f..213a413cb 100644 --- a/apt-pkg/tagfile.cc +++ b/apt-pkg/tagfile.cc @@ -305,21 +305,11 @@ APT_IGNORE_DEPRECATED_PUSH pkgTagSection::pkgTagSection() : Section(0), d(new pkgTagSectionPrivate()), Stop(0) { -#if APT_PKG_ABI < 413 - TagCount = 0; - memset(&Indexes, 0, sizeof(Indexes)); -#endif memset(&AlphaIndexes, 0, sizeof(AlphaIndexes)); } APT_IGNORE_DEPRECATED_POP /*}}}*/ // TagSection::Scan - Scan for the end of the header information /*{{{*/ -#if APT_PKG_ABI < 413 -bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength) -{ - return Scan(Start, MaxLength, true); -} -#endif bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength, bool const Restart) { Section = Start; @@ -345,11 +335,7 @@ bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength, bool const R } d->Tags.reserve(0x100); } -#if APT_PKG_ABI >= 413 unsigned int TagCount = d->Tags.size(); -#else - APT_IGNORE_DEPRECATED(TagCount = d->Tags.size();) -#endif if (Stop == 0) return false; @@ -376,10 +362,6 @@ bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength, bool const R lastTagData.NextInBucket = AlphaIndexes[lastTagHash]; APT_IGNORE_DEPRECATED_PUSH AlphaIndexes[lastTagHash] = TagCount; -#if APT_PKG_ABI < 413 - if (d->Tags.size() < sizeof(Indexes)/sizeof(Indexes[0])) - Indexes[d->Tags.size()] = lastTagData.StartTag; -#endif APT_IGNORE_DEPRECATED_POP d->Tags.push_back(lastTagData); } @@ -423,16 +405,10 @@ bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength, bool const R if (AlphaIndexes[lastTagHash] != 0) lastTagData.NextInBucket = AlphaIndexes[lastTagHash]; APT_IGNORE_DEPRECATED(AlphaIndexes[lastTagHash] = TagCount;) -#if APT_PKG_ABI < 413 - APT_IGNORE_DEPRECATED(Indexes[d->Tags.size()] = lastTagData.StartTag;) -#endif d->Tags.push_back(lastTagData); } pkgTagSectionPrivate::TagData const td(Stop - Section); -#if APT_PKG_ABI < 413 - APT_IGNORE_DEPRECATED(Indexes[d->Tags.size()] = td.StartTag;) -#endif d->Tags.push_back(td); TrimRecord(false,End); return true; @@ -463,11 +439,7 @@ void pkgTagSection::Trim() } /*}}}*/ // TagSection::Exists - return True if a tag exists /*{{{*/ -#if APT_PKG_ABI >= 413 bool pkgTagSection::Exists(const char* const Tag) const -#else -bool pkgTagSection::Exists(const char* const Tag) -#endif { unsigned int tmp; return Find(Tag, tmp); -- cgit v1.2.3 From dfe66c72ffc010e019e96b35154e1ad4ab506a6e Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 15 Jul 2015 14:36:16 +0200 Subject: use a smaller type for flags storage in the cache We store very few flags in the cache, so keeping storage space for 8 is enough for all of them and still leaves a few unused bits remaining for future extensions without wasting bytes for nothing. Git-Dch: Ignore --- apt-pkg/tagfile.cc | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'apt-pkg/tagfile.cc') diff --git a/apt-pkg/tagfile.cc b/apt-pkg/tagfile.cc index 213a413cb..253b1b7a3 100644 --- a/apt-pkg/tagfile.cc +++ b/apt-pkg/tagfile.cc @@ -579,6 +579,34 @@ bool pkgTagSection::FindB(const char *Tag, bool const &Default) const // TagSection::FindFlag - Locate a yes/no type flag /*{{{*/ // --------------------------------------------------------------------- /* The bits marked in Flag are masked on/off in Flags */ +bool pkgTagSection::FindFlag(const char * const Tag, uint8_t &Flags, + uint8_t const Flag) const +{ + const char *Start; + const char *Stop; + if (Find(Tag,Start,Stop) == false) + return true; + return FindFlag(Flags, Flag, Start, Stop); +} +bool pkgTagSection::FindFlag(uint8_t &Flags, uint8_t const Flag, + char const* const Start, char const* const Stop) +{ + switch (StringToBool(string(Start, Stop))) + { + case 0: + Flags &= ~Flag; + return true; + + case 1: + Flags |= Flag; + return true; + + default: + _error->Warning("Unknown flag value: %s",string(Start,Stop).c_str()); + return true; + } + return true; +} bool pkgTagSection::FindFlag(const char *Tag,unsigned long &Flags, unsigned long Flag) const { -- cgit v1.2.3 From 809aa216c630f1cc61b0c3b9d992d4a3be14be3c Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Wed, 12 Aug 2015 20:44:40 +0200 Subject: policy: Be more strict about parsing pin files, and document prio 0 Treat invalid pin priorities and overflows as an error. Closes: #429912 --- apt-pkg/tagfile.cc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'apt-pkg/tagfile.cc') diff --git a/apt-pkg/tagfile.cc b/apt-pkg/tagfile.cc index 253b1b7a3..8acecd735 100644 --- a/apt-pkg/tagfile.cc +++ b/apt-pkg/tagfile.cc @@ -533,9 +533,16 @@ signed int pkgTagSection::FindI(const char *Tag,signed long Default) const return Default; strncpy(S,Start,Stop-Start); S[Stop - Start] = 0; - + + errno = 0; char *End; signed long Result = strtol(S,&End,10); + if (errno == ERANGE) + _error->Errno("strtol", _("Cannot convert %s to integer"), S); + if (Result < std::numeric_limits::min() || Result > std::numeric_limits::max()) { + errno = ERANGE; + _error->Errno("", _("Cannot convert %s to integer"), S); + } if (S == End) return Default; return Result; -- cgit v1.2.3