Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace asserts with exceptions #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions ebml/EbmlCrc32.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
#ifndef LIBEBML_CRC32_H
#define LIBEBML_CRC32_H

#include <cassert>

#include "EbmlTypes.h"
#include "EbmlBinary.h"

Expand Down
51 changes: 51 additions & 0 deletions ebml/EbmlExceptions.h
Original file line number Diff line number Diff line change
@@ -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 [email protected] if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#ifndef LIBEBML_EXCEPTIONS_H
#define LIBEBML_EXCEPTIONS_H

#include <exception>
#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
2 changes: 0 additions & 2 deletions ebml/EbmlSInteger.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
#ifndef LIBEBML_SINTEGER_H
#define LIBEBML_SINTEGER_H

#include <cassert>

#include "EbmlTypes.h"
#include "EbmlElement.h"

Expand Down
2 changes: 0 additions & 2 deletions ebml/IOCallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@

#include "EbmlTypes.h"

#include <cassert>
#include <exception>
#include <cstdio>
// #include <iostream>


START_LIBEBML_NAMESPACE
Expand Down
5 changes: 3 additions & 2 deletions src/EbmlBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
\author Steve Lhomme <robux4 @ users.sf.net>
\author Julien Coloos <suiryc @ users.sf.net>
*/
#include <cassert>
#include <string>

#include "ebml/EbmlExceptions.h"
#include "ebml/EbmlBinary.h"
#include "ebml/StdIOCallback.h"

Expand All @@ -53,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());
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/EbmlDate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
\version \$Id: EbmlDate.cpp 1079 2005-03-03 13:18:14Z robux4 $
\author Steve Lhomme <robux4 @ users.sf.net>
*/
#include <cassert>

#include "ebml/EbmlDate.h"

START_LIBEBML_NAMESPACE
Expand Down
30 changes: 19 additions & 11 deletions src/EbmlElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
\author Steve Lhomme <robux4 @ users.sf.net>
*/

#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stdexcept>
#include <sstream>

#include "ebml/EbmlElement.h"
#include "ebml/EbmlExceptions.h"
#include "ebml/EbmlMaster.h"
#include "ebml/EbmlStream.h"
#include "ebml/EbmlVoid.h"
Expand Down Expand Up @@ -217,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);
if(i>=Size)
throw EbmlError("Invalid index");
if (i<Size)
return MyTable[i];

Expand Down Expand Up @@ -260,7 +262,8 @@ EbmlElement::EbmlElement(const EbmlElement & ElementToClone)

EbmlElement::~EbmlElement()
{
assert(!bLocked);
if(bLocked)
throw EbmlError("Destructor called while element is locked");
}

/*!
Expand Down Expand Up @@ -382,7 +385,8 @@ EbmlElement * EbmlElement::FindNextElement(IOCallback & DataStream, const EbmlSe
do {
// read a potential ID
do {
assert(ReadIndex < 16);
if(ReadIndex >= 16)
throw EbmlError("Invalid state");
// build the ID with the current Read Buffer
bFound = false;
binary IdBitMask = 1 << 7;
Expand Down Expand Up @@ -494,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 {
Expand Down Expand Up @@ -530,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 {
Expand Down Expand Up @@ -560,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--;
Expand Down Expand Up @@ -602,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;
Expand All @@ -614,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<int64>(SupposedSize) != (0-1))
assert(WrittenSize == SupposedSize);
if(WrittenSize != SupposedSize)
throw EbmlError("Written size is not as expected!");
#endif // LIBEBML_DEBUG
result += WrittenSize;
return result;
Expand Down
2 changes: 0 additions & 2 deletions src/EbmlFloat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
\author Steve Lhomme <robux4 @ users.sf.net>
*/

#include <cassert>

#include "ebml/EbmlFloat.h"

START_LIBEBML_NAMESPACE
Expand Down
25 changes: 14 additions & 11 deletions src/EbmlMaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
\author Steve Lhomme <robux4 @ users.sf.net>
*/

#include <cassert>
#include <algorithm>

#include "ebml/EbmlExceptions.h"
#include "ebml/EbmlMaster.h"
#include "ebml/EbmlStream.h"
#include "ebml/EbmlContexts.h"
Expand Down Expand Up @@ -72,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;

Expand All @@ -92,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
Expand Down Expand Up @@ -134,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;

Expand Down Expand Up @@ -184,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)));
}
}
Expand All @@ -198,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++) {
Expand All @@ -223,7 +225,8 @@ bool EbmlMaster::CheckMandatory() const

std::vector<std::string> EbmlMaster::FindAllMissingElements()
{
assert(Context.GetSize() != 0);
if(Context.GetSize() == 0)
throw EbmlError("Cannot continue when context size is 0");

std::vector<std::string> missingElements;

Expand Down
1 change: 0 additions & 1 deletion src/EbmlSInteger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
\author Steve Lhomme <robux4 @ users.sf.net>
\author Moritz Bunkus <moritz @ bunkus.org>
*/
#include <cassert>
#include <limits>

#include "ebml/EbmlSInteger.h"
Expand Down
2 changes: 0 additions & 2 deletions src/EbmlString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
\version \$Id$
\author Steve Lhomme <robux4 @ users.sf.net>
*/
#include <cassert>

#include "ebml/EbmlString.h"

START_LIBEBML_NAMESPACE
Expand Down
2 changes: 0 additions & 2 deletions src/EbmlUInteger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
\author Steve Lhomme <robux4 @ users.sf.net>
\author Moritz Bunkus <moritz @ bunkus.org>
*/
#include <cassert>

#include "ebml/EbmlUInteger.h"

START_LIBEBML_NAMESPACE
Expand Down
2 changes: 0 additions & 2 deletions src/EbmlUnicodeString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
\author Jory Stone <jcsston @ toughguy.net>
*/

#include <cassert>

#include "ebml/EbmlUnicodeString.h"

#include "lib/utf8-cpp/source/utf8/checked.h"
Expand Down
1 change: 0 additions & 1 deletion src/StdIOCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
\author Moritz Bunkus <moritz @ bunkus.org>
*/

#include <cassert>
#include <climits>
#if !defined(__GNUC__) || (__GNUC__ > 2)
#include <sstream>
Expand Down
2 changes: 0 additions & 2 deletions src/platform/win32/WinIOCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
\author Cyrius <suiryc @ users.sf.net>
*/

#include <cassert>

#include "WinIOCallback.h"

#include "ebml/Debug.h"
Expand Down