From patchwork Wed Dec 13 08:39:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 82030 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DF261385DC3B for ; Wed, 13 Dec 2023 08:39:27 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 73404385DC11 for ; Wed, 13 Dec 2023 08:39:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 73404385DC11 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 73404385DC11 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702456751; cv=none; b=b92wE++2FTL5k1Z+0bMkaT/ZT8So+xwZ5ZFDgCEGRyNvE0QNVt+RqsKSWd+t12tP3CavES1l0/TizrvRynyprbHeQ6KK3rnS3LThaPTyM1uOe3H3mXmjgQ2Dw8DpAWwYDwF1fnff44rf6DlFsbVscS+9In6trvrWV283m04XMPQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702456751; c=relaxed/simple; bh=ptoOK50ItQL3pWS9zgcGoL+1gIr1HiWaDhN3gESPl3s=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=ApiTjIG5ZM7eZHMq9tqo6T7bIkRjNhGK+H/OXEf/R3oift3ojE3jiAHbUdmran5sc+pFO8RswcnVpML0dNPN0GfnyW1TOz7TBKIwyNq2BvWq3F/eKzK8WMHaSCDH+yhGRWJ1Kqe/GRAPKgdmrVdvdgSKgnJ8R76sVWypR1usLyE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702456748; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=dvU+LI7EHmenGdA3UDz6NAlD8cX1sUZM9JttjFmmzeU=; b=QYo4WA1RceF0agxEfxowjx/2c+Tr+/qx4ZtKWfR+7mpcbDIyIwKzhLC8LOkmGaorc/kxt4 CX9hz3mlt0o6W3jI7mjbC18ZKI662bZq6C+Bfk9KBiO4Nqr3OnHk6tVulKqJS1EuiiHbJj WlIteJsoNAoYMc/oYLAePw/acCzws9k= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-624-K1y3yNH_O2-lhhKB9dbf7A-1; Wed, 13 Dec 2023 03:39:06 -0500 X-MC-Unique: K1y3yNH_O2-lhhKB9dbf7A-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D63D788F561; Wed, 13 Dec 2023 08:39:05 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 62D6A492BC6; Wed, 13 Dec 2023 08:39:05 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 3BD8d25W2489572 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 13 Dec 2023 09:39:03 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3BD8d1BV2489571; Wed, 13 Dec 2023 09:39:01 +0100 Date: Wed, 13 Dec 2023 09:39:01 +0100 From: Jakub Jelinek To: Jason Merrill , "Joseph S. Myers" , Marek Polacek Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] libcpp: Fix valgrind errors on pr88974.c [PR112956] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Hi! On the c-c++-common/cpp/pr88974.c testcase I'm seeing ==600549== Conditional jump or move depends on uninitialised value(s) ==600549== at 0x1DD3A05: cpp_get_token_1(cpp_reader*, unsigned int*) (macro.cc:3050) ==600549== by 0x1DBFC7F: _cpp_parse_expr (expr.cc:1392) ==600549== by 0x1DB9471: do_if(cpp_reader*) (directives.cc:2087) ==600549== by 0x1DBB4D8: _cpp_handle_directive (directives.cc:572) ==600549== by 0x1DCD488: _cpp_lex_token (lex.cc:3682) ==600549== by 0x1DD3A97: cpp_get_token_1(cpp_reader*, unsigned int*) (macro.cc:2936) ==600549== by 0x7F7EE4: scan_translation_unit (c-ppoutput.cc:350) ==600549== by 0x7F7EE4: preprocess_file(cpp_reader*) (c-ppoutput.cc:106) ==600549== by 0x7F6235: c_common_init() (c-opts.cc:1280) ==600549== by 0x704C8B: lang_dependent_init (toplev.cc:1837) ==600549== by 0x704C8B: do_compile (toplev.cc:2135) ==600549== by 0x704C8B: toplev::main(int, char**) (toplev.cc:2306) ==600549== by 0x7064BA: main (main.cc:39) error. The problem is that _cpp_lex_direct can leave result->src_loc uninitialized in some cases and later on we use that location_t. _cpp_lex_direct essentially does: cppchar_t c; ... cpp_token *result = pfile->cur_token++; fresh_line: result->flags = 0; ... if (buffer->need_line) { if (pfile->state.in_deferred_pragma) { result->type = CPP_PRAGMA_EOL; ... // keeps result->src_loc uninitialized; return result; } if (!_cpp_get_fresh_line (pfile)) { result->type = CPP_EOF; if (!pfile->state.in_directive && !pfile->state.parsing_args) { result->src_loc = pfile->line_table->highest_line; ... } ... // otherwise result->src_loc is sometimes uninitialized here return result; } ... } ... result->src_loc = pfile->line_table->highest_line; ... c = *buffer->cur++; switch (c) { ... case '\n': ... buffer->need_line = true; if (pfile->state.in_deferred_pragma) { result->type = CPP_PRAGMA_EOL; ... return result; } goto fresh_line; ... } ... So, if _cpp_lex_direct is called without buffer->need_line initially set, result->src_loc is always initialized (and actually hundreds of tests rely on that exact value it has), even when c == '\n' and we set that flag later on and goto fresh_line. For CPP_PRAGMA_EOL case we have in that case separate handling and don't goto. But if _cpp_lex_direct is called with buffer->need_line initially set and either decide to return a CPP_PRAGMA_EOL token or if getting a new line fails for some reason and we return an CPP_ERROR token and we are in directive or parsing args state, it is kept uninitialized and can be whatever the allocation left it there as. The following patch attempts to keep the status quo, use value that was returned previously if it was initialized (i.e. we went through the goto fresh_line; statement in c == '\n' handling) and only initialize result->src_loc if it was uninitialized before. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-12-13 Jakub Jelinek PR preprocessor/112956 * lex.cc (_cpp_lex_direct): Initialize c to 0. For CPP_PRAGMA_EOL tokens and if c == 0 also for CPP_EOF set result->src_loc to highest locus. Jakub --- libcpp/lex.cc.jj 2023-12-11 12:39:23.353442196 +0100 +++ libcpp/lex.cc 2023-12-12 13:15:07.154019695 +0100 @@ -3809,7 +3809,7 @@ _cpp_get_fresh_line (cpp_reader *pfile) cpp_token * _cpp_lex_direct (cpp_reader *pfile) { - cppchar_t c; + cppchar_t c = 0; cpp_buffer *buffer; const unsigned char *comment_start; bool fallthrough_comment = false; @@ -3833,6 +3833,7 @@ _cpp_lex_direct (cpp_reader *pfile) pfile->state.in_deferred_pragma = false; if (!pfile->state.pragma_allow_expansion) pfile->state.prevent_expansion--; + result->src_loc = pfile->line_table->highest_line; return result; } if (!_cpp_get_fresh_line (pfile)) @@ -3849,6 +3850,8 @@ _cpp_lex_direct (cpp_reader *pfile) /* Now pop the buffer that _cpp_get_fresh_line did not. */ _cpp_pop_buffer (pfile); } + else if (c == 0) + result->src_loc = pfile->line_table->highest_line; return result; } if (buffer != pfile->buffer)