Skip to contentSkip to conversation

rtc: fix snprintf() checking in is_rtc_hctosys()

PATCH Applied Dan Carpenter 2 participants 2 messages May 11, 2021 – May 24, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rtc: fix snprintf() checking in is_rtc_hctosys()

The scnprintf() function silently truncates the printf() and returns the number bytes that it was able to copy (not counting the NUL terminator). Thus, the highest value it can return here is "NAME_SIZE - 1" and the overflow check is dead code. Fix this by using the snprintf() function which returns the number of bytes that would have been copied if there was enough space and changing the condition from "> NAME_SIZE" to ">= NAME_SIZE".

Fixes 92589c986b33 ("rtc-proc: permit the /proc/driver/rtc device to use other devices")
---
 drivers/rtc/proc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/rtc/proc.c +2 -2
index 73344598fc1b..cbcdbb19d848 100644
@@ -23,8 +23,8 @@ static bool is_rtc_hctosys(struct rtc_device *rtc)
 	int size;
 	char name[NAME_SIZE];
 
-	size = scnprintf(name, NAME_SIZE, "rtc%d", rtc->id);
-	if (size > NAME_SIZE)
+	size = snprintf(name, NAME_SIZE, "rtc%d", rtc->id);
+	if (size >= NAME_SIZE)
 		return false;
 
 	return !strncmp(name, CONFIG_RTC_HCTOSYS_DEVICE, NAME_SIZE);
Signature · 1 line
2.30.2
maintainer Applied
#1 lore raw
Quoted 7 lines · Dan Carpenter

The scnprintf() function silently truncates the printf() and returns the number bytes that it was able to copy (not counting the NUL terminator). Thus, the highest value it can return here is "NAME_SIZE - 1" and the overflow check is dead code. Fix this by using the snprintf() function which returns the number of bytes that would have been copied if there was enough space and changing the condition from "> NAME_SIZE" to ">= NAME_SIZE".

Applied, thanks!

[1/1] rtc: fix snprintf() checking in is_rtc_hctosys()

      commit: 54b909436ede47e0ee07f1765da27ec2efa41e84

Best regards,

Signature · 1 line
Alexandre Belloni <[email protected]>