Thread (3 messages) read the whole thread 3 messages, 3 authors, 2021-08-31
STALE1794d REVIEWED: 1 (0M)

1 review trailer.

[PATCH] lib: sbi: Refine the way to construct platform features

From: Atish Patra <hidden>
Date: 2021-08-30 21:32:19

On Mon, Aug 30, 2021 at 6:59 AM Dong Du [off-list ref] wrote:
quoted hunk ↗ jump to hunk
sbi_platform_get_features_str() uses sbi_snprintf() to construct the
features_str. However, it passes the wrong length value (i.e., the nfstr),
which should be (nfstr-offset) as the starting point of str (i.e., features_str + offset)
changes.
This commit also checks the return value of snprintf, and handles the corner case
that the string buffer is full.

Signed-off-by: Dong Du <redacted>
---
 lib/sbi/sbi_platform.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/lib/sbi/sbi_platform.c b/lib/sbi/sbi_platform.c
index 99bd8f5..56c37aa 100644
--- a/lib/sbi/sbi_platform.c
+++ b/lib/sbi/sbi_platform.c
@@ -48,9 +48,17 @@ void sbi_platform_get_features_str(const struct sbi_platform *plat,
                if (features & feat) {
                        temp = sbi_platform_feature_id2string(feat);
                        if (temp) {
-                               sbi_snprintf(features_str + offset, nfstr,
+                               int len = sbi_snprintf(features_str + offset, nfstr - offset,
                                             "%s,", temp);
-                               offset = offset + sbi_strlen(temp) + 1;
+                               if (len < 0)
+                                       break;
+
+                               if (offset + len >= nfstr) {
+                                       // No more space for features
+                                       offset = nfstr;
+                                       break;
+                               } else
+                                       offset = offset + len;
                        }
                }
                feat = feat << 1;
--
2.31.1


--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
Thanks for catching it. Looks good

Reviewed-by: Atish Patra <redacted>


-- 
Regards,
Atish
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help