NetBSD Problem Report #43860

From www@NetBSD.org  Thu Sep  9 20:38:15 2010
Return-Path: <www@NetBSD.org>
Received: from mail.netbsd.org (mail.netbsd.org [204.152.190.11])
	by www.NetBSD.org (Postfix) with ESMTP id B46EE63BC83
	for <gnats-bugs@gnats.NetBSD.org>; Thu,  9 Sep 2010 20:38:15 +0000 (UTC)
Message-Id: <20100909203815.7FEA663BC7C@www.NetBSD.org>
Date: Thu,  9 Sep 2010 20:38:15 +0000 (UTC)
From: netbsd@happyjack.org
Reply-To: netbsd@happyjack.org
To: gnats-bugs@NetBSD.org
Subject: Undefined behavior in src/games/monop/cards.c
X-Send-Pr-Version: www-1.0

>Number:         43860
>Category:       bin
>Synopsis:       Undefined behavior in src/games/monop/cards.c
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Sep 09 20:40:01 +0000 2010
>Closed-Date:    Sun Sep 26 21:17:27 +0000 2010
>Last-Modified:  Sun Sep 26 21:17:27 +0000 2010
>Originator:     Chris Spiegel
>Release:        5.0.2
>Organization:
>Environment:
N/A: discovered while porting to another platform.
>Description:
Line 250 of src/games/monop/cards.c is:
dp->top_card = ++(dp->top_card) % dp->num_cards;

This violates §6.5p2 of C99 ("Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression.").  C89/C90 have identical wording.
>How-To-Repeat:

>Fix:
I presume the following implements the expected behavior:
dp->top_card = (dp->top_card + 1) % dp->num_cards;

That is, change the preincrement to a simple "x+1" construct.

>Release-Note:

>Audit-Trail:
From: "David A. Holland" <dholland@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/43860 CVS commit: src/games/monop
Date: Sun, 26 Sep 2010 21:12:24 +0000

 Module Name:	src
 Committed By:	dholland
 Date:		Sun Sep 26 21:12:24 UTC 2010

 Modified Files:
 	src/games/monop: cards.c

 Log Message:
 Avoid undefined behavior, from Chris Spiegel in PR 43860.


 To generate a diff of this commit:
 cvs rdiff -u -r1.23 -r1.24 src/games/monop/cards.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

State-Changed-From-To: open->closed
State-Changed-By: dholland@NetBSD.org
State-Changed-When: Sun, 26 Sep 2010 21:17:27 +0000
State-Changed-Why:
fixed, thanks


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.39 2013/11/01 18:47:49 spz Exp $
$NetBSD: gnats_config.sh,v 1.8 2006/05/07 09:23:38 tsutsui Exp $
Copyright © 1994-2007 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.