Skip to content

Commit 9c7e4f5

Browse files
authored
Merge pull request #5848 from tautschnig/fix-strcat
C library/str(n)cat: do not write spurious 0 byte
2 parents fb38813 + 62c9fa9 commit 9c7e4f5

File tree

5 files changed

+27
-29
lines changed

5 files changed

+27
-29
lines changed

regression/cbmc-library/strcat-01/main.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,5 @@ int main()
1010
assert(A1[3] == 'd');
1111
assert(strlen(A1) == 4);
1212

13-
char A2[5] = {'a', 'b', '\0'};
14-
char B2[3] = {'c', 'd', '\0'};
15-
16-
strncat(A2, B2, 2);
17-
assert(A2[3] == 'd');
18-
assert(strlen(A2) == 4);
19-
20-
char A3[5] = {'a', 'b', '\0'};
21-
char B3[3] = {'c', 'd', '\0'};
22-
23-
strncat(A3, B3, 1);
24-
assert(A3[3] == '\0');
25-
assert(strlen(A3) == 4); // expected to fail
26-
2713
return 0;
2814
}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
CORE
22
main.c
3-
--unwind 10
4-
^EXIT=10$
3+
--unwind 10 --pointer-check --bounds-check
4+
^VERIFICATION SUCCESSFUL$
5+
^EXIT=0$
56
^SIGNAL=0$
6-
^VERIFICATION FAILED$
7-
\[main.assertion.6\] .* assertion strlen\(A3\) == 4: FAILURE
8-
\*\* 1 of 9 failed
97
--
108
^warning: ignoring

regression/cbmc-library/strncat-01/main.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,19 @@
33

44
int main()
55
{
6-
strncat();
7-
assert(0);
6+
char A2[5] = {'a', 'b', '\0'};
7+
char B2[3] = {'c', 'd', '\0'};
8+
9+
strncat(A2, B2, 2);
10+
assert(A2[3] == 'd');
11+
assert(strlen(A2) == 4);
12+
13+
char A3[5] = {'a', 'b', '\0'};
14+
char B3[3] = {'c', 'd', '\0'};
15+
16+
strncat(A3, B3, 1);
17+
assert(A3[3] == '\0');
18+
assert(strlen(A3) == 4); // expected to fail
19+
820
return 0;
921
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
KNOWNBUG
1+
CORE
22
main.c
3-
--pointer-check --bounds-check
4-
^EXIT=0$
3+
--unwind 10 --pointer-check --bounds-check
4+
^EXIT=10$
55
^SIGNAL=0$
6-
^VERIFICATION SUCCESSFUL$
6+
^VERIFICATION FAILED$
7+
\[main.assertion.4\] .* assertion strlen\(A3\) == 4: FAILURE
8+
\*\* 1 of \d+ failed
79
--
810
^warning: ignoring

src/ansi-c/library/string.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ __CPROVER_HIDE:;
7272
ch = src[j];
7373
dst[i] = ch;
7474
}
75-
dst[i] = '\0';
7675
#endif
7776
return dst;
7877
}
@@ -122,7 +121,8 @@ __CPROVER_HIDE:;
122121
ch = src[j];
123122
dst[i] = ch;
124123
}
125-
dst[i] = '\0';
124+
if(ch != (char)0)
125+
dst[i] = '\0';
126126
#endif
127127
return dst;
128128
}
@@ -288,7 +288,6 @@ __CPROVER_HIDE:;
288288
ch = src[j];
289289
dst[i] = ch;
290290
}
291-
dst[i] = '\0';
292291
#endif
293292
return dst;
294293
}
@@ -342,7 +341,8 @@ __CPROVER_HIDE:;
342341
ch = src[j];
343342
dst[i] = ch;
344343
}
345-
dst[i] = '\0';
344+
if(ch != (char)0)
345+
dst[i] = '\0';
346346
#endif
347347
return dst;
348348
}

0 commit comments

Comments
 (0)