From 218fc3f6516a8eea855175509ba46c426b4275bd Mon Sep 17 00:00:00 2001 From: Michael Mohr Date: Sun, 2 Jun 2019 21:22:19 -0700 Subject: [PATCH 1/2] Do not include cassert --- ebml/EbmlCrc32.h | 2 -- ebml/EbmlSInteger.h | 2 -- ebml/IOCallback.h | 2 -- src/EbmlBinary.cpp | 1 - src/EbmlDate.cpp | 2 -- src/EbmlElement.cpp | 1 - src/EbmlFloat.cpp | 2 -- src/EbmlMaster.cpp | 1 - src/EbmlSInteger.cpp | 1 - src/EbmlString.cpp | 2 -- src/EbmlUInteger.cpp | 2 -- src/EbmlUnicodeString.cpp | 2 -- src/StdIOCallback.cpp | 1 - src/platform/win32/WinIOCallback.cpp | 2 -- 14 files changed, 23 deletions(-) diff --git a/ebml/EbmlCrc32.h b/ebml/EbmlCrc32.h index e41dc088..de7043cf 100644 --- a/ebml/EbmlCrc32.h +++ b/ebml/EbmlCrc32.h @@ -37,8 +37,6 @@ #ifndef LIBEBML_CRC32_H #define LIBEBML_CRC32_H -#include - #include "EbmlTypes.h" #include "EbmlBinary.h" diff --git a/ebml/EbmlSInteger.h b/ebml/EbmlSInteger.h index 0cd1b0d2..1e2dc3ea 100644 --- a/ebml/EbmlSInteger.h +++ b/ebml/EbmlSInteger.h @@ -38,8 +38,6 @@ #ifndef LIBEBML_SINTEGER_H #define LIBEBML_SINTEGER_H -#include - #include "EbmlTypes.h" #include "EbmlElement.h" diff --git a/ebml/IOCallback.h b/ebml/IOCallback.h index bf1ad028..aa6701ad 100644 --- a/ebml/IOCallback.h +++ b/ebml/IOCallback.h @@ -35,10 +35,8 @@ #include "EbmlTypes.h" -#include #include #include -// #include START_LIBEBML_NAMESPACE diff --git a/src/EbmlBinary.cpp b/src/EbmlBinary.cpp index d58340d9..cc02740d 100644 --- a/src/EbmlBinary.cpp +++ b/src/EbmlBinary.cpp @@ -34,7 +34,6 @@ \author Steve Lhomme \author Julien Coloos */ -#include #include #include "ebml/EbmlBinary.h" diff --git a/src/EbmlDate.cpp b/src/EbmlDate.cpp index 8d54bfd9..53f9d5c3 100644 --- a/src/EbmlDate.cpp +++ b/src/EbmlDate.cpp @@ -31,8 +31,6 @@ \version \$Id: EbmlDate.cpp 1079 2005-03-03 13:18:14Z robux4 $ \author Steve Lhomme */ -#include - #include "ebml/EbmlDate.h" START_LIBEBML_NAMESPACE diff --git a/src/EbmlElement.cpp b/src/EbmlElement.cpp index 871247c4..79f19f43 100644 --- a/src/EbmlElement.cpp +++ b/src/EbmlElement.cpp @@ -32,7 +32,6 @@ \author Steve Lhomme */ -#include #include #include #include diff --git a/src/EbmlFloat.cpp b/src/EbmlFloat.cpp index bf316189..e4d67361 100644 --- a/src/EbmlFloat.cpp +++ b/src/EbmlFloat.cpp @@ -34,8 +34,6 @@ \author Steve Lhomme */ -#include - #include "ebml/EbmlFloat.h" START_LIBEBML_NAMESPACE diff --git a/src/EbmlMaster.cpp b/src/EbmlMaster.cpp index 145e3e51..087095d5 100644 --- a/src/EbmlMaster.cpp +++ b/src/EbmlMaster.cpp @@ -34,7 +34,6 @@ \author Steve Lhomme */ -#include #include #include "ebml/EbmlMaster.h" diff --git a/src/EbmlSInteger.cpp b/src/EbmlSInteger.cpp index 7f141874..c8dd1ee0 100644 --- a/src/EbmlSInteger.cpp +++ b/src/EbmlSInteger.cpp @@ -32,7 +32,6 @@ \author Steve Lhomme \author Moritz Bunkus */ -#include #include #include "ebml/EbmlSInteger.h" diff --git a/src/EbmlString.cpp b/src/EbmlString.cpp index 4bd2d54a..fafdf575 100644 --- a/src/EbmlString.cpp +++ b/src/EbmlString.cpp @@ -33,8 +33,6 @@ \version \$Id$ \author Steve Lhomme */ -#include - #include "ebml/EbmlString.h" START_LIBEBML_NAMESPACE diff --git a/src/EbmlUInteger.cpp b/src/EbmlUInteger.cpp index e4f0390f..c7a1dffc 100644 --- a/src/EbmlUInteger.cpp +++ b/src/EbmlUInteger.cpp @@ -34,8 +34,6 @@ \author Steve Lhomme \author Moritz Bunkus */ -#include - #include "ebml/EbmlUInteger.h" START_LIBEBML_NAMESPACE diff --git a/src/EbmlUnicodeString.cpp b/src/EbmlUnicodeString.cpp index b1233a07..da5a31f5 100644 --- a/src/EbmlUnicodeString.cpp +++ b/src/EbmlUnicodeString.cpp @@ -35,8 +35,6 @@ \author Jory Stone */ -#include - #include "ebml/EbmlUnicodeString.h" #include "lib/utf8-cpp/source/utf8/checked.h" diff --git a/src/StdIOCallback.cpp b/src/StdIOCallback.cpp index 777c86f1..84eff181 100644 --- a/src/StdIOCallback.cpp +++ b/src/StdIOCallback.cpp @@ -33,7 +33,6 @@ \author Moritz Bunkus */ -#include #include #if !defined(__GNUC__) || (__GNUC__ > 2) #include diff --git a/src/platform/win32/WinIOCallback.cpp b/src/platform/win32/WinIOCallback.cpp index f3aeff4d..4c9998f5 100644 --- a/src/platform/win32/WinIOCallback.cpp +++ b/src/platform/win32/WinIOCallback.cpp @@ -34,8 +34,6 @@ \author Cyrius */ -#include - #include "WinIOCallback.h" #include "ebml/Debug.h" From 96aee6916fac811422fdade6d77c751cd136f81a Mon Sep 17 00:00:00 2001 From: Michael Mohr Date: Sun, 2 Jun 2019 22:14:59 -0700 Subject: [PATCH 2/2] Replace asserts with exceptions in Binary, Element, and Master source files --- CMakeLists.txt | 1 + ebml/EbmlExceptions.h | 51 +++++++++++++++++++++++++++++++++++++++++++ src/EbmlBinary.cpp | 4 +++- src/EbmlElement.cpp | 29 +++++++++++++++--------- src/EbmlMaster.cpp | 24 +++++++++++--------- 5 files changed, 88 insertions(+), 21 deletions(-) create mode 100644 ebml/EbmlExceptions.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 39eb0e88..feb5ca1b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,6 +48,7 @@ set(libebml_PUBLIC_HEADERS ebml/EbmlDummy.h ebml/EbmlElement.h ebml/EbmlEndian.h + ebml/EbmlExceptions.h ebml/EbmlFloat.h ebml/EbmlHead.h ebml/EbmlId.h diff --git a/ebml/EbmlExceptions.h b/ebml/EbmlExceptions.h new file mode 100644 index 00000000..c58e7eda --- /dev/null +++ b/ebml/EbmlExceptions.h @@ -0,0 +1,51 @@ +/**************************************************************************** +** libebml : parse EBML files, see http://embl.sourceforge.net/ +** +** Defines all exception classes to be used in libebml. +** +** Copyright (C) 2019 Michael Mohr. All rights reserved. +** +** This file is part of libebml. +** +** This library is free software; you can redistribute it and/or +** modify it under the terms of the GNU Lesser General Public +** License as published by the Free Software Foundation; either +** version 2.1 of the License, or (at your option) any later version. +** +** This library is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +** Lesser General Public License for more details. +** +** You should have received a copy of the GNU Lesser General Public +** License along with this library; if not, write to the Free Software +** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +** +** See http://www.gnu.org/licenses/lgpl-2.1.html for LGPL licensing information. +** +** Contact license@matroska.org if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ + +#ifndef LIBEBML_EXCEPTIONS_H +#define LIBEBML_EXCEPTIONS_H + +#include +#include "ebml_export.h" +#include "ebml/EbmlConfig.h" + +START_LIBEBML_NAMESPACE + +class EBML_DLL_API EbmlError : public std::exception +{ + private: + const char *reason; + public: + explicit EbmlError(const char *why) { this->reason = why; } + const char * what() const throw() override { return this->reason; } +}; + +END_LIBEBML_NAMESPACE + +#endif //LIBEBML_EXCEPTIONS_H diff --git a/src/EbmlBinary.cpp b/src/EbmlBinary.cpp index cc02740d..9646d953 100644 --- a/src/EbmlBinary.cpp +++ b/src/EbmlBinary.cpp @@ -36,6 +36,7 @@ */ #include +#include "ebml/EbmlExceptions.h" #include "ebml/EbmlBinary.h" #include "ebml/StdIOCallback.h" @@ -52,7 +53,8 @@ EbmlBinary::EbmlBinary(const EbmlBinary & ElementToClone) Data = NULL; else { Data = (binary *)malloc(GetSize() * sizeof(binary)); - assert(Data != NULL); + if(Data == nullptr) + throw EbmlError("Unable to allocate memory"); memcpy(Data, ElementToClone.Data, GetSize()); } } diff --git a/src/EbmlElement.cpp b/src/EbmlElement.cpp index 79f19f43..ce41715d 100644 --- a/src/EbmlElement.cpp +++ b/src/EbmlElement.cpp @@ -39,6 +39,7 @@ #include #include "ebml/EbmlElement.h" +#include "ebml/EbmlExceptions.h" #include "ebml/EbmlMaster.h" #include "ebml/EbmlStream.h" #include "ebml/EbmlVoid.h" @@ -216,12 +217,14 @@ EbmlCallbacks::EbmlCallbacks(EbmlElement & (*Creator)(), const EbmlId & aGlobalI ,DebugName(aDebugName) ,Context(aContext) { - assert((Create!=NULL) || !strcmp(aDebugName, "DummyElement")); + if(!(Create != nullptr || !strcmp(aDebugName, "DummyElement"))) + throw EbmlError("Invalid state"); } const EbmlSemantic & EbmlSemanticContext::GetSemantic(size_t i) const { - assert(i=Size) + throw EbmlError("Invalid index"); if (i= 16) + throw EbmlError("Invalid state"); // build the ID with the current Read Buffer bFound = false; binary IdBitMask = 1 << 7; @@ -493,8 +498,8 @@ EbmlElement * EbmlElement::SkipData(EbmlStream & DataStream, const EbmlSemanticC { EbmlElement * Result = NULL; if (bSizeIsFinite) { - assert(TestReadElt == NULL); - assert(ElementPosition < SizePosition); + if(TestReadElt != nullptr || ElementPosition >= SizePosition) + throw EbmlError("Invalid state"); DataStream.I_O().setFilePointer(SizePosition + CodedSizeLength(Size, SizeLength, bSizeIsFinite) + Size, seek_beginning); // DataStream.I_O().setFilePointer(Size, seek_current); } else { @@ -529,7 +534,8 @@ EbmlElement * EbmlElement::SkipData(EbmlStream & DataStream, const EbmlSemanticC if (EBML_CTX_PARENT(Context) != NULL) { Result = SkipData(DataStream, *EBML_CTX_PARENT(Context), Result); } else { - assert(Context.GetGlobalContext != NULL); + if(Context.GetGlobalContext == NULL) + throw EbmlError("No global context supplied"); if (Context != Context.GetGlobalContext()) { Result = SkipData(DataStream, Context.GetGlobalContext(), Result); } else { @@ -559,7 +565,8 @@ EbmlElement *EbmlElement::CreateElementUsingContext(const EbmlId & aID, const Eb } // global elements - assert(Context.GetGlobalContext != NULL); // global should always exist, at least the EBML ones + if(Context.GetGlobalContext == nullptr) + throw EbmlError("global should always exist, at least the EBML ones"); const EbmlSemanticContext & tstContext = Context.GetGlobalContext(); if (tstContext != Context) { LowLevel--; @@ -601,7 +608,8 @@ EbmlElement *EbmlElement::CreateElementUsingContext(const EbmlId & aID, const Eb */ filepos_t EbmlElement::Render(IOCallback & output, bool bWithDefault, bool bKeepPosition, bool bForceRender) { - assert(bValueIsSet || (bWithDefault && DefaultISset())); // an element is been rendered without a value set !!! + if(!(bValueIsSet || (bWithDefault && DefaultISset()))) + throw EbmlError("an element is being rendered without a value set!"); // it may be a mandatory element without a default value if (!bWithDefault && IsDefaultValue()) { return 0; @@ -613,7 +621,8 @@ filepos_t EbmlElement::Render(IOCallback & output, bool bWithDefault, bool bKeep uint64 WrittenSize = RenderData(output, bForceRender, bWithDefault); #if defined(LIBEBML_DEBUG) if (static_cast(SupposedSize) != (0-1)) - assert(WrittenSize == SupposedSize); + if(WrittenSize != SupposedSize) + throw EbmlError("Written size is not as expected!"); #endif // LIBEBML_DEBUG result += WrittenSize; return result; diff --git a/src/EbmlMaster.cpp b/src/EbmlMaster.cpp index 087095d5..23c39e7f 100644 --- a/src/EbmlMaster.cpp +++ b/src/EbmlMaster.cpp @@ -36,6 +36,7 @@ #include +#include "ebml/EbmlExceptions.h" #include "ebml/EbmlMaster.h" #include "ebml/EbmlStream.h" #include "ebml/EbmlContexts.h" @@ -71,7 +72,8 @@ EbmlMaster::EbmlMaster(const EbmlMaster & ElementToClone) EbmlMaster::~EbmlMaster() { - assert(!IsLocked()); // you're trying to delete a locked element !!! + if(IsLocked()) + throw EbmlError("You're trying to delete a locked element!"); size_t Index; @@ -91,8 +93,8 @@ filepos_t EbmlMaster::RenderData(IOCallback & output, bool bForceRender, bool bW filepos_t Result = 0; size_t Index; - if (!bForceRender) { - assert(CheckMandatory()); + if (!bForceRender && !CheckMandatory()) { + throw EbmlError("No forced render and not mandatory!"); } if (!bChecksumUsed) { // old school @@ -133,9 +135,9 @@ uint64 EbmlMaster::UpdateSize(bool bWithDefault, bool bForceRender) if (!IsFiniteSize()) return (0-1); - if (!bForceRender) { - assert(CheckMandatory()); - } + if (!bForceRender && !CheckMandatory()) { + throw EbmlError("No forced render and not mandatory!"); + } size_t Index; @@ -183,12 +185,12 @@ bool EbmlMaster::ProcessMandatory() return true; } - assert(Context.GetSize() != 0); + if(Context.GetSize() == 0) + throw EbmlError("Cannot continue when context size is 0"); unsigned int EltIdx; for (EltIdx = 0; EltIdx < EBML_CTX_SIZE(Context); EltIdx++) { if (EBML_CTX_IDX(Context,EltIdx).IsMandatory() && EBML_CTX_IDX(Context,EltIdx).IsUnique()) { -// assert(EBML_CTX_IDX(Context,EltIdx).Create != NULL); PushElement(EBML_SEM_CREATE(EBML_CTX_IDX(Context,EltIdx))); } } @@ -197,7 +199,8 @@ bool EbmlMaster::ProcessMandatory() bool EbmlMaster::CheckMandatory() const { - assert(Context.GetSize() != 0); + if(Context.GetSize() == 0) + throw EbmlError("Cannot continue when context size is 0"); unsigned int EltIdx; for (EltIdx = 0; EltIdx < EBML_CTX_SIZE(Context); EltIdx++) { @@ -222,7 +225,8 @@ bool EbmlMaster::CheckMandatory() const std::vector EbmlMaster::FindAllMissingElements() { - assert(Context.GetSize() != 0); + if(Context.GetSize() == 0) + throw EbmlError("Cannot continue when context size is 0"); std::vector missingElements;