[PATCH 1/5] sysreset: mpc83xx: use scnprintf instead of snprintf

Rasmus Villemoes rasmus.villemoes at prevas.dk
Fri May 1 16:55:12 CEST 2020


Neither snprintf or scnprintf ever return a negative value, so the
error checking is pointless. The correct idiom for printing piecemeal
to a buffer of a given size is to use scnprintf(), since that will
ensure buf will never point past the actual given buffer, and the
remaining size will never become negative (since scnprintf(), when
given a non-zero size, has the property that the return value is
strictly less than the given size).

Signed-off-by: Rasmus Villemoes <rasmus.villemoes at prevas.dk>
---
 drivers/sysreset/sysreset_mpc83xx.c | 54 ++++++++++-------------------
 1 file changed, 19 insertions(+), 35 deletions(-)

diff --git a/drivers/sysreset/sysreset_mpc83xx.c b/drivers/sysreset/sysreset_mpc83xx.c
index 9092764e0b..7148464d8b 100644
--- a/drivers/sysreset/sysreset_mpc83xx.c
+++ b/drivers/sysreset/sysreset_mpc83xx.c
@@ -104,23 +104,23 @@ static int print_83xx_arb_event(bool force, char *buf, int size)
 		return 0;
 
 	if (CONFIG_IS_ENABLED(CONFIG_DISPLAY_AER_FULL)) {
-		res = snprintf(buf, size,
-			       "Arbiter Event Status:\n"
-			       "    %s: 0x%08lX\n"
-			       "    %s:    0x%1x  = %s\n"
-			       "    %s:     0x%02x = %s\n"
-			       "    %s: 0x%1x  = %d bytes\n"
-			       "    %s: 0x%02x = %s\n",
-			       "Event Address", gd->arch.arbiter_event_address,
-			       "Event Type", etype, event[etype],
-			       "Master ID", mstr_id, master[mstr_id],
-			       "Transfer Size", tsize_val, tsize_bytes,
-			       "Transfer Type", ttype, transfer[ttype]);
+		res = scnprintf(buf, size,
+				"Arbiter Event Status:\n"
+				"    %s: 0x%08lX\n"
+				"    %s:    0x%1x  = %s\n"
+				"    %s:     0x%02x = %s\n"
+				"    %s: 0x%1x  = %d bytes\n"
+				"    %s: 0x%02x = %s\n",
+				"Event Address", gd->arch.arbiter_event_address,
+				"Event Type", etype, event[etype],
+				"Master ID", mstr_id, master[mstr_id],
+				"Transfer Size", tsize_val, tsize_bytes,
+				"Transfer Type", ttype, transfer[ttype]);
 	} else if (CONFIG_IS_ENABLED(CONFIG_DISPLAY_AER_BRIEF)) {
-		res = snprintf(buf, size,
-			       "Arbiter Event Status: AEATR=0x%08lX, AEADR=0x%08lX\n",
-			       gd->arch.arbiter_event_attributes,
-			       gd->arch.arbiter_event_address);
+		res = scnprintf(buf, size,
+				"Arbiter Event Status: AEATR=0x%08lX, AEADR=0x%08lX\n",
+				gd->arch.arbiter_event_attributes,
+				gd->arch.arbiter_event_address);
 	}
 
 	return res;
@@ -150,13 +150,7 @@ static int mpc83xx_sysreset_get_status(struct udevice *dev, char *buf, int size)
 	int i;
 	char *sep;
 
-	res = snprintf(buf, size, "Reset Status:");
-	if (res < 0) {
-		debug("%s: Could not write reset status message (err = %d)\n",
-		      dev->name, res);
-		return -EIO;
-	}
-
+	res = scnprintf(buf, size, "Reset Status:");
 	buf += res;
 	size -= res;
 
@@ -164,13 +158,8 @@ static int mpc83xx_sysreset_get_status(struct udevice *dev, char *buf, int size)
 	for (i = 0; i < ARRAY_SIZE(bits); i++)
 		/* Print description of set bits */
 		if (rsr & bits[i].mask) {
-			res = snprintf(buf, size, "%s%s%s", sep, bits[i].desc,
+			res = scnprintf(buf, size, "%s%s%s", sep, bits[i].desc,
 				       (i == ARRAY_SIZE(bits) - 1) ? "\n" : "");
-			if (res < 0) {
-				debug("%s: Could not write reset status message (err = %d)\n",
-				      dev->name, res);
-				return -EIO;
-			}
 			buf += res;
 			size -= res;
 			sep = ", ";
@@ -187,15 +176,10 @@ static int mpc83xx_sysreset_get_status(struct udevice *dev, char *buf, int size)
 		 * event to be printed
 		 */
 		res = print_83xx_arb_event(rsr & RSR_BMRS, buf, size);
-		if (res < 0) {
-			debug("%s: Could not write arbiter event message (err = %d)\n",
-			      dev->name, res);
-			return -EIO;
-		}
 		buf += res;
 		size -= res;
 	}
-	snprintf(buf, size, "\n");
+	scnprintf(buf, size, "\n");
 
 	return 0;
 }
-- 
2.23.0



More information about the U-Boot mailing list