1
0
mirror of https://github.com/cc65/cc65.git synced 2024-06-07 23:29:39 +00:00

Check if the integer size is known in GetIntegerTypeMin/Max() to prevent potential misuse.

This commit is contained in:
acqn 2020-07-27 19:20:07 +08:00 committed by Oliver Schmidt
parent 0f412b6beb
commit c37f9f1a41
2 changed files with 20 additions and 4 deletions

View File

@ -275,8 +275,14 @@ const Type* GetStructReplacementType (const Type* SType)
long GetIntegerTypeMin (const Type* Type)
/* Get the smallest possible value of the integer type */
/* Get the smallest possible value of the integer type.
** The type must have a known size.
*/
{
if (SizeOf (Type) == 0) {
Internal ("Incomplete type used in GetIntegerTypeMin");
}
if (IsSignSigned (Type)) {
/* The smallest possible signed value of N-byte integer is -pow(2, 8*N-1) */
return (long)((unsigned long)(-1L) << (CHAR_BITS * SizeOf (Type) - 1U));
@ -288,8 +294,14 @@ long GetIntegerTypeMin (const Type* Type)
unsigned long GetIntegerTypeMax (const Type* Type)
/* Get the largest possible value of the integer type */
/* Get the largest possible value of the integer type.
** The type must have a known size.
*/
{
if (SizeOf (Type) == 0) {
Internal ("Incomplete type used in GetIntegerTypeMax");
}
if (IsSignSigned (Type)) {
/* Min signed value of N-byte integer is pow(2, 8*N-1) - 1 */
return (1UL << (CHAR_BITS * SizeOf (Type) - 1U)) - 1UL;

View File

@ -249,10 +249,14 @@ const Type* GetStructReplacementType (const Type* SType);
/* Get a replacement type for passing a struct/union in the primary register */
long GetIntegerTypeMin (const Type* Type);
/* Get the smallest possible value of the integer type */
/* Get the smallest possible value of the integer type.
** The type must have a known size.
*/
unsigned long GetIntegerTypeMax (const Type* Type);
/* Get the largest possible value of the integer type */
/* Get the largest possible value of the integer type.
** The type must have a known size.
*/
Type* PointerTo (const Type* T);
/* Return a type string that is "pointer to T". The type string is allocated