Mercurial > pub > dyncall > dyncall
comparison test/callback_suite_aggrs/main.c @ 548:a6d00ee46731
- dyncallback test: added aggregate arg bounds check
author | Tassilo Philipp |
---|---|
date | Mon, 20 Jun 2022 11:04:20 +0200 |
parents | 71c884e610f0 |
children | 5e1002095afa |
comparison
equal
deleted
inserted
replaced
547:917d5d213815 | 548:a6d00ee46731 |
---|---|
66 { | 66 { |
67 char atype; | 67 char atype; |
68 const char* sig = signature; | 68 const char* sig = signature; |
69 int pos = 0; | 69 int pos = 0; |
70 int s = 0; | 70 int s = 0; |
71 int do_bndck = 1; | |
71 while ( (atype = *sig) != '\0') { | 72 while ( (atype = *sig) != '\0') { |
72 switch(atype) { | 73 switch(atype) { |
73 case '_': sig += 2; /* skip cconv prefix */ continue; | 74 case '_': sig += 2; /* skip cconv prefix */ continue; |
74 case ')': ++sig; /* skip ret type separator */ continue; | 75 case ')': ++sig; /* skip ret type separator */ do_bndck = 0; /* no bounds check on retval */ continue; |
75 case 'v': s = (sig > signature) && sig[-1] == ')'; /* assure this was the return type */ break; /*TODO:check that no return-arg was touched.*/ | 76 case 'v': s = (sig > signature) && sig[-1] == ')'; /* assure this was the return type */ break; /*TODO:check that no return-arg was touched.*/ |
76 case 'B': s = ( V_B[pos] == K_B[pos] ); if (!s) printf("'%c':%d: %d != %d ; ", atype, pos, V_B[pos], K_B[pos]); break; | 77 case 'B': s = ( V_B[pos] == K_B[pos] ); if (!s) printf("'%c':%d: %d != %d ; ", atype, pos, V_B[pos], K_B[pos]); break; |
77 case 'c': s = ( V_c[pos] == K_c[pos] ); if (!s) printf("'%c':%d: %d != %d ; ", atype, pos, V_c[pos], K_c[pos]); break; | 78 case 'c': s = ( V_c[pos] == K_c[pos] ); if (!s) printf("'%c':%d: %d != %d ; ", atype, pos, V_c[pos], K_c[pos]); break; |
78 case 's': s = ( V_s[pos] == K_s[pos] ); if (!s) printf("'%c':%d: %d != %d ; ", atype, pos, V_s[pos], K_s[pos]); break; | 79 case 's': s = ( V_s[pos] == K_s[pos] ); if (!s) printf("'%c':%d: %d != %d ; ", atype, pos, V_s[pos], K_s[pos]); break; |
79 case 'i': s = ( V_i[pos] == K_i[pos] ); if (!s) printf("'%c':%d: %d != %d ; ", atype, pos, V_i[pos], K_i[pos]); break; | 80 case 'i': s = ( V_i[pos] == K_i[pos] ); if (!s) printf("'%c':%d: %d != %d ; ", atype, pos, V_i[pos], K_i[pos]); break; |
89 case 'd': s = ( V_d[pos] == K_d[pos] ); if (!s) printf("'%c':%d: %f != %f ; ", atype, pos, V_d[pos], K_d[pos]); break; | 90 case 'd': s = ( V_d[pos] == K_d[pos] ); if (!s) printf("'%c':%d: %f != %f ; ", atype, pos, V_d[pos], K_d[pos]); break; |
90 case '<': /* union */ | 91 case '<': /* union */ |
91 case '{': /* struct */ | 92 case '{': /* struct */ |
92 { | 93 { |
93 /* no check: guaranteed to exist, or invoke func would've exited when passing args, above */ | 94 /* no check: guaranteed to exist, or invoke func would've exited when passing args, above */ |
94 int len; | 95 int len, bndck_i; |
95 int i = find_agg_idx(&len, sig); | 96 int i = find_agg_idx(&len, sig); |
96 s = ((int(*)(const void*,const void*))G_agg_cmpfuncs[i])(V_a[pos], K_a[pos]); | 97 s = ((int(*)(const void*,const void*))G_agg_cmpfuncs[i])(V_a[pos], K_a[pos]); |
97 if (!s) printf("'%c':%d: *%p != *%p ; ", atype, pos, V_a[pos], K_a[pos]); | 98 if (!s) printf("'%c':%d: *%p != *%p ; ", atype, pos, V_a[pos], K_a[pos]); |
98 sig += len-1; /* advance to next arg char; -1 to compensate for ++sig, below */ | 99 sig += len-1; /* advance to next arg char; -1 to compensate for ++sig, below */ |
100 | |
101 /* bounds check */ | |
102 for(bndck_i = 0; do_bndck && bndck_i < AGGR_BND_CHECK_PAD; ++bndck_i) { | |
103 if(((unsigned char*)V_a[pos] + G_agg_sizes[i])[bndck_i] != 0xab) { | |
104 printf("'%c':%d: buffer overflow retrieving aggr arg, target buffer of size %d too small; ", atype, pos, G_agg_sizes[i]); | |
105 s = 0; | |
106 } | |
107 } | |
108 | |
99 break; | 109 break; |
100 } | 110 } |
101 default: printf("unknown atype '%c' ; ", atype); return 0; | 111 default: printf("unknown atype '%c' ; ", atype); return 0; |
102 } | 112 } |
103 if(!s) { | 113 if(!s) { |
135 case DC_SIGCHAR_ULONGLONG: V_L[pos] = dcbArgULongLong(input); break; | 145 case DC_SIGCHAR_ULONGLONG: V_L[pos] = dcbArgULongLong(input); break; |
136 case DC_SIGCHAR_FLOAT: V_f[pos] = dcbArgFloat (input); break; | 146 case DC_SIGCHAR_FLOAT: V_f[pos] = dcbArgFloat (input); break; |
137 case DC_SIGCHAR_DOUBLE: V_d[pos] = dcbArgDouble (input); break; | 147 case DC_SIGCHAR_DOUBLE: V_d[pos] = dcbArgDouble (input); break; |
138 case DC_SIGCHAR_STRING: | 148 case DC_SIGCHAR_STRING: |
139 case DC_SIGCHAR_POINTER: V_p[pos] = dcbArgPointer (input); break; | 149 case DC_SIGCHAR_POINTER: V_p[pos] = dcbArgPointer (input); break; |
140 case DC_SIGCHAR_AGGREGATE: dcbArgAggr (input, V_a[pos]); break; | 150 case DC_SIGCHAR_AGGREGATE: |
151 /* bounds check init */ | |
152 memset(V_a[pos], 0xab, get_max_aggr_size() + AGGR_BND_CHECK_PAD); | |
153 dcbArgAggr(input, V_a[pos]); | |
154 break; | |
155 | |
141 case DC_SIGCHAR_CC_PREFIX: ++signature; /* skip cconv prefix */ continue; | 156 case DC_SIGCHAR_CC_PREFIX: ++signature; /* skip cconv prefix */ continue; |
142 default: assert(0); | 157 default: assert(0); |
143 } | 158 } |
144 ++pos; | 159 ++pos; |
145 } | 160 } |
211 int i = find_agg_idx(&len, signature); | 226 int i = find_agg_idx(&len, signature); |
212 if(i == -1) { | 227 if(i == -1) { |
213 printf("unknown aggr sig at '%s' ;", signature); | 228 printf("unknown aggr sig at '%s' ;", signature); |
214 return 0; | 229 return 0; |
215 } | 230 } |
216 dc_sig[len_sig++] = 'A'; | 231 dc_sig[len_sig++] = DC_SIGCHAR_AGGREGATE; |
217 dc_aggrs[n_aggrs++] = ((DCaggr*(*)())G_agg_touchAfuncs[i])(); | 232 dc_aggrs[n_aggrs++] = ((DCaggr*(*)())G_agg_touchAfuncs[i])(); |
218 signature += len; /* advance to next arg char */ | 233 signature += len; /* advance to next arg char */ |
219 break; | 234 break; |
220 } | 235 } |
221 default: | 236 default: |