From patchwork Fri Nov 8 00:29:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 2008202 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Acm3asvl; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Xl0Fj4wz4z1xyM for ; Fri, 8 Nov 2024 11:30:01 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E78223858CDB for ; Fri, 8 Nov 2024 00:29:58 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 57A753858D20 for ; Fri, 8 Nov 2024 00:29:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 57A753858D20 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 57A753858D20 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=1731025781; cv=none; b=xPOrgj7T/qWrB20sASK1blUwPCzkMObqqtAu4+p+Uk/UNrgdtpx/8ArlXCrTUS25EBo3gGfRDGlVVWIExfFQPYyVlpg0XCNkPnhqIrc7t4MTy66NUv+pKiBuMVHQEKUPXEdM8GHhtnEBwee5pDSvJ6AiEdKC9zGZUauKuItpEoU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1731025781; c=relaxed/simple; bh=euSwloCjB3fDUoXYhHlffM13kKCv1PE6gPLz586g3bM=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=SCPufl3QRsdxQRfwerRF2VJEqeghWyTmI5+fqsz4fh2TZQubjFHUu1mCmBRgMUly/OzBDM2AB/iu97myK+zY7Q63TYA413uLYPtdJbCkYIaFic/vFSXyTgSG3P+MmUcmqmZKmnaHOyTgCc5g867M9fRHi3NfOOM1qdPEktDnckE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1731025779; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=vDH+V4YRkGmC0i/hBygfhIMVl7L5MeiJR0DfuP0GQ5I=; b=Acm3asvl+yL3mKtyQelEV1PU3yl5NKWEpxOXBwWpJaSLleq3nwIeRVkaBpKTw6+Y7rddnT kZ1K7EnfRiNPD4y05bzVEZVDOO8Lm5d+rMijwx6DQK5UpV6wuzqNfDrBFFVgFm2m8RjQvi /qDXMno5KVWzV8dB1DOMPkkE19KNSrU= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-152-Y7SNnXJHOVKhOVYeIasQkQ-1; Thu, 07 Nov 2024 19:29:37 -0500 X-MC-Unique: Y7SNnXJHOVKhOVYeIasQkQ-1 X-Mimecast-MFC-AGG-ID: Y7SNnXJHOVKhOVYeIasQkQ Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-4315c1b5befso9852525e9.1 for ; Thu, 07 Nov 2024 16:29:37 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731025774; x=1731630574; h=mime-version:message-id:subject:to:from:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=vDH+V4YRkGmC0i/hBygfhIMVl7L5MeiJR0DfuP0GQ5I=; b=u4jiryF3eIs3A1bWmNyRdbmfbdxMUlAbzF/ak1R9znN6pNrgg9zRuI2+qnBeSbx4Ls G7Z8UmHYcDTCWgK3gw9i3KUwhs8Q6i3yeK5+w0o5J5F5Hu9hM59sc76qxwmNa4ko0jSR EhGWCp43Q5tCAUtyKypKnw16G3uVxljb/7rmiEn3QdbJPjiU9hxNWcOTc0WyTZup+Nzz GcGFooJ+dEKdt/7muhChT12CNWCanc6Zuc/Xvqobcm9yfBGKw1vv+WgbcqPvUjKeZw7l DHE16zKOnrRRN3nzyZz9vlZXBWNsVehI0nS+qpKp1L/Wds1zC2nYqFONgON8wR31hapr gcAg== X-Gm-Message-State: AOJu0YxdHIsS5WIObWwxzEzY79gspbncj474+zrnIcrz0aiLfgaMrD8J MJEyH8jjK4oPK5YN1HxcrTPryXkptRp0/sAvv3ypVuP19c7nZiMvdEiBK2kT9itKBMmu7XHF3UO EMhreDHb84RtoOzi+W7QZ3RZUAVLAlNTYmqPPDmfe/6Ba7a5ZlHU23O2xYOIHjRBcIxWDydYvtP v/ZDnTHsRY713SrfFUd2r7MWZ4XwsPwg4ToVOtwgEByg== X-Received: by 2002:a05:600c:5127:b0:431:5ba2:2450 with SMTP id 5b1f17b1804b1-432b751e264mr5034905e9.33.1731025774578; Thu, 07 Nov 2024 16:29:34 -0800 (PST) X-Google-Smtp-Source: AGHT+IFwLubXn2ATdLvt6qbva75abNVs/KVm9FU936t9mcEQ1eoCcpaERkwbC5McfAx0+inxhyUY8g== X-Received: by 2002:a05:600c:5127:b0:431:5ba2:2450 with SMTP id 5b1f17b1804b1-432b751e264mr5034705e9.33.1731025774033; Thu, 07 Nov 2024 16:29:34 -0800 (PST) Received: from digraph.polyomino.org.uk (digraph.polyomino.org.uk. [2001:8b0:bf73:93f7::51bb:e332]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-432b0530671sm43524235e9.2.2024.11.07.16.29.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Nov 2024 16:29:33 -0800 (PST) Received: from jsm28 (helo=localhost) by digraph.polyomino.org.uk with local-esmtp (Exim 4.97) (envelope-from ) id 1t9Cs2-0000000BOmh-26F4 for libc-alpha@sourceware.org; Fri, 08 Nov 2024 00:29:02 +0000 Date: Fri, 8 Nov 2024 00:29:02 +0000 (UTC) From: Joseph Myers To: libc-alpha@sourceware.org Subject: Avoid uninitialized result in sem_open when file does not exist Message-ID: <6f9470e3-389d-4cc8-f98f-de9f78ea6e6e@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 2jhvSNeLsu7jdLOG7tusXyovcW5_vl-FVRopYzH6vew_1731025776 X-Mimecast-Originator: redhat.com X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org A static analyzer apparently reported an uninitialized use of the variable result in sem_open in the case where the file is required to exist but does not exist. The report appears to be correct; set result to SEM_FAILED in that case, and add a test for it. Note: the test passes for me even without the sem_open fix, I guess because result happens to get value SEM_FAILED (i.e. 0) when uninitialized. Tested for x86_64. Reviewed-by: H.J. Lu diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile index 4c1dc04b20..7f52e7d511 100644 --- a/sysdeps/pthread/Makefile +++ b/sysdeps/pthread/Makefile @@ -256,6 +256,7 @@ tests += \ tst-sem14 \ tst-sem15 \ tst-sem16 \ + tst-sem17 \ tst-setuid3 \ tst-signal1 \ tst-signal2 \ diff --git a/sysdeps/pthread/sem_open.c b/sysdeps/pthread/sem_open.c index e41236157a..dab734191a 100644 --- a/sysdeps/pthread/sem_open.c +++ b/sysdeps/pthread/sem_open.c @@ -76,6 +76,7 @@ __sem_open (const char *name, int oflag, ...) goto try_create; /* Return. errno is already set. */ + result = SEM_FAILED; } else /* Check whether we already have this semaphore mapped and diff --git a/sysdeps/pthread/tst-sem17.c b/sysdeps/pthread/tst-sem17.c new file mode 100644 index 0000000000..c3f05d196f --- /dev/null +++ b/sysdeps/pthread/tst-sem17.c @@ -0,0 +1,35 @@ +/* Test sem_open with missing file. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +#include + +int +do_test (void) +{ + sem_unlink ("/glibc-tst-sem17"); + errno = 0; + sem_t *s = sem_open ("/glibc-tst-sem17", 0); + TEST_VERIFY (s == SEM_FAILED); + TEST_COMPARE (errno, ENOENT); + return 0; +} + +#include