const svalue *dest_sval = cd.get_arg_svalue (0);
const region *dest_reg = deref_rvalue (dest_sval, cd.get_arg_tree (0),
cd.get_ctxt ());
+ const svalue *src_sval = cd.get_arg_svalue (1);
+ const region *src_reg = deref_rvalue (src_sval, cd.get_arg_tree (1),
+ cd.get_ctxt ());
+ const svalue *src_contents_sval = get_store_value (src_reg,
+ cd.get_ctxt ());
cd.maybe_set_lhs (dest_sval);
- check_region_for_write (dest_reg, cd.get_ctxt ());
+ /* Try to get the string size if SRC_REG is a string_region. */
+ const svalue *copied_bytes_sval = get_string_size (src_reg);
+ /* Otherwise, check if the contents of SRC_REG is a string. */
+ if (copied_bytes_sval->get_kind () == SK_UNKNOWN)
+ copied_bytes_sval = get_string_size (src_contents_sval);
- /* For now, just mark region's contents as unknown. */
- mark_region_as_unknown (dest_reg, cd.get_uncertainty ());
+ const region *sized_dest_reg
+ = m_mgr->get_sized_region (dest_reg, NULL_TREE, copied_bytes_sval);
+ set_value (sized_dest_reg, src_contents_sval, cd.get_ctxt ());
}
/* Handle the on_call_pre part of "strlen". */
return m_mgr->get_or_create_unknown_svalue (sizetype);
}
+/* Return the string size, including the 0-terminator, if SVAL is a
+ constant_svalue holding a string. Otherwise, return an unknown_svalue. */
+
+const svalue *
+region_model::get_string_size (const svalue *sval) const
+{
+ tree cst = sval->maybe_get_constant ();
+ if (!cst || TREE_CODE (cst) != STRING_CST)
+ return m_mgr->get_or_create_unknown_svalue (size_type_node);
+
+ tree out = build_int_cst (size_type_node, TREE_STRING_LENGTH (cst));
+ return m_mgr->get_or_create_constant_svalue (out);
+}
+
+/* Return the string size, including the 0-terminator, if REG is a
+ string_region. Otherwise, return an unknown_svalue. */
+
+const svalue *
+region_model::get_string_size (const region *reg) const
+{
+ const string_region *str_reg = dyn_cast <const string_region *> (reg);
+ if (!str_reg)
+ return m_mgr->get_or_create_unknown_svalue (size_type_node);
+
+ tree cst = str_reg->get_string_cst ();
+ tree out = build_int_cst (size_type_node, TREE_STRING_LENGTH (cst));
+ return m_mgr->get_or_create_constant_svalue (out);
+}
+
/* If CTXT is non-NULL, use it to warn about any problems accessing REG,
using DIR to determine if this access is a read or write. */
const svalue *get_capacity (const region *reg) const;
+ const svalue *get_string_size (const svalue *sval) const;
+ const svalue *get_string_size (const region *reg) const;
+
/* Implemented in sm-malloc.cc */
void on_realloc_with_move (const call_details &cd,
const svalue *old_ptr_sval,
--- /dev/null
+/* { dg-additional-options "-Wno-stringop-overflow -Wno-stringop-truncation" } */
+#include <string.h>
+
+/* Wanalyzer-out-of-bounds tests for strpy-related overflows.
+
+ The intra-procedural tests are all caught by Wstringop-overflow.
+ The inter-procedural out-of-bounds are only found by the analyzer. */
+
+void test1 (void)
+{
+ char dst[5];
+ strcpy (dst, "Hello"); /* { dg-line test1 } */
+
+ /* { dg-warning "overflow" "warning" { target *-*-* } test1 } */
+ /* { dg-message "dst" "note" { target *-*-* } test1 } */
+}
+
+void test2 (void)
+{
+ char dst[6];
+ strcpy (dst, "Hello");
+}
+
+void test3 (void)
+{
+ char *src = "Hello";
+ char dst[5];
+ strcpy (dst, src); /* { dg-line test3 } */
+
+ /* { dg-warning "overflow" "warning" { target *-*-* } test3 } */
+ /* { dg-message "dst" "note" { target *-*-* } test3 } */
+}
+
+void test4 (void)
+{
+ char *src = "Hello";
+ char dst[6];
+ strcpy (dst, src);
+}
+
+const char *return_hello (void)
+{
+ return "hello";
+}
+
+void test5 (void)
+{
+ const char *str = return_hello ();
+ if (!str)
+ return;
+ char dst[5];
+ strcpy (dst, str); /* { dg-line test5 } */
+
+ /* { dg-warning "overflow" "warning" { target *-*-* } test5 } */
+ /* { dg-message "dst" "note" { target *-*-* } test5 } */
+}
+
+void test6 (void)
+{
+ const char *str = return_hello ();
+ if (!str)
+ return;
+ char dst[6];
+ strcpy (dst, str);
+}
--- /dev/null
+#include <string.h>
+#include "analyzer-decls.h"
+
+void test_1 (void)
+{
+ char str[] = "Hello";
+ char buf[6];
+ char *result = strcpy (buf, str);
+ __analyzer_describe (1, result); /* { dg-warning "region_svalue.*?'buf'" } */
+ __analyzer_eval (result == buf); /* { dg-warning "TRUE" } */
+ __analyzer_eval (buf[0] == 'H'); /* { dg-warning "TRUE" } */
+ __analyzer_eval (buf[1] == 'e'); /* { dg-warning "TRUE" } */
+ __analyzer_eval (buf[2] == 'l'); /* { dg-warning "TRUE" } */
+ __analyzer_eval (buf[3] == 'l'); /* { dg-warning "TRUE" } */
+ __analyzer_eval (buf[4] == 'o'); /* { dg-warning "TRUE" } */
+ __analyzer_eval (buf[5] == 0); /* { dg-warning "TRUE" } */
+ __analyzer_eval (result[0] == 'H'); /* { dg-warning "TRUE" } */
+ __analyzer_eval (result[1] == 'e'); /* { dg-warning "TRUE" } */
+ __analyzer_eval (result[2] == 'l'); /* { dg-warning "TRUE" } */
+ __analyzer_eval (result[3] == 'l'); /* { dg-warning "TRUE" } */
+ __analyzer_eval (result[4] == 'o'); /* { dg-warning "TRUE" } */
+ __analyzer_eval (result[5] == 0); /* { dg-warning "TRUE" } */
+}