Skip to content

False positive, cpp/ql/src/Security/CWE/CWE-120/BadlyBoundedWrite.ql #13913

@peternmartin

Description

@peternmartin

This badly bounded write query flags the following code incorrectly:

#include <stdio.h>
#include <stdlib.h>

char* dest = "a";

int main(int argc, char* argv[])
{
  const char src[] = "Testing testing 123";
  dest = malloc(sizeof(src));
  if (dest == 0)
    return EXIT_FAILURE;

  snprintf(dest, sizeof(src), "%s", src);
  fprintf(stdout, "%s\n", dest);
  return EXIT_SUCCESS;
}

The snprintf is flagged:

|        bw        |                                             col1                                              |
+------------------+-----------------------------------------------------------------------------------------------+
| call to snprintf | This 'call to snprintf' operation is limited to 20 bytes but the destination is only 0 bytes. |
| call to snprintf | This 'call to snprintf' operation is limited to 20 bytes but the destination is only 2 bytes. |

Admittedly, the code is doing something slightly strange (that is, taking a pointer that was pointing to a string constant, and repointing it at dynamically allocated memory). However, I don't think the finding is correct.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions