From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id B10773858296 for ; Thu, 24 Aug 2023 14:39:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B10773858296 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692887950; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xqGuJARd4NLsbiTKIN/l8fhdX7YVMkkxVm4SOKch7mo=; b=ecezPkDDFjYs7DwYXVKla8KzHhYCsZlfjwf+EwA4plIFIsdBGNbUJ5UilZWYa1Sh2bD4tb CrGCtiVHfW8+EMCIM9agFwQIR56I7h8kIZ5dOYm2uteQuYRf3DI3uDBPfD7hxP3PqS7JIh G/uwHE5JnRNhCF0wrFEHOHGEPSOEkwM= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-447-dFAwcLjNNMe9w2OV2lj_bg-1; Thu, 24 Aug 2023 10:39:07 -0400 X-MC-Unique: dFAwcLjNNMe9w2OV2lj_bg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5478810AFD03 for ; Thu, 24 Aug 2023 14:39:07 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.32.117]) by smtp.corp.redhat.com (Postfix) with ESMTP id 045EE40D2839; Thu, 24 Aug 2023 14:39:06 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH 6/9] analyzer: handle strlen(INIT_VAL(STRING_REG)) [PR105899] Date: Thu, 24 Aug 2023 10:39:00 -0400 Message-Id: <20230824143903.3161185-7-dmalcolm@redhat.com> In-Reply-To: <20230824143903.3161185-1-dmalcolm@redhat.com> References: <20230824143903.3161185-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: gcc/analyzer/ChangeLog: PR analyzer/105899 * region-model.cc (fragment::has_null_terminator): Move STRING_CST handling to fragment::string_cst_has_null_terminator; also use it to handle INIT_VAL(STRING_REG). (fragment::string_cst_has_null_terminator): New, from above. gcc/testsuite/ChangeLog: PR analyzer/105899 * gcc.dg/analyzer/strcpy-3.c (test_2): New. --- gcc/analyzer/region-model.cc | 68 ++++++++++++++++-------- gcc/testsuite/gcc.dg/analyzer/strcpy-3.c | 7 +++ 2 files changed, 54 insertions(+), 21 deletions(-) diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc index 00c306ab7dae..6574ec140074 100644 --- a/gcc/analyzer/region-model.cc +++ b/gcc/analyzer/region-model.cc @@ -3310,27 +3310,10 @@ struct fragment switch (TREE_CODE (cst)) { case STRING_CST: - { - /* Look for the first 0 byte within STRING_CST - from START_READ_OFFSET onwards. */ - const HOST_WIDE_INT num_bytes_to_search - = std::min ((TREE_STRING_LENGTH (cst) - - rel_start_read_offset_hwi), - available_bytes_hwi); - const char *start = (TREE_STRING_POINTER (cst) - + rel_start_read_offset_hwi); - if (num_bytes_to_search >= 0) - if (const void *p = memchr (start, 0, - num_bytes_to_search)) - { - *out_bytes_read = (const char *)p - start + 1; - return tristate (true); - } - - *out_bytes_read = available_bytes; - return tristate (false); - } - break; + return string_cst_has_null_terminator (cst, + rel_start_read_offset_hwi, + available_bytes_hwi, + out_bytes_read); case INTEGER_CST: if (rel_start_read_offset_hwi == 0 && integer_onep (TYPE_SIZE_UNIT (TREE_TYPE (cst)))) @@ -3357,12 +3340,55 @@ struct fragment } } break; + + case SK_INITIAL: + { + const initial_svalue *initial_sval = (const initial_svalue *)m_sval; + const region *reg = initial_sval->get_region (); + if (const string_region *string_reg = reg->dyn_cast_string_region ()) + { + tree string_cst = string_reg->get_string_cst (); + return string_cst_has_null_terminator (string_cst, + rel_start_read_offset_hwi, + available_bytes_hwi, + out_bytes_read); + } + return tristate::TS_UNKNOWN; + } + break; + default: // TODO: it may be possible to handle other cases here. return tristate::TS_UNKNOWN; } } + static tristate + string_cst_has_null_terminator (tree string_cst, + HOST_WIDE_INT rel_start_read_offset_hwi, + HOST_WIDE_INT available_bytes_hwi, + byte_offset_t *out_bytes_read) + { + /* Look for the first 0 byte within STRING_CST + from START_READ_OFFSET onwards. */ + const HOST_WIDE_INT num_bytes_to_search + = std::min ((TREE_STRING_LENGTH (string_cst) + - rel_start_read_offset_hwi), + available_bytes_hwi); + const char *start = (TREE_STRING_POINTER (string_cst) + + rel_start_read_offset_hwi); + if (num_bytes_to_search >= 0) + if (const void *p = memchr (start, 0, + num_bytes_to_search)) + { + *out_bytes_read = (const char *)p - start + 1; + return tristate (true); + } + + *out_bytes_read = available_bytes_hwi; + return tristate (false); + } + byte_range m_byte_range; const svalue *m_sval; }; diff --git a/gcc/testsuite/gcc.dg/analyzer/strcpy-3.c b/gcc/testsuite/gcc.dg/analyzer/strcpy-3.c index abb49bc39f27..a7b324fc445e 100644 --- a/gcc/testsuite/gcc.dg/analyzer/strcpy-3.c +++ b/gcc/testsuite/gcc.dg/analyzer/strcpy-3.c @@ -22,3 +22,10 @@ void test_1 (void) __analyzer_eval (result[5] == 0); /* { dg-warning "TRUE" } */ __analyzer_eval (strlen (result) == 5); /* { dg-warning "TRUE" } */ } + +void test_2 (void) +{ + char buf[16]; + __builtin_strcpy (buf, "abc"); + __analyzer_eval (strlen (buf) == 3); /* { dg-warning "TRUE" } */ +} -- 2.26.3