From patchwork Sat Apr 5 16:25:58 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Vetter X-Patchwork-Id: 3942261 Return-Path: X-Original-To: patchwork-dri-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 1BBA79F334 for ; Sat, 5 Apr 2014 16:26:11 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 3C1E42035E for ; Sat, 5 Apr 2014 16:26:10 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 74AD320251 for ; Sat, 5 Apr 2014 16:26:08 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 5E3036E1F6; Sat, 5 Apr 2014 09:26:07 -0700 (PDT) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wg0-f51.google.com (mail-wg0-f51.google.com [74.125.82.51]) by gabe.freedesktop.org (Postfix) with ESMTP id F19B66E1F6 for ; Sat, 5 Apr 2014 09:26:04 -0700 (PDT) Received: by mail-wg0-f51.google.com with SMTP id k14so4746965wgh.34 for ; Sat, 05 Apr 2014 09:26:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=SaLtOwErSjiM7mpv5reYk0FSiwBy3FoIFQXdJjWT2R0=; b=HUghgkAbkVk3mLgjp5IV5Wbhe2Vunh5Vgkp4AIl+EGMqZLtNfyMY0ThDT0u+Doxhcn hRKguM/Fk0YPCLIwdPnKpS/jrLYAFJNacJk2Sxx/pZY17WmX9/ngFjg/vjzldtCl7rCk wcv8sUVK1zNthzO7LWVR8m1SBGiKafqi1Tb9g= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=SaLtOwErSjiM7mpv5reYk0FSiwBy3FoIFQXdJjWT2R0=; b=RlGdiTksUre/DZnGD4s+V0I+ROMjsXFOSuwIKOXcIR+JStcg7Z4CGFt6C/j4lPe93d qL3CjUHrrPzZ/h7BtdLPDWHhDQHe8S5vuYNQWsyR7cYOKNDZZeT+ixcKI/qZpoXIeWeF 9X9PIrSjOInoMrfUNiK06kZygKfQQWs5AYqnSDw9dUGckHnbYOwfxFNsbwjwlnP/ltWH lMYtwJpbuQSvVB9tV2d+APN1ETtNKGUxzmbTmSrT353lQDkbYoiabadoec9fU+5qmDjn 2FyPicLHhFLxbt3R6aXAaiv6d/xi9YjJ/2STQmEPlBnkls6X+deDOtGjVlmSDnS2D1eG ftXw== X-Gm-Message-State: ALoCoQmhJeW7GgNNajd1nk955lTEcPExnVrOQyNeJi4ugwM9DfOizjwqhUO5TsJCv9tQN35SMcD5 X-Received: by 10.180.189.139 with SMTP id gi11mr12986929wic.53.1396715163665; Sat, 05 Apr 2014 09:26:03 -0700 (PDT) Received: from phenom.ffwll.local (84-73-67-144.dclient.hispeed.ch. [84.73.67.144]) by mx.google.com with ESMTPSA id bc51sm28051166eeb.22.2014.04.05.09.26.02 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 05 Apr 2014 09:26:02 -0700 (PDT) From: Daniel Vetter To: DRI Development Subject: [PATCH] drm/i2c/tda998x: Fix signed overflow issue Date: Sat, 5 Apr 2014 18:25:58 +0200 Message-Id: <1396715158-16953-1-git-send-email-daniel.vetter@ffwll.ch> X-Mailer: git-send-email 1.8.5.2 In-Reply-To: <20140405132932.6f3fc0fc@armhf> References: <20140405132932.6f3fc0fc@armhf> Cc: Dave Jones , Russell King , Daniel Vetter X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Spam-Status: No, score=-4.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This is C standard hair-splitting, but afaict - sum will be promoted to signed int in computation since uint8_t fits - signed overflow is undefined. No we need to add up an awful lot of bytes to actually make it overflow. But I guess the real risk is gcc spotting this and going bananas. Fix this by simply using unsigned in to force all computations to use the well-defined unsigned behaviour. Spotted by coverity. v2: Simplify the entire computation as suggested by Jean. Cc: Russell King Cc: Rob Clark Cc: Jean-Francois Moine Signed-off-by: Daniel Vetter --- drivers/gpu/drm/i2c/tda998x_drv.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/gpu/drm/i2c/tda998x_drv.c b/drivers/gpu/drm/i2c/tda998x_drv.c index 48af5cac1902..240c331405b9 100644 --- a/drivers/gpu/drm/i2c/tda998x_drv.c +++ b/drivers/gpu/drm/i2c/tda998x_drv.c @@ -568,11 +568,11 @@ static irqreturn_t tda998x_irq_thread(int irq, void *data) static uint8_t tda998x_cksum(uint8_t *buf, size_t bytes) { - uint8_t sum = 0; + int sum = 0; while (bytes--) - sum += *buf++; - return (255 - sum) + 1; + sum -= *buf++; + return sum; } #define HB(x) (x)