Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
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.
 
 
 
 

222 lines
7.3 KiB

  1. #include "rangecheck.h"
  2. #include <assert.h>
  3. #include <stdio.h>
  4. #include <inttypes.h>
  5. #include <limits.h>
  6. void check_minmax() {
  7. /* make sure the __MIN and __MAX macros work */
  8. assert(__MIN(signed char)==-128);
  9. assert(__MAX(signed char)==127);
  10. assert(__MIN(unsigned char)==0);
  11. assert(__MAX(unsigned char)==255);
  12. assert(__MIN(short)==SHRT_MIN);
  13. assert(__MAX(short)==SHRT_MAX);
  14. assert(__MIN(unsigned short)==0);
  15. assert(__MAX(unsigned short)==USHRT_MAX);
  16. assert(__MIN(int)==INT_MIN);
  17. assert(__MAX(int)==INT_MAX);
  18. assert(__MIN(unsigned int)==0);
  19. assert(__MAX(unsigned int)==UINT_MAX);
  20. assert(__MIN(long)==LONG_MIN);
  21. assert(__MAX(long)==LONG_MAX);
  22. assert(__MIN(unsigned long)==0);
  23. assert(__MAX(unsigned long)==ULONG_MAX);
  24. #ifdef LLONG_MIN
  25. assert(__MIN(long long)==LLONG_MIN);
  26. assert(__MAX(long long)==LLONG_MAX);
  27. assert(__MIN(unsigned long long)==0);
  28. assert(__MAX(unsigned long long)==ULLONG_MAX);
  29. #endif
  30. assert(__MIN(int32_t)==(int32_t)0x80000000);
  31. assert(__MAX(int32_t)==0x7fffffff);
  32. assert(__MIN(uint32_t)==0);
  33. assert(__MAX(uint32_t)==0xffffffff);
  34. }
  35. void check_assign() {
  36. int a;
  37. assert(assign(a,5)==0 && a==5);
  38. a=23; assert(assign(a,(unsigned int)-23)==1);
  39. #ifdef LLONG_MIN
  40. a=23; assert(assign(a,LLONG_MAX)==1);
  41. #endif
  42. }
  43. void check_rangeptrbuf() {
  44. char buf[1000];
  45. /* does range_ptrinbuf check all the incoming pointer cases right? */
  46. assert(range_ptrinbuf(buf,sizeof(buf),0)==0);
  47. assert(range_ptrinbuf(buf,sizeof(buf),buf-1)==0);
  48. assert(range_ptrinbuf(buf,sizeof(buf),buf)==1);
  49. assert(range_ptrinbuf(buf,sizeof(buf),buf+50)==1);
  50. assert(range_ptrinbuf(buf,sizeof(buf),buf+sizeof(buf))==0);
  51. /* what if we give it an invalid buffer? */
  52. assert(range_ptrinbuf(0,sizeof(buf),0)==0);
  53. assert(range_ptrinbuf(buf,(unsigned long)-1,buf+1)==0);
  54. /* see if range_validbuf works */
  55. assert(range_validbuf(buf,sizeof(buf))==1);
  56. assert(range_validbuf(0,sizeof(buf))==0);
  57. assert(range_validbuf(buf,(unsigned long)-1)==0);
  58. /* range_bufinbuf */
  59. assert(range_bufinbuf(buf,sizeof(buf),buf,sizeof(buf))==1);
  60. assert(range_bufinbuf(buf,sizeof(buf),buf,sizeof(buf)+1)==0);
  61. assert(range_bufinbuf(buf,sizeof(buf),buf-1,sizeof(buf))==0);
  62. assert(range_bufinbuf(buf-1,sizeof(buf)+1,buf,sizeof(buf))==1);
  63. assert(range_bufinbuf(buf-1,sizeof(buf)+2,buf,sizeof(buf))==1);
  64. assert(range_bufinbuf(0,sizeof(buf),(void*)1,1)==0);
  65. assert(range_bufinbuf(buf,(unsigned long)-1,buf,1)==0);
  66. /* the quintessential integer overflow exploit scenario */
  67. assert(range_bufinbuf(buf,sizeof(buf),buf+10,(unsigned long)-5)==0);
  68. assert(range_bufinbuf(buf,sizeof(buf),buf+10,sizeof(buf))==0);
  69. assert(range_bufinbuf(buf,sizeof(buf),buf+10,sizeof(buf)-10)==1);
  70. /* range_arrayinbuf */
  71. assert(range_arrayinbuf(buf,sizeof(buf),buf,1,10)==1);
  72. assert(range_arrayinbuf(buf,sizeof(buf),buf+sizeof(buf)-10,1,10)==1);
  73. assert(range_arrayinbuf(buf,sizeof(buf),buf-1,1,10)==0);
  74. assert(range_arrayinbuf(buf,sizeof(buf),buf+1,1,1000)==0);
  75. assert(range_arrayinbuf(buf,sizeof(buf),buf-1,1,1002)==0);
  76. /* now overflow the array parameters */
  77. assert(range_arrayinbuf(buf,sizeof(buf),buf+10,0x10000,0x10000)==0);
  78. assert(range_arrayinbuf(buf,sizeof(buf),buf+10,0x80000000,2)==0);
  79. assert(range_arrayinbuf(buf,sizeof(buf),buf+10,0xffffffff,1)==0);
  80. /* range_strinbuf */
  81. assert(range_strinbuf(buf,sizeof(buf),0)==0);
  82. assert(range_strinbuf(buf,sizeof(buf),buf+sizeof(buf))==0);
  83. {
  84. char* x="fnord";
  85. assert(range_strinbuf(x,5,x)==0);
  86. assert(range_strinbuf(x,6,x)==1);
  87. assert(range_strinbuf(x,6,x+5)==1);
  88. assert(range_strinbuf(x,6,x+6)==0);
  89. }
  90. /* range_str2inbuf */
  91. assert(range_str2inbuf(buf,sizeof(buf),0)==0);
  92. assert(range_str2inbuf(buf,sizeof(buf),buf+sizeof(buf))==0);
  93. {
  94. uint16_t y[6];
  95. int i;
  96. for (i=0; i<6; ++i) y[i]="fnord"[i];
  97. assert(range_str2inbuf(y,5*2,y)==0);
  98. assert(range_str2inbuf(y,5*2+1,y)==0);
  99. assert(range_str2inbuf(y,sizeof(y),y)==1);
  100. assert(range_str2inbuf(y,sizeof(y),y+5)==1);
  101. assert(range_str2inbuf(y,sizeof(y),y+6)==0);
  102. }
  103. /* range_str4inbuf */
  104. assert(range_str4inbuf(buf,sizeof(buf),0)==0);
  105. assert(range_str4inbuf(buf,sizeof(buf),buf+sizeof(buf))==0);
  106. {
  107. uint32_t y[6];
  108. int i;
  109. for (i=0; i<6; ++i) y[i]="fnord"[i];
  110. assert(range_str4inbuf(y,5*4,y)==0);
  111. assert(range_str4inbuf(y,5*4+3,y)==0);
  112. assert(range_str4inbuf(y,sizeof(y),y)==1);
  113. assert(range_str4inbuf(y,sizeof(y),y+5)==1);
  114. assert(range_str4inbuf(y,sizeof(y),y+6)==0);
  115. }
  116. assert(range_ptrinbuf2(buf,buf+sizeof(buf),buf));
  117. assert(range_ptrinbuf2(buf+sizeof(buf),buf,buf)==0);
  118. assert(range_ptrinbuf2(buf,buf+sizeof(buf),buf+sizeof(buf)-1));
  119. assert(range_ptrinbuf2(buf,buf+sizeof(buf),buf+sizeof(buf))==0);
  120. assert(range_ptrinbuf2(buf,buf,buf)==0);
  121. assert(range_ptrinbuf2(0,buf+100,buf)==0);
  122. assert(range_validbuf2(buf,buf+100));
  123. assert(range_validbuf2(buf,buf-1)==0);
  124. assert(range_validbuf2(buf,buf));
  125. assert(range_validbuf2(NULL,buf+100)==0);
  126. }
  127. void check_intof() {
  128. /* since the macros are type independent, we only check one signed and
  129. * one unsigned type */
  130. /* some checks are redundant, but we want to cover off-by-one cases */
  131. {
  132. int a;
  133. a=0; assert(add_of(a,INT_MAX-3,1)==0 && a==INT_MAX-2);
  134. a=0; assert(add_of(a,INT_MAX-3,5)==1);
  135. a=0; assert(add_of(a,INT_MAX-3,3)==0 && a==INT_MAX);
  136. a=0; assert(add_of(a,INT_MAX-3,4)==1);
  137. a=0; assert(add_of(a,0,INT_MIN)==0 && a==INT_MIN);
  138. a=0; assert(add_of(a,-1,INT_MIN)==1);
  139. a=0; assert(add_of(a,1,INT_MAX)==1);
  140. /* now let's see what happens if the addition has a wider data
  141. * type than the destionation */
  142. a=0; assert(add_of(a,0x100000000ll,-0x80000000ll)==1);
  143. a=0; assert(add_of(a,0x100000000ll,-0x80000001ll)==0 && a==0x7fffffff);
  144. a=0; assert(add_of(a,0x100000000ll,-10)==1);
  145. a=0; assert(add_of(a,-0x90000000ll,0x10000000)==0 && a==0x80000000);
  146. /* what if we add two ints, the result does not overflow but is
  147. * still negative, but we assign it to an unsigned int? */
  148. a=0; assert(add_of(a,0x7ffffff0,0x10)==1);
  149. }
  150. {
  151. unsigned int a;
  152. a=0; assert(add_of(a,UINT_MAX-3,1)==0 && a==UINT_MAX-2);
  153. a=0; assert(add_of(a,UINT_MAX-3,5)==1);
  154. a=0; assert(add_of(a,UINT_MAX-3,4)==1);
  155. a=0; assert(add_of(a,2,-3)==1);
  156. a=23; assert(add_of(a,2,-2)==0 && a==0);
  157. a=0; assert(add_of(a,(int)0x80000000,(int)-2147483648)==0 && a==0);
  158. a=0; assert(add_of(a,(int)0x7fffffff,(int)-2147483648)==1);
  159. a=0; assert(add_of(a,1,UINT_MAX)==1);
  160. /* now let's see what happens if the addition has a wider data
  161. * type than the destionation */
  162. a=0; assert(add_of(a,0x100000000ll,-5)==0 && a==4294967291);
  163. a=0; assert(add_of(a,-0x100000010ll,5)==1);
  164. a=0; assert(add_of(a,0x100000010ll,-5)==1);
  165. /* what if we add two ints, the result does not overflow but is
  166. * still negative, but we assign it to an unsigned int? */
  167. a=0; assert(add_of(a,-10,-5)==1);
  168. }
  169. {
  170. int a;
  171. a=0; assert(sub_of(a,10,5)==0 && a==5);
  172. a=0; assert(sub_of(a,5,10)==0 && a==-5);
  173. a=0; assert(sub_of(a,INT_MIN,10)==1);
  174. a=0; assert(sub_of(a,INT_MIN,-10)==0 && a==INT_MIN+10);
  175. a=0; assert(sub_of(a,INT_MAX,-10)==1);
  176. a=0; assert(sub_of(a,INT_MAX,10)==0 && a==INT_MAX-10);
  177. }
  178. {
  179. unsigned long long a;
  180. a=0; assert(add_of(a,0xfffffff0ul,0x10)==0 && a==0x100000000);
  181. }
  182. }
  183. int main() {
  184. check_minmax();
  185. check_assign();
  186. check_intof();
  187. check_rangeptrbuf();
  188. puts("all tests ok");
  189. return 0;
  190. }