From a9c4eca8b5732d6246735f0776d4044358c3c3f6 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Tue, 27 Jun 2023 17:21:28 -0400 Subject: [PATCH 83/98] FIXME: add gcc/testsuite/c-c++-common/analyzer/strncmp-1.c --- .../c-c++-common/analyzer/strncmp-1.c | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 gcc/testsuite/c-c++-common/analyzer/strncmp-1.c diff --git a/gcc/testsuite/c-c++-common/analyzer/strncmp-1.c b/gcc/testsuite/c-c++-common/analyzer/strncmp-1.c new file mode 100644 index 000000000000..d3e700e9dede --- /dev/null +++ b/gcc/testsuite/c-c++-common/analyzer/strncmp-1.c @@ -0,0 +1,56 @@ +/* See e.g. https://en.cppreference.com/w/c/string/byte/strncmp + and https://man7.org/linux/man-pages/man3/strncmp.3p.html */ + +#include "analyzer-decls.h" + +extern int strncmp (const char *lhs, const char *rhs, size_t count); + +int +test_passthrough (const char *lhs, const char *rhs, size_t count) +{ + return strncmp (lhs, rhs, count); +} + +int +test_null_lhs (const char *rhs, size_t count) +{ + return strncmp (NULL, rhs, count); // FIXME: complain about this? +} + +int +test_null_rhs (const char *lhs, size_t count) +{ + return strncmp (lhs, NULL, count); // FIXME: complain about this? +} + +void +test_zero_count (const char *lhs, const char *rhs) +{ + __analyzer_eval (strncmp (lhs, rhs, 0) == 0); /* { dg-warning "TRUE" } */ +} + +void +test_same_ptr (const char *lhs, size_t count) +{ + __analyzer_eval (strncmp (lhs, lhs, count) == 0); /* { dg-warning "TRUE" } */ +} + +/* TODO: + - ptrs not at start of string + + "Compares at most count characters of two possibly null-terminated arrays. The comparison is done lexicographically. Characters following the null character are not compared." + + "The sign of the result is the sign of the difference between the values of the first pair of characters (both interpreted as unsigned char) that differ in the arrays being compared." + + "The behavior is undefined when access occurs past the end of either array lhs or rhs." + +Parameters +lhs, rhs - pointers to the possibly null-terminated arrays to compare +count - maximum number of characters to compare + +Return value + +"Negative value if lhs appears before rhs in lexicographical order." +"Zero if lhs and rhs compare equal, or if count is zero." +"Positive value if lhs appears after rhs in lexicographical order." +*/ -- 2.49.0