Skip to content

Commit

Permalink
Converted some asserts to errors to catch more stuff in release mode
Browse files Browse the repository at this point in the history
  • Loading branch information
CromFr committed Oct 12, 2023
1 parent d623237 commit 0c476c8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
6 changes: 3 additions & 3 deletions source/nwn/fastgff.d
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ struct GffResRef{

///
this(in char[] value){
assert(value.length <= 32, "Resref cannot be longer than 32 characters");
enforce(value.length <= 32, "Resref cannot be longer than 32 characters");
data[0 .. value.length] = value;
if(value.length < data.length)
data[value.length .. $] = 0;
Expand All @@ -114,7 +114,7 @@ struct GffResRef{

version(FastGffWrite)
void opAssign(in string str){
assert(str.length <= 32, "Value is too long");
enforce(str.length <= 32, "Value is too long");
data = str.stringToCharArray!(char[32]);
}

Expand Down Expand Up @@ -285,7 +285,7 @@ private:
struct GffList{
/// Get nth child GffStruct
const(GffStruct) opIndex(size_t index) const{
assert(gff !is null && index < length, "Out of bound");
enforce(gff !is null && index < length, "Out of bound");
return const(GffStruct)(gff, gff.getStruct(structIndexList[index]));
}

Expand Down
24 changes: 12 additions & 12 deletions source/nwn/gff.d
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ private:
private struct GffRawParser{
@disable this();
this(in ubyte[] _data){
assert(_data.length >= RawHeader.sizeof, "Data length is so small it cannot even contain the header");
enforce(_data.length >= RawHeader.sizeof, "Data length is so small it cannot even contain the header");

data = _data;
headerPtr = cast(const RawHeader*) (data.ptr);
Expand All @@ -844,7 +844,7 @@ private struct GffRawParser{
fieldIndicesPtr = cast(const RawFieldIndices*)(data.ptr + headerPtr.field_indices_offset);
listIndicesPtr = cast(const RawListIndices*) (data.ptr + headerPtr.list_indices_offset);

assert(data.length == headerPtr.list_indices_offset+headerPtr.list_indices_count,
enforce(data.length == headerPtr.list_indices_offset+headerPtr.list_indices_count,
"Data length do not match header");
}

Expand Down Expand Up @@ -910,27 +910,27 @@ private struct GffRawParser{
const void* listIndicesPtr;

const(RawStruct*) getRawStruct(in size_t index) const {
assert(index < headerPtr.struct_count, "index "~index.to!string~" out of bounds");
enforce(index < headerPtr.struct_count, "index "~index.to!string~" out of bounds");
return &structsPtr[index];
}
const(RawField*) getRawField(in size_t index) const {
assert(index < headerPtr.field_count, "index "~index.to!string~" out of bounds");
enforce(index < headerPtr.field_count, "index "~index.to!string~" out of bounds");
return &fieldsPtr[index];
}
const(RawLabel*) getRawLabel(in size_t index) const {
assert(index < headerPtr.label_count, "index "~index.to!string~" out of bounds");
enforce(index < headerPtr.label_count, "index "~index.to!string~" out of bounds");
return &labelsPtr[index];
}
const(RawFieldData*) getRawFieldData(in size_t offset) const {
assert(offset < headerPtr.field_data_count, "offset "~offset.to!string~" out of bounds");
enforce(offset < headerPtr.field_data_count, "offset "~offset.to!string~" out of bounds");
return cast(const RawFieldData*)(fieldDatasPtr + offset);
}
const(RawFieldIndices*) getRawFieldIndices(in size_t offset) const {
assert(offset < headerPtr.field_indices_count, "offset "~offset.to!string~" out of bounds");
enforce(offset < headerPtr.field_indices_count, "offset "~offset.to!string~" out of bounds");
return cast(const RawFieldIndices*)(fieldIndicesPtr + offset);
}
const(RawListIndices*) getRawListIndices(in size_t offset) const {
assert(offset < headerPtr.list_indices_count, "offset "~offset.to!string~" out of bounds");
enforce(offset < headerPtr.list_indices_count, "offset "~offset.to!string~" out of bounds");
return cast(const RawListIndices*)(listIndicesPtr + offset);
}

Expand Down Expand Up @@ -1217,7 +1217,7 @@ private struct GffRawSerializer{
gff_verbose_rtIndent ~= "";
}

assert(label.length <= 16, "Label too long");//TODO: Throw exception on GffNode.label set
enforce(label.length <= 16, "Label too long");//TODO: Throw exception on GffNode.label set

if(auto i = (label in knownLabels)){
fields[createdFieldIndex].label_index = *i;
Expand Down Expand Up @@ -1260,7 +1260,7 @@ private struct GffRawSerializer{
fieldDatas ~= (cast(ubyte*)value.get!GffString.ptr)[0..strLen].dup;
break;
case ResRef:
assert(value.get!GffResRef.length <= 32, "Resref too long (max length: 32 characters)");
enforce(value.get!GffResRef.length <= 32, "Resref too long (max length: 32 characters)");
immutable strLen = cast(uint8_t)value.get!GffResRef.length;

fields[createdFieldIndex].data_or_data_offset = cast(uint32_t)fieldDatas.length;
Expand Down Expand Up @@ -1331,11 +1331,11 @@ private struct GffRawSerializer{
ubyte[] serialize(Gff gff){
registerStruct(gff.root);

assert(gff.fileType.length <= 4);
enforce(gff.fileType.length <= 4);
header.file_type = " ";
header.file_type[0..gff.fileType.length] = gff.fileType.dup;

assert(gff.fileVersion.length <= 4);
enforce(gff.fileVersion.length <= 4);
header.file_version = " ";
header.file_version[0..gff.fileVersion.length] = gff.fileVersion.dup;

Expand Down

0 comments on commit 0c476c8

Please sign in to comment.