From patchwork Wed Oct 12 18:24:59 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathan Sidwell X-Patchwork-Id: 681380 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3svMjb4sN5z9ryn for ; Thu, 13 Oct 2016 05:25:23 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=KtHMrNfD; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :subject:to:references:cc:from:message-id:date:mime-version :in-reply-to:content-type; q=dns; s=default; b=qJAyepeTSTCRpGNpI BSwR4NTsmVaPNROLfMotIqhmi4cr9lwREkpfdZnuBmyWhsfHz32wMdlDbLaGcjcW vyIWiYGhUqDu1+QeJOwk2i8Dnr0s3ZMG1F+EOZetzcXfiqJQgvDBr+pqIJgVlbUf VPBral3Jhsxk2kbfu1U+gXO/+A= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :subject:to:references:cc:from:message-id:date:mime-version :in-reply-to:content-type; s=default; bh=z93qKk5eprKcmZiUA9lL2RT zDdY=; b=KtHMrNfDedfkT5hBcHfDLdCzrXTjJIMGuHUnonPrtuWPbE1s3iFn58M t6jPc/rxxClUazb13bO4l0cjOezdcd+s0J2bV39+ubd2AAChH6Nqb5FtcxEdHP2M shBm6N6WwKoqzIs9/wttfH3fFHKUc96+Ck7hTaWlkC254Auh28TM= Received: (qmail 74717 invoked by alias); 12 Oct 2016 18:25:14 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 74699 invoked by uid 89); 12 Oct 2016 18:25:13 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=1.3 required=5.0 tests=BAYES_50, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=no version=3.3.2 spammy=acquire, survived, readwrite, read-write X-HELO: mail-qk0-f181.google.com Received: from mail-qk0-f181.google.com (HELO mail-qk0-f181.google.com) (209.85.220.181) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 12 Oct 2016 18:25:03 +0000 Received: by mail-qk0-f181.google.com with SMTP id o68so92817038qkf.3 for ; Wed, 12 Oct 2016 11:25:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:subject:to:references:cc:from:message-id :date:user-agent:mime-version:in-reply-to; bh=gAqRX5VcideMlEV3cwVYhTpOYsSOXR8jPA10zeCrNL4=; b=QilVMDcNvQBv8oQhjA1+kH4et/2pnMwLM2aXCL68lX/qlORV0mGHLz3WLkgXiWRBY4 MK5a6qlkxDgdNC1Ev/mCbocIA0mz9vV7EYA4bdG08OSZvPxbntePl1nRozQJ5aC85NyA /ZoA4/cgJ+676HJnx80R/oPvCPIrJ3whM8VZZ4UWSJ/2NJLvZN5liWaZSJxnk0c5epFt lI9NoWL/4tervLR4M3Cc+hIEI+CwP2/KPyyIeQc2H/oW82O3lqNeEy87rK6CSD4mGQKk qCzs/w+CqEh9ZLtxNU5+UY+frz1L6gdlwlSIC9x38VxGGSpNA+WvAkBVcerUT8hXU5jF fn9A== X-Gm-Message-State: AA6/9Rldv7Fa6GtcSWDAL4A2xpPYJmAsqx//BZ59aQqpzS9uKeX/ct+dU6uEIyhPSWF49w== X-Received: by 10.55.145.197 with SMTP id t188mr2949786qkd.59.1476296701501; Wed, 12 Oct 2016 11:25:01 -0700 (PDT) Received: from ?IPv6:2601:181:c003:1930:3fe6:c217:b86a:6e86? ([2601:181:c003:1930:3fe6:c217:b86a:6e86]) by smtp.googlemail.com with ESMTPSA id k48sm3238024qtc.12.2016.10.12.11.25.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Oct 2016 11:25:00 -0700 (PDT) Subject: Re: Questionable code in gcov-io.c To: Andreas Schwab References: <20161012121001.GA27295@redhat.com> <5d4f3cdb-4698-f323-fe38-e2a80421034f@acm.org> <20161012134323.GC27295@redhat.com> <639591ca-c3ee-2c96-e185-78e04b869776@acm.org> Cc: Marek Polacek , GCC Patches , Jakub Jelinek From: Nathan Sidwell Message-ID: <041f52bf-91e2-d98d-dcb5-d43635df7a55@acm.org> Date: Wed, 12 Oct 2016 14:24:59 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.3.0 MIME-Version: 1.0 In-Reply-To: On 10/12/16 11:04, Andreas Schwab wrote: > Do we still need to call fstat? I don't think it can ever fail here. Update removing the fstat. Survived a profiled bootstrap, so I'll commit tomorrow, unless there are further comments. Thanks for spotting this! nathan 2016-10-12 Nathan Sidwell * gcov-io.c (gcov_open): Fix documentation. Simplify setting gcov_var.mode. Remove unnecessary fstat. Index: gcov-io.c =================================================================== --- gcov-io.c (revision 241027) +++ gcov-io.c (working copy) @@ -115,10 +115,9 @@ static inline gcov_unsigned_t from_file opened. If MODE is >= 0 an existing file will be opened, if possible, and if MODE is <= 0, a new file will be created. Use MODE=0 to attempt to reopen an existing file and then fall back on - creating a new one. If MODE < 0, the file will be opened in + creating a new one. If MODE > 0, the file will be opened in read-only mode. Otherwise it will be opened for modification. - Return zero on failure, >0 on opening an existing file and <0 on - creating a new one. */ + Return zero on failure, non-zero on success. */ GCOV_LINKAGE int #if IN_LIBGCOV @@ -156,17 +155,12 @@ gcov_open (const char *name, int mode) /* pass mode (ignored) for compatibility */ fd = open (name, O_RDONLY, S_IRUSR | S_IWUSR); } - else if (mode < 0) + else { /* Write mode - acquire a write-lock. */ s_flock.l_type = F_WRLCK; - fd = open (name, O_RDWR | O_CREAT | O_TRUNC, 0666); - } - else /* mode == 0 */ - { - /* Read-Write mode - acquire a write-lock. */ - s_flock.l_type = F_WRLCK; - fd = open (name, O_RDWR | O_CREAT, 0666); + /* Truncate if force new mode. */ + fd = open (name, O_RDWR | O_CREAT | (mode < 0 ? O_TRUNC : 0), 0666); } if (fd < 0) return 0; @@ -181,42 +175,23 @@ gcov_open (const char *name, int mode) close (fd); return 0; } - - if (mode > 0) - gcov_var.mode = 1; - else if (mode == 0) - { - struct stat st; - - if (fstat (fd, &st) < 0) - { - fclose (gcov_var.file); - gcov_var.file = 0; - return 0; - } - if (st.st_size != 0) - gcov_var.mode = 1; - else - gcov_var.mode = mode * 2 + 1; - } - else - gcov_var.mode = mode * 2 + 1; #else if (mode >= 0) + /* Open an existing file. */ gcov_var.file = fopen (name, (mode > 0) ? "rb" : "r+b"); if (gcov_var.file) - gcov_var.mode = 1; + mode = 1; else if (mode <= 0) - { - gcov_var.file = fopen (name, "w+b"); - if (gcov_var.file) - gcov_var.mode = mode * 2 + 1; - } + /* Create a new file. */ + gcov_var.file = fopen (name, "w+b"); + if (!gcov_var.file) return 0; #endif + gcov_var.mode = mode ? mode : 1; + setbuf (gcov_var.file, (char *)0); return 1;