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

Skip to content

Commit 6025c03

Browse files
committed
[CPP-370] Add nested.cpp test case, for nested calls to ...printf functions.
1 parent f6903c7 commit 6025c03

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
| NonConstantFormat.c:34:9:34:36 | call to any_random_function | The format string argument to printf should be constant to prevent security issues and other potential errors. |
33
| NonConstantFormat.c:41:9:41:27 | call to any_random_function | The format string argument to printf should be constant to prevent security issues and other potential errors. |
44
| NonConstantFormat.c:45:9:45:48 | call to any_random_function | The format string argument to printf should be constant to prevent security issues and other potential errors. |
5+
| nested.cpp:21:23:21:26 | fmt0 | The format string argument to snprintf should be constant to prevent security issues and other potential errors. |
6+
| nested.cpp:80:32:80:38 | call to get_fmt | The format string argument to diagnostic should be constant to prevent security issues and other potential errors. |
57
| test.cpp:45:10:45:21 | call to make_message | The format string argument to printf should be constant to prevent security issues and other potential errors. |
68
| test.cpp:50:12:50:16 | hello | The format string argument to printf should be constant to prevent security issues and other potential errors. |
79
| test.cpp:53:12:53:21 | call to const_wash | The format string argument to printf should be constant to prevent security issues and other potential errors. |
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
typedef void *va_list;
2+
#define va_start(ap, parmN)
3+
#define va_end(ap)
4+
#define va_arg(ap, type) ((type)0)
5+
#define NULL 0
6+
7+
extern "C" int printf(const char *fmt, ...);
8+
extern "C" int snprint(char *buf, int len, const char *fmt, ...);
9+
extern "C" int _vsnprintf_s(
10+
char *buffer,
11+
int sizeOfBuffer,
12+
int count,
13+
const char *fmt,
14+
va_list argptr
15+
);
16+
extern "C" int snprintf ( char * s, int n, const char * format, ... );
17+
18+
struct A {
19+
void do_print(const char *fmt0) {
20+
char buf[32];
21+
snprintf(buf, 32, fmt0);
22+
}
23+
};
24+
25+
struct B {
26+
A a;
27+
void do_printing(const char *fmt) {
28+
a.do_print(fmt);
29+
}
30+
};
31+
32+
struct C {
33+
B b;
34+
void do_some_printing(const char *fmt) {
35+
b.do_printing(fmt);
36+
}
37+
const char *ext_fmt_str(void);
38+
};
39+
40+
void foo(void) {
41+
C c;
42+
c.do_some_printing(c.ext_fmt_str());
43+
}
44+
45+
struct some_class {
46+
// Retrieve some target specific output strings
47+
virtual const char * get_fmt() const = 0;
48+
};
49+
50+
struct debug_ {
51+
int
52+
out_str(
53+
const char *fmt,
54+
va_list args)
55+
{
56+
char str[4096];
57+
//int length = printf(fmt, args);
58+
int length = _vsnprintf_s(str, sizeof(str), 0, fmt, args);
59+
if (length > 0)
60+
{
61+
return 0;
62+
}
63+
return 1;
64+
}
65+
};
66+
67+
some_class* some_instance = NULL;
68+
debug_ *debug_ctrl;
69+
70+
void diagnostic(const char *fmt, ...)
71+
{
72+
va_list args;
73+
74+
va_start(args, fmt);
75+
debug_ctrl->out_str(fmt, args);
76+
va_end(args);
77+
}
78+
79+
void bar(void) {
80+
diagnostic (some_instance->get_fmt());
81+
}

0 commit comments

Comments
 (0)