Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
1.8 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation
  3. Module Name:
  4. toverflow.c
  5. Abstract:
  6. Test program for overflow functions
  7. Author:
  8. Jay Krell (Jaykrell)
  9. Revision History:
  10. --*/
  11. #include "nt.h"
  12. #include "ntrtl.h"
  13. #include "nturtl.h"
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include "windows.h"
  17. const unsigned __int32 numbers[]=
  18. {
  19. 0, 0,
  20. 1, 1,
  21. 0x7fff, 0x7fff,
  22. 0x7fff, 0x8000,
  23. 0x8000, 0x8000,
  24. 0x7fffffff, 0x7fffffff,
  25. 0x80000000, 0x7fffffff,
  26. 0x80000000, 0x80000000,
  27. 0x80000000, 0xffffffff,
  28. 0x7fffffff, 0xffffffff,
  29. 0xffffffff, 0xffffffff,
  30. 0x80000001, 0xffffffff,
  31. 0x80000001, 0x7fffffff,
  32. };
  33. void
  34. F(
  35. __int32 *p
  36. );
  37. void
  38. TestOverflow(
  39. )
  40. {
  41. SIZE_T i = 0;
  42. unsigned __int32 ua32 = 0;
  43. unsigned __int32 ub32 = 0;
  44. unsigned __int32 uc32 = 0;
  45. unsigned __int64 ua64 = 0;
  46. unsigned __int64 ub64 = 0;
  47. unsigned __int64 uc64 = 0;
  48. __int32 a32 = 0;
  49. __int32 b32 = 0;
  50. __int32 c32 = 0;
  51. __int64 a64 = 0;
  52. __int64 b64 = 0;
  53. __int64 c64 = 0;
  54. BOOLEAN carry = 0;
  55. BOOLEAN overflow = 0;
  56. for (i = 0 ; i != RTL_NUMBER_OF(numbers) ; i += 2)
  57. {
  58. ua32 = numbers[i];
  59. ub32 = numbers[i+1];
  60. ua64 = ua32;
  61. ub64 = ub32;
  62. carry = RtlUnsignedAddWithCarryOut32(&uc32, ua32, ub32);
  63. printf("unsigned add32: 0x%I64x + 0x%I64x => carry=%d\n", ua64, ub64, (int)carry);
  64. a32 = (__int32)ua32;
  65. b32 = (__int32)ub32;
  66. a64 = a32;
  67. b64 = b32;
  68. overflow = RtlSignedAddWithOverflowOut32(&c32, a32, b32);
  69. printf("signed add32: %I64d + %I64d => overflow=%d\n", a64, b64, (int)overflow);
  70. }
  71. }
  72. int
  73. __cdecl
  74. main(
  75. int argc,
  76. char **argv
  77. )
  78. {
  79. TestOverflow();
  80. return 0;
  81. }