Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eef050d

Browse files
committed
CPP: Improve deduction of %S types in FormattingFunction.qll.
1 parent 4a25c37 commit eef050d

4 files changed

Lines changed: 23 additions & 17 deletions

File tree

cpp/ql/src/semmle/code/cpp/models/interfaces/FormattingFunction.qll

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ abstract class FormattingFunction extends Function {
9494
* which is correct for a particular function.
9595
*/
9696
Type getNonDefaultCharType() {
97-
(
98-
getDefaultCharType().getSize() = 1 and
99-
result = getAFormatterWideTypeOrDefault()
100-
) or (
101-
getDefaultCharType().getSize() > 1 and
102-
result instanceof PlainCharType
103-
)
97+
(
98+
getDefaultCharType().getSize() = 1 and
99+
result = getWideCharType()
100+
) or (
101+
not getDefaultCharType().getSize() = 1 and
102+
result instanceof PlainCharType
103+
)
104104
}
105105

106106
/**
@@ -110,10 +110,12 @@ abstract class FormattingFunction extends Function {
110110
*/
111111
Type getWideCharType() {
112112
(
113-
result = getDefaultCharType() or
114-
result = getNonDefaultCharType()
115-
) and
116-
result.getSize() > 1
113+
result = getFormatCharType() and
114+
result.getSize() > 1
115+
) or (
116+
not getFormatCharType().getSize() > 1 and
117+
result = getAFormatterWideTypeOrDefault() // may have more than one result
118+
)
117119
}
118120

119121
/**

cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_mixed_byte_wprintf/WrongTypeFormatArguments.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22
| tests.cpp:19:15:19:22 | Hello | This argument should be of type 'char *' but is of type 'wchar_t *' |
33
| tests.cpp:26:17:26:24 | Hello | This argument should be of type 'char *' but is of type 'char16_t *' |
44
| tests.cpp:27:17:27:24 | Hello | This argument should be of type 'char *' but is of type 'wchar_t *' |
5+
| tests.cpp:29:17:29:23 | Hello | This argument should be of type 'wchar_t *' but is of type 'char *' |
6+
| tests.cpp:30:17:30:24 | Hello | This argument should be of type 'wchar_t *' but is of type 'char16_t *' |
57
| tests.cpp:34:36:34:43 | Hello | This argument should be of type 'char *' but is of type 'char16_t *' |
68
| tests.cpp:35:36:35:43 | Hello | This argument should be of type 'char *' but is of type 'wchar_t *' |
9+
| tests.cpp:37:36:37:42 | Hello | This argument should be of type 'char16_t *' but is of type 'char *' |
10+
| tests.cpp:39:36:39:43 | Hello | This argument should be of type 'char16_t *' but is of type 'wchar_t *' |
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
| tests.cpp:8:5:8:10 | printf | char | char | char16_t, wchar_t | char16_t, wchar_t |
2-
| tests.cpp:9:5:9:11 | wprintf | wchar_t | char | char16_t, wchar_t | char16_t, wchar_t |
3-
| tests.cpp:10:5:10:12 | swprintf | char16_t | char | char16_t, wchar_t | char16_t, wchar_t |
2+
| tests.cpp:9:5:9:11 | wprintf | wchar_t | char | wchar_t | wchar_t |
3+
| tests.cpp:10:5:10:12 | swprintf | char16_t | char | char16_t | char16_t |

cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_mixed_byte_wprintf/tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ void tests() {
2626
wprintf(L"%s", u"Hello"); // BAD: expecting char
2727
wprintf(L"%s", L"Hello"); // BAD: expecting char
2828

29-
wprintf(L"%S", "Hello"); // BAD: expecting wchar_t [NOT DETECTED]
30-
wprintf(L"%S", u"Hello"); // BAD: expecting wchar_t [NOT DETECTED]
29+
wprintf(L"%S", "Hello"); // BAD: expecting wchar_t
30+
wprintf(L"%S", u"Hello"); // BAD: expecting wchar_t
3131
wprintf(L"%S", L"Hello"); // GOOD
3232

3333
swprintf(buffer, BUF_SIZE, u"%s", "Hello"); // GOOD
3434
swprintf(buffer, BUF_SIZE, u"%s", u"Hello"); // BAD: expecting char
3535
swprintf(buffer, BUF_SIZE, u"%s", L"Hello"); // BAD: expecting char
3636

37-
swprintf(buffer, BUF_SIZE, u"%S", "Hello"); // BAD: expecting char16_t [NOT DETECTED]
37+
swprintf(buffer, BUF_SIZE, u"%S", "Hello"); // BAD: expecting char16_t
3838
swprintf(buffer, BUF_SIZE, u"%S", u"Hello"); // GOOD
39-
swprintf(buffer, BUF_SIZE, u"%S", L"Hello"); // BAD: expecting char16_t [NOT DETECTED]
39+
swprintf(buffer, BUF_SIZE, u"%S", L"Hello"); // BAD: expecting char16_t
4040
}

0 commit comments

Comments
 (0)