mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: Guanglei Zhu <zhugl3@xiaopeng.com>
Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org,
	liuc63@xiaopeng.com, stable@vger.kernel.org
Subject: Re: [PATCH v2] Input: joydev - avoid signed overflow in the broken-axis correction
Date: Tue, 03 Nov 2026 19:38:52 -0800	[thread overview]
Message-ID: <8063a24bb9f75bb9096c947d8ffa7123@gmail.com> (raw)
In-Reply-To: <20261103085514.431010-1-zhugl3@xiaopeng.com>

Hi Guanglei,

On Tue, Nov 03, 2026 at 04:55:14PM +0800, Guanglei Zhu wrote:
> The JS_CORR_BROKEN correction multiplies two user-controlled 32-bit
> values before shifting and clamping the result:
...
> -	case JS_CORR_BROKEN:
> -		value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 :
> -			((corr->coef[3] * (value - corr->coef[1])) >> 14)) :
> -			((corr->coef[2] * (value - corr->coef[0])) >> 14);
> +	case JS_CORR_BROKEN: {
> +		s64 tmp = value > corr->coef[0] ?
> +			(value < corr->coef[1] ? 0 :
> +			 (s64)corr->coef[3] * ((s64)value - corr->coef[1])) :
> +			(s64)corr->coef[2] * ((s64)value - corr->coef[0]);
> +
> +		value = clamp_t(s64, tmp >> 14, -32767, 32767);
>  		break;
> +	}

Assigning to value and breaking out of the switch causes value to be
clamped a second time at the end of joydev_correct(). Also, pre-converting
value to s64 and replacing the nested ternary with if/else statements
avoids all the explicit (s64) casts and returns 0 immediately when the
input is in the deadzone:

static int joydev_correct(int value, struct js_corr *corr)
{
	s64 v64;

	switch (corr->type) {
	case JS_CORR_NONE:
		return clamp(value, -32767, 32767);

	case JS_CORR_BROKEN:
		v64 = value;
		if (value < corr->coef[0])
			v64 = (v64 - corr->coef[0]) * corr->coef[2];
		else if (value > corr->coef[0] && value > corr->coef[1])
			v64 = (v64 - corr->coef[1]) * corr->coef[3];
		else
			return 0;

		return clamp_val(v64 >> 14, -32767, 32767);

	default:
		return 0;
	}
}

Also, please note that the kernel builds with -fno-strict-overflow, so
signed overflow is defined as two's-complement wraparound rather than
compiler undefined behavior, and please drop Fixes and Cc: stable as
this only affects synthetic coefficients passed via JSIOCSCORR.

Thanks.

-- 
Dmitry

  reply	other threads:[~2026-11-04  3:38 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-03  8:41 [PATCH] Input: joydev - avoid an undefined shift in the legacy 0x read Guanglei Zhu
2026-11-03  8:41 ` [PATCH] Input: joydev - avoid signed overflow in the broken-axis correction Guanglei Zhu
2026-11-03  8:55   ` [PATCH v2] " Guanglei Zhu
2026-11-04  3:38     ` Dmitry Torokhov [this message]
2026-11-04  5:50       ` [PATCH v3] " Guanglei Zhu
2026-11-04  6:02       ` Guanglei Zhu
2026-11-03  8:41 ` [PATCH] Input: adi - bound the two-stream packet merge Guanglei Zhu
2026-11-03 11:17   ` [PATCH v2] " Guanglei Zhu
2026-11-03  8:41 ` [PATCH] Input: db9 - enlarge the Saturn multitap packet buffer Guanglei Zhu
2026-11-03  8:41 ` [PATCH] Input: lpc32xx-keys - reject matrices bigger than the hardware has Guanglei Zhu
2026-11-03 18:54   ` Dmitry Torokhov
2026-11-04  3:07     ` [PATCH v2] " Guanglei Zhu
2026-11-04  6:25       ` Dmitry Torokhov
2026-11-03  8:41 ` [PATCH] Input: mt6779-keypad - check the reported row and column Guanglei Zhu
2026-11-03  8:41 ` [PATCH] Input: tegra-kbc - check the pin numbers from the device tree Guanglei Zhu
2026-11-03  8:41 ` [PATCH] Input: cyapa_gen3 - clamp the reported finger count to the array Guanglei Zhu
2026-11-03 18:45 ` [PATCH] Input: joydev - avoid an undefined shift in the legacy 0x read Dmitry Torokhov
2026-11-04  3:07   ` [PATCH v2] " Guanglei Zhu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8063a24bb9f75bb9096c947d8ffa7123@gmail.com \
    --to=dmitry.torokhov@gmail.com \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=liuc63@xiaopeng.com \
    --cc=stable@vger.kernel.org \
    --cc=zhugl3@xiaopeng.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®