Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C - C++ Learning > Back to School ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 5 Topic 4104 of 4218
Post > Topic >>

Back to School with NoName: Lesson 2, Integer Range Checking

by "NoName" <NoName@[EMAIL PROTECTED] > Apr 12, 2008 at 03:30 AM

Lesson 1, Identified 4 weaknesses.

Lesson 2, Identifies 16 weaknesses.

(Think positive: at least the compiler, compiles, and the linker, links, 
which cannot be said for competing products of competitors.)

See comments or see website for code/zips:

http://members.home.nl/hbthouppermans/VS2008Analysis/Index.htm

// *** Begin of Main.cpp ***

#include <stdio.h>

typedef char int8;
typedef short int int16;
typedef int int32;
typedef long long int64;

typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned long long uint64;

/*

12 april 2008

Back to School:

Lesson 2, Integer Range Checking.

Conclusions:

Underflow for int8 not detected, BAD
Overflow for int8 not detected, BAD

Underflow for uint8 not detected, BAD
Overflow for uint8 not detected, BAD

Underflow for int16 not detected, BAD
Overflow for int16 not detected, BAD

Underflow for uint16 not detected, BAD
Overflow for uint16 not detected, BAD

Underflow for int32 not detected, BAD
Overflow for int32 not detected, BAD

Underflow for uint32 not detected, BAD
Overflow for uint32 not detected, BAD

Underflow for int64 not detected, BAD
Overflow for int64 not detected, BAD

Underflow for uint64 not detected, BAD
Overflow for uint64 not detected, BAD

16 out of 16 tests failed.

Score: 0 out of 10

*/

int main()
{
    printf("Program Started \n\n");

    int8 vMinInt8;
    int8 vMaxInt8;

    uint8 vMinUint8;
    uint8 vMaxUint8;

    int16 vMinInt16;
    int16 vMaxInt16;

    uint16 vMinUint16;
    uint16 vMaxUint16;

    int32 vMinInt32;
    int32 vMaxInt32;

    uint32 vMinUint32;
    uint32 vMaxUint32;

    int64 vMinInt64;
    int64 vMaxInt64;

    uint64 vMinUint64;
    uint64 vMaxUint64;

    // test int8
    vMinInt8 = -128;
    vMinInt8 = vMinInt8 - 1;    // Underflow not detected, BAD
    printf("vMinInt8: %d \n", vMinInt8 );

    vMaxInt8 = 127;
    vMaxInt8 = vMaxInt8 + 1;    // Overflow not detected, BAD
    printf("vMaxInt8: %d \n\n", vMaxInt8 );

    // test uint8
    vMinUint8 = 0;
    vMinUint8 = vMinUint8 - 1;  // Underflow not detected, BAD
    printf("vMinUint8: %u \n", vMinUint8 );

    vMaxUint8 = 255;
    vMaxUint8 = vMaxUint8 + 1;  // Overflow not detected, BAD
    printf("vMaxUint8: %u \n\n", vMaxUint8 );

    // test int16
    vMinInt16 = -32768;
    vMinInt16 = vMinInt16 - 1;  // Underflow not detected, BAD
    printf("vMinInt16: %d \n", vMinInt16 );

    vMaxInt16 = 32767;
    vMaxInt16 = vMaxInt16 + 1;  // Overflow not detected, BAD
    printf("vMaxInt16: %d \n\n", vMaxInt16 );

    // test uint16
    vMinUint16 = 0;
    vMinUint16 = vMinUint16 - 1;    // Underflow not detected, BAD
    printf("vMinUint16: %u \n", vMinUint16 );

    vMaxUint16 = 65535;
    vMaxUint16 = vMaxUint16 + 1;    // Overflow not detected, BAD
    printf("vMaxUint16: %u \n\n", vMaxUint16 );

    // test int32
    vMinInt32 = 2147483648; // same as -2147483648, solves warning, <- 
weakness in language.
    vMinInt32 = vMinInt32 - 1;      // Underflow not detected, BAD
    printf("vMinInt32: %d \n", vMinInt32 );

    vMaxInt32 = 2147483647;
    vMaxInt32 = vMaxInt32 + 1;      // Overflow not detected, BAD
    printf("vMaxInt32: %d \n\n", vMaxInt32 );

    // test uint32
    vMinUint32 = 0;
    vMinUint32 = vMinUint32 - 1;    // Underflow not detected, BAD
    printf("vMinUint32: %u \n", vMinUint32 );

    vMaxUint32 = 4294967295;
    vMaxUint32 = vMaxUint32 + 1;    // Overflow not detected, BAD
    printf("vMaxUint32: %u \n\n", vMaxUint32 );

    // test int64
    vMinInt64 = 9223372036854775808; // same as -9223372036854775808,
solves 
warning, <- weakness in language
    vMinInt64 = vMinInt64 - 1;      // Underflow not detected, BAD
    printf("vMinInt64: %lld \n", vMinInt64 );

    vMaxInt64 = 9223372036854775807; // 9223372036854775808 would give no 
warning ?!?!, BAD
    vMaxInt64 = vMaxInt64 + 1;      // Overflow not detected, BAD
    printf("vMaxInt64: %lld \n\n", vMaxInt64 );

    // test uint64
    vMinUint64 = 0;
    vMinUint64 = vMinUint64 - 1;    // Underflow not detected, BAD
    printf("vMinUint64: %llu \n", vMinUint64 );

    vMaxUint64 = 18446744073709551615;
    vMaxUint64 = vMaxUint64 + 1;    // Overflow not detected, BAD
    printf("vMaxUint64: %llu \n\n", vMaxUint64 );

    printf("Program finished \n");
}

// *** End of Main.cpp ***
 




 5 Posts in Topic:
Back to School with NoName: Lesson 2, Integer Range Checking
"NoName" <No  2008-04-12 03:30:03 
Re: Back to School with NoName: Lesson 2, Integer Range Checking
Ulrich Eckhardt <dooms  2008-04-14 07:55:30 
Re: Back to School with NoName: Lesson 2, Integer Range Checking
Richard Heathfield <rj  2008-04-14 07:02:34 
Re: Back to School with NoName: Lesson 2, Integer Range Checking
Ulrich Eckhardt <dooms  2008-04-15 23:05:50 
Re: Back to School with NoName: Lesson 2, Integer Range Checking
"TheDevil" <  2008-04-15 01:09:17 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 12:48:44 CDT 2008.