[PATCH i-g-t v7 39/48] lib/i915/dp: Properly check sscanf results

Louis Chauvet
2026-02-23T12:09:41Z

Current sscanf usage can fail silently, add assert to ensure that the
returned value is correct.

Reviewed-by: Kory Maincent <kory.maincent@bootlin.com>
Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
---
lib/i915/i915_dp.c | 18 ++++++++++++++----
1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/lib/i915/i915_dp.c b/lib/i915/i915_dp.c
index da3dd05c3293..c2cddb955d75 100644
--- a/lib/i915/i915_dp.c
+++ b/lib/i915/i915_dp.c
@@ -128,7 +128,9 @@ int i915_dp_get_max_link_rate(int drm_fd, igt_output_t *output)
igt_assert_f(res == 0, "Unable to read %s/i915_dp_max_link_rate\n",
output->name);

- sscanf(buf, "%d", &ret);
+ igt_assert_f(sscanf(buf, "%d", &ret) == 1,
+ "Failed to parse max link rate from %s\n", buf);
+
return ret;
}

@@ -150,7 +152,9 @@ int i915_dp_get_max_lane_count(int drm_fd, igt_output_t *output)
igt_assert_f(res == 0, "Unable to read %s/i915_dp_max_lane_count\n",
output->name);

- sscanf(buf, "%d", &ret);
+ igt_assert_f(sscanf(buf, "%d", &ret) == 1,
+ "Failed to parse max lane count from %s\n", buf);
+
return ret;
}

@@ -257,7 +261,10 @@ int i915_dp_get_pending_lt_failures(int drm_fd, igt_output_t *output)
buf, sizeof(buf));
igt_assert_f(res == 0, "Unable to read %s/i915_dp_force_link_training_failure\n",
output->name);
- sscanf(buf, "%d", &ret);
+
+ igt_assert_f(sscanf(buf, "%d", &ret) == 1,
+ "Failed to parse pending link training failures from %s\n", buf);
+
return ret;
}

@@ -278,7 +285,10 @@ int i915_dp_get_pending_retrain(int drm_fd, igt_output_t *output)
buf, sizeof(buf));
igt_assert_f(res == 0, "Unable to read %s/i915_dp_force_link_retrain\n",
output->name);
- sscanf(buf, "%d", &ret);
+
+ igt_assert_f(sscanf(buf, "%d", &ret) == 1,
+ "Failed to parse pending link retrains from %s\n", buf);
+
return ret;
}

--
2.52.0