6252: Use distinct error return codes for `CSnapshotDelta::UnpackDelta` r=heinrich5991 a=Robyt3

Use a different error code for every return statement, so it's easier to determine why unpacking a delta failed.

The codes are grouped by the first digit of the error code:

- `-1xx`: not enough data to read
- `-2xx`: value is invalid
- `-3xx`: could not build snapshot item

<!-- What is the motivation for the changes of this pull request? -->

<!-- Note that builds and other checks will be run for your change. Don't feel intimidated by failures in some of the checks. If you can't resolve them yourself, experienced devs can also resolve them before merging your pull request. -->

## Checklist

- [ ] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test (especially base/) or added coverage to integration test
- [ ] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Robert Müller <robytemueller@gmail.com>
This commit is contained in:
bors[bot] 2023-01-08 13:56:54 +00:00 committed by GitHub
commit 7d4fabd7f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -372,10 +372,10 @@ int CSnapshotDelta::UnpackDelta(CSnapshot *pFrom, CSnapshot *pTo, const void *pS
// unpack deleted stuff // unpack deleted stuff
int *pDeleted = pData; int *pDeleted = pData;
if(pDelta->m_NumDeletedItems < 0) if(pDelta->m_NumDeletedItems < 0)
return -1; return -201;
pData += pDelta->m_NumDeletedItems; pData += pDelta->m_NumDeletedItems;
if(pData > pEnd) if(pData > pEnd)
return -1; return -101;
// copy all non deleted stuff // copy all non deleted stuff
for(int i = 0; i < pFrom->NumItems(); i++) for(int i = 0; i < pFrom->NumItems(); i++)
@ -396,7 +396,7 @@ int CSnapshotDelta::UnpackDelta(CSnapshot *pFrom, CSnapshot *pTo, const void *pS
{ {
void *pObj = Builder.NewItem(pFromItem->Type(), pFromItem->ID(), ItemSize); void *pObj = Builder.NewItem(pFromItem->Type(), pFromItem->ID(), ItemSize);
if(!pObj) if(!pObj)
return -4; return -301;
// keep it // keep it
mem_copy(pObj, pFromItem->Data(), ItemSize); mem_copy(pObj, pFromItem->Data(), ItemSize);
@ -407,15 +407,15 @@ int CSnapshotDelta::UnpackDelta(CSnapshot *pFrom, CSnapshot *pTo, const void *pS
for(int i = 0; i < pDelta->m_NumUpdateItems; i++) for(int i = 0; i < pDelta->m_NumUpdateItems; i++)
{ {
if(pData + 2 > pEnd) if(pData + 2 > pEnd)
return -1; return -102;
const int Type = *pData++; const int Type = *pData++;
if(Type < 0 || Type > CSnapshot::MAX_TYPE) if(Type < 0 || Type > CSnapshot::MAX_TYPE)
return -3; return -202;
const int ID = *pData++; const int ID = *pData++;
if(ID < 0 || ID > CSnapshot::MAX_ID) if(ID < 0 || ID > CSnapshot::MAX_ID)
return -3; return -203;
int ItemSize; int ItemSize;
if(Type < MAX_NETOBJSIZES && m_aItemSizes[Type]) if(Type < MAX_NETOBJSIZES && m_aItemSizes[Type])
@ -423,14 +423,14 @@ int CSnapshotDelta::UnpackDelta(CSnapshot *pFrom, CSnapshot *pTo, const void *pS
else else
{ {
if(pData + 1 > pEnd) if(pData + 1 > pEnd)
return -2; return -103;
if(*pData < 0 || *pData > INT_MAX / 4) if(*pData < 0 || *pData > INT_MAX / 4)
return -3; return -204;
ItemSize = (*pData++) * 4; ItemSize = (*pData++) * 4;
} }
if(ItemSize < 0 || RangeCheck(pEnd, pData, ItemSize)) if(ItemSize < 0 || RangeCheck(pEnd, pData, ItemSize))
return -3; return -205;
const int Key = (Type << 16) | ID; const int Key = (Type << 16) | ID;
@ -440,14 +440,14 @@ int CSnapshotDelta::UnpackDelta(CSnapshot *pFrom, CSnapshot *pTo, const void *pS
pNewData = (int *)Builder.NewItem(Type, ID, ItemSize); pNewData = (int *)Builder.NewItem(Type, ID, ItemSize);
if(!pNewData) if(!pNewData)
return -4; return -302;
const int FromIndex = pFrom->GetItemIndex(Key); const int FromIndex = pFrom->GetItemIndex(Key);
if(FromIndex != -1) if(FromIndex != -1)
{ {
// we got an update so we need to apply the diff // we got an update so we need to apply the diff
if(!UndiffItem(pFrom->GetItem(FromIndex)->Data(), pData, pNewData, ItemSize / 4, &m_aSnapshotDataRate[Type])) if(!UndiffItem(pFrom->GetItem(FromIndex)->Data(), pData, pNewData, ItemSize / 4, &m_aSnapshotDataRate[Type]))
return -3; return -206;
} }
else // no previous, just copy the pData else // no previous, just copy the pData
{ {