Skip to content

Commit f366ace

Browse files
add strncmp() wrapper
1 parent 3ebd00e commit f366ace

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

runtime/LibcWrappers.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,4 +410,30 @@ int SYM(strcmp)(const char *a, const char *b) {
410410
reinterpret_cast<uintptr_t>(SYM(strcmp)));
411411
return result;
412412
}
413+
414+
int SYM(strncmp)(const char *a, const char *b, size_t n) {
415+
tryAlternative(a, _sym_get_parameter_expression(0), SYM(strncmp));
416+
tryAlternative(b, _sym_get_parameter_expression(1), SYM(strncmp));
417+
tryAlternative(n, _sym_get_parameter_expression(2), SYM(strncmp));
418+
419+
auto result = strncmp(a, b, n);
420+
_sym_set_return_expression(nullptr);
421+
422+
if (isConcrete(a, n) && isConcrete(b, n))
423+
return result;
424+
425+
auto aShadowIt = ReadOnlyShadow(a, n).begin_non_null();
426+
auto bShadowIt = ReadOnlyShadow(b, n).begin_non_null();
427+
auto *allEqual = _sym_build_equal(*aShadowIt, *bShadowIt);
428+
for (size_t i = 1; i < n; i++) {
429+
++aShadowIt;
430+
++bShadowIt;
431+
allEqual =
432+
_sym_build_bool_and(allEqual, _sym_build_equal(*aShadowIt, *bShadowIt));
433+
}
434+
435+
_sym_push_path_constraint(allEqual, result == 0,
436+
reinterpret_cast<uintptr_t>(SYM(strncmp)));
437+
return result;
438+
}
413439
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdarg.h>
4+
#include <stdlib.h>
5+
#include <stdint.h>
6+
#include <unistd.h>
7+
int main(int argc, char *argv[]) {
8+
9+
char buf[1024];
10+
ssize_t i;
11+
if ((i = read(0, buf, sizeof(buf) - 1)) < 24) return 0;
12+
buf[i] = 0;
13+
if (buf[0] != 'A') return 0;
14+
if (buf[1] != 'B') return 0;
15+
if (buf[2] != 'C') return 0;
16+
if (buf[3] != 'D') return 0;
17+
if (memcmp(buf + 4, "1234", 4) || memcmp(buf + 8, "EFGH", 4)) return 0;
18+
if (strncmp(buf + 12, "AAAA", 4) == 0){
19+
printf("HIT!\n");
20+
} else {
21+
printf("NOT HIT!\n");
22+
}
23+
24+
return 0;
25+
26+
}

0 commit comments

Comments
 (0)