LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Luca Ceresoli <luca@lucaceresoli.net>
To: linux-media@vger.kernel.org
Cc: Luca Ceresoli <luca@lucaceresoli.net>,
	Leon Luo <leonl@leopardimaging.com>,
	Mauro Carvalho Chehab <mchehab@kernel.org>,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 07/13] media: imx274: initialize format before v4l2 controls
Date: Tue, 24 Apr 2018 10:24:12 +0200	[thread overview]
Message-ID: <1524558258-530-8-git-send-email-luca@lucaceresoli.net> (raw)
In-Reply-To: <1524558258-530-1-git-send-email-luca@lucaceresoli.net>

The current probe function calls v4l2_ctrl_handler_setup() before
initializing the format info. This triggers call paths such as:
imx274_probe -> v4l2_ctrl_handler_setup -> imx274_s_ctrl ->
imx274_set_exposure, where priv->mode_index is accessed before being
assigned.

This is wrong but does not trigger a visible bug because priv is
zero-initialized and 0 is the default value for priv->mode_index. But
this would become a crash in follow-up commits when mode_index is
replaced by a pointer that must always be valid.

Fix the bug before it shows up by initializing struct members early.

Signed-off-by: Luca Ceresoli <luca@lucaceresoli.net>

---
Changed v1 -> v2:
 - add "media: " prefix to commit message
---
 drivers/media/i2c/imx274.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/drivers/media/i2c/imx274.c b/drivers/media/i2c/imx274.c
index 63fb94e7da37..8a8a11b8d75d 100644
--- a/drivers/media/i2c/imx274.c
+++ b/drivers/media/i2c/imx274.c
@@ -1632,6 +1632,16 @@ static int imx274_probe(struct i2c_client *client,
 
 	mutex_init(&imx274->lock);
 
+	/* initialize format */
+	imx274->mode_index = IMX274_MODE_3840X2160;
+	imx274->format.width = imx274_formats[0].size.width;
+	imx274->format.height = imx274_formats[0].size.height;
+	imx274->format.field = V4L2_FIELD_NONE;
+	imx274->format.code = MEDIA_BUS_FMT_SRGGB10_1X10;
+	imx274->format.colorspace = V4L2_COLORSPACE_SRGB;
+	imx274->frame_interval.numerator = 1;
+	imx274->frame_interval.denominator = IMX274_DEF_FRAME_RATE;
+
 	/* initialize regmap */
 	imx274->regmap = devm_regmap_init_i2c(client, &imx274_regmap_config);
 	if (IS_ERR(imx274->regmap)) {
@@ -1720,16 +1730,6 @@ static int imx274_probe(struct i2c_client *client,
 		goto err_ctrls;
 	}
 
-	/* initialize format */
-	imx274->mode_index = IMX274_MODE_3840X2160;
-	imx274->format.width = imx274_formats[0].size.width;
-	imx274->format.height = imx274_formats[0].size.height;
-	imx274->format.field = V4L2_FIELD_NONE;
-	imx274->format.code = MEDIA_BUS_FMT_SRGGB10_1X10;
-	imx274->format.colorspace = V4L2_COLORSPACE_SRGB;
-	imx274->frame_interval.numerator = 1;
-	imx274->frame_interval.denominator = IMX274_DEF_FRAME_RATE;
-
 	/* load default control values */
 	ret = imx274_load_default(imx274);
 	if (ret) {
-- 
2.7.4

  parent reply	other threads:[~2018-04-24  8:25 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-24  8:24 [PATCH v2 00/13] media: imx274: cleanups, improvements and SELECTION API support Luca Ceresoli
2018-04-24  8:24 ` [PATCH v2 01/13] media: imx274: document reset delays more clearly Luca Ceresoli
2018-04-24  8:24 ` [PATCH v2 02/13] media: imx274: fix typo in comment Luca Ceresoli
2018-04-24  8:24 ` [PATCH v2 03/13] media: imx274: slightly simplify code Luca Ceresoli
2018-04-24  8:24 ` [PATCH v2 04/13] media: imx274: remove unused data from struct imx274_frmfmt Luca Ceresoli
2018-04-24  8:24 ` [PATCH v2 05/13] media: imx274: rename and reorder register address definitions Luca Ceresoli
2018-04-24  8:24 ` [PATCH v2 06/13] media: imx274: remove non-indexed pointers from mode_table Luca Ceresoli
2018-04-24  8:24 ` Luca Ceresoli [this message]
2018-04-24  8:24 ` [PATCH v2 08/13] media: imx274: consolidate per-mode data in imx274_frmfmt Luca Ceresoli
2018-04-24  8:24 ` [PATCH v2 09/13] media: imx274: get rid of mode_index Luca Ceresoli
2018-04-24 17:27   ` kbuild test robot
2018-04-24  8:24 ` [PATCH v2 10/13] media: imx274: actually use IMX274_DEFAULT_MODE Luca Ceresoli
2018-04-24  8:24 ` [PATCH v2 11/13] media: imx274: simplify imx274_write_table() Luca Ceresoli
2018-04-24  8:24 ` [PATCH v2 12/13] media: imx274: add helper function to fill a reg_8 table chunk Luca Ceresoli
2018-04-24  8:24 ` [PATCH v2 13/13] media: imx274: add SELECTION support for cropping Luca Ceresoli
2018-04-24  9:59   ` Sakari Ailus
2018-04-24 14:30     ` Luca Ceresoli
2018-04-26  8:13       ` Sakari Ailus
2018-04-26 13:43         ` Luca Ceresoli

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=1524558258-530-8-git-send-email-luca@lucaceresoli.net \
    --to=luca@lucaceresoli.net \
    --cc=leonl@leopardimaging.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --subject='Re: [PATCH v2 07/13] media: imx274: initialize format before v4l2 controls' \
    /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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).