[clang] [clang][analyzer] Model more getline/getdelim pre and postconditions (PR #83027)

Balázs Kéri via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 21 08:07:08 PDT 2024


================
@@ -376,3 +377,122 @@ void fflush_on_open_failed_stream(void) {
   }
   fclose(F);
 }
+
+void getline_null_file() {
+  char *buffer = NULL;
+  size_t n = 0;
+  getline(&buffer, &n, NULL); // expected-warning {{Stream pointer might be NULL}}
+}
+
+void getdelim_null_file() {
+  char *buffer = NULL;
+  size_t n = 0;
+  getdelim(&buffer, &n, '\n', NULL); // expected-warning {{Stream pointer might be NULL}}
+}
+
+void getline_no_return_check() {
+  FILE *file = fopen("file.txt", "r");
+  if (file == NULL) {
+    return;
+  }
+
+  char *line = NULL;
+  size_t len = 0;
+  getline(&line, &len, file);
+
+  if (line[0] == '\0') {} // expected-warning {{The left operand of '==' is a garbage value}}
----------------
balazske wrote:

I like better if this check is included only in the failure case:
```
if (getline(&line, &len, file) == -1) {
  if (line[0] == '\0') {} // expected-warning {{The left operand of '==' is a garbage value}}
} else {
  if (line[0] == '\0') {} // no warning
}

```

https://github.com/llvm/llvm-project/pull/83027


More information about the cfe-commits mailing list