brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.8 KiB · 0546e0c Raw
104 lines · c
1// RUN: %check_clang_tidy %s bugprone-unchecked-string-to-number-conversion %t2 3typedef __SIZE_TYPE__      size_t;4typedef signed             ptrdiff_t;5typedef long long          intmax_t;6typedef unsigned long long uintmax_t;7typedef void *             FILE;8 9extern FILE *stdin;10 11extern int fscanf(FILE *stream, const char *format, ...);12extern int scanf(const char *format, ...);13extern int sscanf(const char *s, const char *format, ...);14 15extern double atof(const char *nptr);16extern int atoi(const char *nptr);17extern long int atol(const char *nptr);18extern long long int atoll(const char *nptr);19 20void f1(const char *in) {21  int i;22  long long ll;23  unsigned int ui;24  unsigned long long ull;25  intmax_t im;26  uintmax_t uim;27  float f;28  double d;29  long double ld;30 31  // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'sscanf' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtol' instead [bugprone-unchecked-string-to-number-conversion]32  sscanf(in, "%d", &i);33  // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'fscanf' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtoll' instead34  fscanf(stdin, "%lld", &ll);35  // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'sscanf' used to convert a string to an unsigned integer value, but function will not report conversion errors; consider using 'strtoul' instead36  sscanf(in, "%u", &ui);37  // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'fscanf' used to convert a string to an unsigned integer value, but function will not report conversion errors; consider using 'strtoull' instead38  fscanf(stdin, "%llu", &ull);39  // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'scanf' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtoimax' instead40  scanf("%jd", &im);41  // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'fscanf' used to convert a string to an unsigned integer value, but function will not report conversion errors; consider using 'strtoumax' instead42  fscanf(stdin, "%ju", &uim);43  // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'sscanf' used to convert a string to a floating-point value, but function will not report conversion errors; consider using 'strtof' instead44  sscanf(in, "%f", &f); // to float45  // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'fscanf' used to convert a string to a floating-point value, but function will not report conversion errors; consider using 'strtod' instead46  fscanf(stdin, "%lg", &d);47  // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'sscanf' used to convert a string to a floating-point value, but function will not report conversion errors; consider using 'strtold' instead48  sscanf(in, "%Le", &ld);49 50  // These are conversions with other modifiers51  short s;52  char c;53  size_t st;54  ptrdiff_t pt;55 56  // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'scanf' used to convert57  scanf("%hhd", &c);58  // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'scanf' used to convert59  scanf("%hd", &s);60  // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'scanf' used to convert61  scanf("%zu", &st);62  // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'scanf' used to convert63  scanf("%td", &pt);64  // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'scanf' used to convert65  scanf("%o", ui);66  // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'scanf' used to convert67  scanf("%X", ui);68  // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'scanf' used to convert69  scanf("%x", ui);70}71 72void f2(const char *in) {73  // CHECK-MESSAGES: :[[@LINE+1]]:11: warning: 'atoi' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtol' instead74  int i = atoi(in); // to int75  // CHECK-MESSAGES: :[[@LINE+1]]:12: warning: 'atol' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtol' instead76  long l = atol(in); // to long77  // CHECK-MESSAGES: :[[@LINE+1]]:18: warning: 'atoll' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtoll' instead78  long long ll = atoll(in); // to long long79  // CHECK-MESSAGES: :[[@LINE+1]]:14: warning: 'atof' used to convert a string to a floating-point value, but function will not report conversion errors; consider using 'strtod' instead80  double d = atof(in); // to double81}82 83void f3(void) {84  int i;85  unsigned int u;86  float f;87  char str[32];88 89  // Test that we don't report multiple infractions for a single call.90  // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'scanf' used to convert91  scanf("%d%u%f", &i, &u, &f);92 93  // Test that we still catch infractions that are not the first specifier.94  // CHECK-MESSAGES: :[[@LINE+1]]:3: warning: 'scanf' used to convert95  scanf("%s%d", str, &i);96}97 98void do_not_diagnose(void) {99  char str[32];100 101  scanf("%s", str); // Not a numerical conversion102  scanf("%*d"); // Assignment suppressed103}104