Skip to contentSkip to conversation

rtc: gamecube: Check the return value of ioremap()

PATCH Superseded Haotian Zhang 3 participants 5 messages Nov 26, 2025 – Dec 8, 2025 Real time clock (RTC)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rtc: gamecube: Check the return value of ioremap()

The function ioremap() in gamecube_rtc_read_offset_from_sram() can fail and return NULL, which is dereferenced without checking, leading to a NULL pointer dereference.

Add a check for the return value of ioremap() and return -ENOMEM on failure.

Fixes 86559400b3ef ("rtc: gamecube: Add a RTC driver for the GameCube, Wii and Wii U")
---
 drivers/rtc/rtc-gamecube.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/rtc/rtc-gamecube.c +4 -0
index c828bc8e05b9..cd7714437107 100644
@@ -242,6 +242,10 @@ static int gamecube_rtc_read_offset_from_sram(struct priv *d)
 	}
 
 	hw_srnprot = ioremap(res.start, resource_size(&res));
+	if (!hw_srnprot) {
+		pr_err("Failed to ioremap hw_srnprot\n");
+		return -ENOMEM;
+	}
 	old = ioread32be(hw_srnprot);
 
 	/* TODO: figure out why we use this magic constant.  I obtained it by
Signature · 1 line
2.50.1.windows.1
Reviewed-by on v1 1/1
#1 lore raw

Good catch!

Quoted 18 lines · Haotian Zhang

The function ioremap() in gamecube_rtc_read_offset_from_sram() can fail and return NULL, which is dereferenced without checking, leading to a NULL pointer dereference.

Add a check for the return value of ioremap() and return -ENOMEM on failure.

Fixes 86559400b3ef ("rtc: gamecube: Add a RTC driver for the GameCube, Wii and Wii U")
---
 drivers/rtc/rtc-gamecube.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/rtc/rtc-gamecube.c +0 -0
index c828bc8e05b9..cd7714437107 100644

drivers/rtc/rtc-gamecube.c static int gamecube_rtc_read_offset_from_sram(struct priv *d) · line 246

Quoted 5 lines
drivers/rtc/rtc-gamecube.c +2 -0
@@ -242,6 +242,10 @@ static int gamecube_rtc_read_offset_from_sram(struct priv *d)
 	}
 
 	hw_srnprot = ioremap(res.start, resource_size(&res));
+	if (!hw_srnprot) {
+		pr_err("Failed to ioremap hw_srnprot\n");

The error messages on lines 240 and 276 start with a lowercase letter, please use the same case for this message. From a quick grep through the kernel, it seems we use either lowercase or uppercase, but I’d prefer to keep the case consistent in this driver at least.

Quoted 8 lines
drivers/rtc/rtc-gamecube.c +2 -0
+		return -ENOMEM;
+	}
 	old = ioread32be(hw_srnprot);
 
 	/* TODO: figure out why we use this magic constant.  I obtained it by
Signature · 1 line
2.50.1.windows.1

With that change:

Signature · 1 line
Link Mauve
#2 lore raw

PATCH v2 1/1 rtc: gamecube: Check the return value of ioremap()

In reply to Haotian Zhang

The function ioremap() in gamecube_rtc_read_offset_from_sram() can fail and return NULL, which is dereferenced without checking, leading to a NULL pointer dereference.

Add a check for the return value of ioremap() and return -ENOMEM on failure.

Fixes 86559400b3ef ("rtc: gamecube: Add a RTC driver for the GameCube, Wii and Wii U")

---
Changes in v2:
-Use lowercase for error message to match existing style.

---
 drivers/rtc/rtc-gamecube.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/rtc/rtc-gamecube.c +4 -0
index c828bc8e05b9..045d5d45ab4b 100644
@@ -242,6 +242,10 @@ static int gamecube_rtc_read_offset_from_sram(struct priv *d)
 	}
 
 	hw_srnprot = ioremap(res.start, resource_size(&res));
+	if (!hw_srnprot) {
+		pr_err("failed to ioremap hw_srnprot\n");
+		return -ENOMEM;
+	}
 	old = ioread32be(hw_srnprot);
 
 	/* TODO: figure out why we use this magic constant.  I obtained it by
Signature · 1 line
2.50.1.windows.1
Reviewed-by on v2 1/1
#3 lore raw

Hi,

Quoted 9 lines · Haotian Zhang

The function ioremap() in gamecube_rtc_read_offset_from_sram() can fail and return NULL, which is dereferenced without checking, leading to a NULL pointer dereference.

Add a check for the return value of ioremap() and return -ENOMEM on failure.

Fixes 86559400b3ef ("rtc: gamecube: Add a RTC driver for the GameCube, Wii and Wii U")

You forgot to carry my R-b, but here it is again:

Quoted 25 lines

---
Changes in v2:
-Use lowercase for error message to match existing style.

---
 drivers/rtc/rtc-gamecube.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/rtc/rtc-gamecube.c +4 -0
index c828bc8e05b9..045d5d45ab4b 100644
@@ -242,6 +242,10 @@ static int gamecube_rtc_read_offset_from_sram(struct priv *d)
 	}
 
 	hw_srnprot = ioremap(res.start, resource_size(&res));
+	if (!hw_srnprot) {
+		pr_err("failed to ioremap hw_srnprot\n");
+		return -ENOMEM;
+	}
 	old = ioread32be(hw_srnprot);
 
 	/* TODO: figure out why we use this magic constant.  I obtained it by
Signature · 1 line
2.50.1.windows.1
Signature · 1 line
Link Mauve
maintainer Applied
#4 lore raw
In reply to Haotian Zhang
Quoted 8 lines · Haotian Zhang

The function ioremap() in gamecube_rtc_read_offset_from_sram() can fail and return NULL, which is dereferenced without checking, leading to a NULL pointer dereference.

Add a check for the return value of ioremap() and return -ENOMEM on failure.

[...]

Applied, thanks!

[1/1] rtc: gamecube: Check the return value of ioremap()

      https://git.kernel.org/abelloni/c/d1220e47e4bd

Best regards,

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com