Really handle edge cases for C string to int32

This commit is contained in:
UnknownShadow200 2017-08-23 15:08:02 +10:00
parent b346307ef3
commit 123cadf9c8
3 changed files with 25 additions and 15 deletions

View File

@ -61,20 +61,29 @@ bool Convert_TryParseInt32(STRING_TRANSIENT String* str, Int32* value) {
if (c < '0' || c > '9') return false;
Int32 digit = c - '0';
/* Cannot add another digit without overflow */
if (sum >= (Int32)214748364) return false;
sum *= 10;
/* Can only add a certain digit here */
if (sum >= (Int32)2147483639) {
/* Potential for adding digit to overflow */
while (digit > 0) {
if (sum == Int32_MaxValue) return false;
sum++; digit--;
}
} else {
sum += digit;
/* Magnitude of largest negative integer cannot be expressed
as a positive integer, so this case must be specially handled. */
if (sum == (Int32)214748364 && digit == 8 && negate) {
*value = Int32_MinValue;
return true;
}
/* Overflow handling */
if (sum >= (Int32)214748364) {
Int32 diff = sum - (Int32)214748364;
diff *= 10; diff += digit;
/* Handle magnitude of max negative value specially,
as it cannot be represented as a positive integer */
if (diff == 8 && negate) {
*value = Int32_MinValue;
return true;
}
/* Overflows max positive value */
if (diff > 7) return false;
}
sum *= 10; sum += digit;
}
if (negate) sum = -sum;

View File

@ -40,7 +40,7 @@ Int32 Atlas2D_LoadTextureElement_Raw(TextureLoc texLoc, Bitmap* element) {
Bitmap_CopyBlock(x * size, y * size, 0, 0,
&Atlas2D_Bitmap, element, size);
return Gfx_CreateTexture(element, true);
return Gfx_CreateTexture(element, true, Gfx_Mipmaps);
}
void Atlas2D_Free(void) {
@ -110,7 +110,7 @@ void Atlas1D_Make1DTexture(Int32 i, Int32 atlas1DHeight, Int32* index) {
(*index)++;
}
Atlas1D_TexIds[i] = Gfx_CreateTexture(&atlas1D, true);
Atlas1D_TexIds[i] = Gfx_CreateTexture(&atlas1D, true, Gfx_Mipmaps);
Platform_MemFree(atlas1D.Scan0);
}

View File

@ -44,6 +44,7 @@ typedef UInt8 TextureLoc;
#define UInt8_MaxValue ((UInt8)0xFF)
#define Int16_MaxValue ((Int16)0x7FFF)
#define Int32_MaxValue ((Int32)0x7FFFFFFFL)
#define Int32_MinValue ((Int32)0xFFFFFFFFL)
#define USE_DX true
#endif