[clang] [clang][StaticAnalyzer] Adding getentropy to CStringChecker. (PR #83675)

via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 22 06:13:09 PDT 2024


================
@@ -529,3 +529,37 @@ void nocrash_on_locint_offset(void *addr, void* from, struct S s) {
   size_t iAdd = (size_t) addr;
   memcpy(((void *) &(s.f)), from, iAdd);
 }
+
+//===----------------------------------------------------------------------===//
+// getentropy()
+//===----------------------------------------------------------------------===//
+
+int getentropy(void *d, size_t n);
+
+int getentropy0(void) {
+  char buf[16] = {0};
+
+  int r = getentropy(buf, sizeof(buf)); // no-warning
+  return r;
+}
+
+int getentropy1(void) {
+  char buf[257] = {0};
+
+  int r = getentropy(buf, 256); // no-warning
+  return r;
+}
+
+int getentropy2(void) {
+  char buf[1024] = {0};
+
+  int r = getentropy(buf, sizeof(buf)); // expected-warning{{must be smaller than or equal to 256}}
+  return r;
+}
+
+int getentropy3(void) {
+  char buf[256] = {0};
+
+  int r = getentropy(buf, 0); // no-wwarning
+  return r;
+}
----------------
NagyDonat wrote:

Add testcases like

```
int getentropy4(size_t arg) {
  char buf[257] = {0};

  int r = getentropy(buf, arg); // no-warning
  return r;
}

void do_something();
int getentropy5(size_t arg) {
  char buf[257] = {0};

  // split the state and introduce a separate execution path where arg > 256
  if (arg <= 256)
    do_something();

  int r = getentropy(buf, arg); // expected-warning{{must be smaller than or equal to 256}}
  return r;
}
```

and also create a few testcases where `getentropy` fails because the buffer is a nullpointer or the specified size is larger than the buffer size.

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


More information about the cfe-commits mailing list