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

Skip to content

Commit 99b01e7

Browse files
committed
C++: Additional test case for FormattingFunction.
1 parent 931322e commit 99b01e7

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
| printf1.h:231:25:231:25 | i | This argument should be of type 'char *' but is of type 'int' |
5454
| printf1.h:234:25:234:25 | i | This argument should be of type 'char *' but is of type 'int' |
5555
| printf1.h:235:22:235:22 | s | This argument should be of type 'int' but is of type 'char *' |
56+
| printf1.h:276:32:276:32 | s | This argument should be of type 'int' but is of type 'char *' |
57+
| printf1.h:278:17:278:17 | s | This argument should be of type 'int' but is of type 'char *' |
5658
| real_world.h:61:21:61:22 | & ... | This argument should be of type 'int *' but is of type 'short *' |
5759
| real_world.h:62:22:62:23 | & ... | This argument should be of type 'short *' but is of type 'int *' |
5860
| real_world.h:63:22:63:24 | & ... | This argument should be of type 'short *' but is of type 'unsigned int *' |

cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_signed_chars/common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ typedef struct _IO_FILE FILE;
88
#define va_list void *
99
#define va_start(x, y)
1010
#define va_end(x)
11+
#define va_arg(ap, type) ((type)0)
1112

1213
extern int printf(const char *fmt, ...);
1314
extern int vprintf(const char *fmt, va_list ap);
1415
extern int vfprintf(FILE *stream, const char *format, va_list ap);
16+
extern int vsnprintf(char *s, size_t n, const char *format, va_list arg);
1517

1618
#include "printf1.h"
1719
#include "real_world.h"

cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_signed_chars/printf1.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,46 @@ void complexFormatSymbols(int i, const char *s)
234234
printf("%2$-*2$s", s, i); // BAD
235235
printf("%1$-*1$s", s, i); // BAD
236236
}
237+
238+
void myvsnprintf(const char *format_string, char *target, size_t buffer_size, va_list args)
239+
{
240+
// wraps vsnprintf with different parameter order
241+
vsnprintf(target, buffer_size, format_string, args);
242+
}
243+
244+
void mysprintf(const char *format_string, char *target, size_t buffer_size, ...)
245+
{
246+
// wraps myvsnprintf as an snprintf-like
247+
va_list args;
248+
249+
va_start(args, text);
250+
myvsnprintf(format_string, target, buffer_size, args);
251+
252+
// ...
253+
254+
va_end(args);
255+
}
256+
257+
void myprintf(const char *format_string, ...)
258+
{
259+
// wraps myvsnprintf as an printf-like (i.e. doesn't pass in a buffer from the caller)
260+
char buffer[1024];
261+
va_list args;
262+
263+
va_start(args, text);
264+
myvsnprintf(format_string, buffer, 1024, args);
265+
266+
// ...
267+
268+
va_end(args);
269+
}
270+
271+
void usemyprintf(int i, char *s)
272+
{
273+
char buffer[1024];
274+
275+
mysprintf("%i", buffer, 1024, i); // GOOD
276+
mysprintf("%i", buffer, 1024, s); // BAD
277+
myprintf("%i", i); // GOOD
278+
myprintf("%i", s); // BAD
279+
}

0 commit comments

Comments
 (0)