From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3895 invoked by alias); 27 May 2010 17:54:16 -0000 Received: (qmail 3882 invoked by uid 22791); 27 May 2010 17:54:15 -0000 X-SWARE-Spam-Status: No, hits=-5.7 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_HI,SPF_HELO_PASS,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 27 May 2010 17:54:10 +0000 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o4RHs83p019056 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 27 May 2010 13:54:08 -0400 Received: from host0.dyn.jankratochvil.net (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o4RHs5dr007190 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Thu, 27 May 2010 13:54:07 -0400 Received: from host0.dyn.jankratochvil.net (localhost [127.0.0.1]) by host0.dyn.jankratochvil.net (8.14.4/8.14.4) with ESMTP id o4RHs537016972 for ; Thu, 27 May 2010 19:54:05 +0200 Received: (from jkratoch@localhost) by host0.dyn.jankratochvil.net (8.14.4/8.14.4/Submit) id o4RHs4pn016971 for gdb-patches@sourceware.org; Thu, 27 May 2010 19:54:04 +0200 Date: Thu, 27 May 2010 18:00:00 -0000 From: Jan Kratochvil To: gdb-patches@sourceware.org Subject: [patch] Fix crash on /proc/PID/stat race Message-ID: <20100527175404.GA16087@host0.dyn.jankratochvil.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-12-10) X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2010-05/txt/msg00642.txt.bz2 Hi, got a reported a core file that gdb crashes in linux_nat_core_of_thread_1 called from linux_nat_wait_1 on TARGET_WAITKIND_EXITED. It crashes because CONTENT is empty there. While it is understanable /proc/PID/stat is not available after TARGET_WAITKIND_EXITED I failed to artificially reproduce it by sleep 1&p=$!;(sleep 2;cat) * linux-nat.c (linux_nat_core_of_thread_1): Fix crash on invalid CONTENT. --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -5502,16 +5502,23 @@ linux_nat_core_of_thread_1 (ptid_t ptid) make_cleanup (xfree, content); + /* Do not assume anything about CONTENT. In some race fopen can be still + successful but CONTENT_READ can be 0 for an exited process. */ + p = strchr (content, '('); - p = strchr (p, ')') + 2; /* skip ")" and a whitespace. */ + if (p != NULL) + p = strchr (p, ')'); + if (p != NULL) + p++; /* If the first field after program name has index 0, then core number is the field with index 36. There's no constant for that anywhere. */ - p = strtok_r (p, " ", &ts); - for (i = 0; i != 36; ++i) + if (p != NULL) + p = strtok_r (p, " ", &ts); + for (i = 0; p != NULL && i != 36; ++i) p = strtok_r (NULL, " ", &ts); - if (sscanf (p, "%d", &core) == 0) + if (p == NULL || sscanf (p, "%d", &core) == 0) core = -1; do_cleanups (back_to);