linux-rdma.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] RDMA/ocrdma: Fix an off-by-one issue in 'ocrdma_add_stat'
@ 2020-03-28  7:30 Christophe JAILLET
  2020-04-14 18:34 ` Jason Gunthorpe
  0 siblings, 1 reply; 13+ messages in thread
From: Christophe JAILLET @ 2020-03-28  7:30 UTC (permalink / raw)
  To: selvin.xavier, devesh.sharma, dledford, jgg, leon, colin.king,
	roland
  Cc: linux-rdma, linux-kernel, kernel-janitors, Christophe JAILLET

There is an off-by-one issue when checking if there is enough space in the
output buffer, because we must keep some place for a final '\0'.

While at it:
   - Use 'scnprintf' instead of 'snprintf' in order to avoid a superfluous
    'strlen'
   - avoid some useless initializations
   - avoida hard coded buffer size that can be computed at built time.

Fixes: a51f06e1679e ("RDMA/ocrdma: Query controller information")
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
The '\0' comes from memset(..., 0, ...) in all callers.
This could be also avoided if needed.
---
 drivers/infiniband/hw/ocrdma/ocrdma_stats.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/drivers/infiniband/hw/ocrdma/ocrdma_stats.c b/drivers/infiniband/hw/ocrdma/ocrdma_stats.c
index 5f831e3bdbad..614a449e6b87 100644
--- a/drivers/infiniband/hw/ocrdma/ocrdma_stats.c
+++ b/drivers/infiniband/hw/ocrdma/ocrdma_stats.c
@@ -49,13 +49,12 @@ static struct dentry *ocrdma_dbgfs_dir;
 static int ocrdma_add_stat(char *start, char *pcur,
 				char *name, u64 count)
 {
-	char buff[128] = {0};
-	int cpy_len = 0;
+	char buff[128];
+	int cpy_len;
 
-	snprintf(buff, 128, "%s: %llu\n", name, count);
-	cpy_len = strlen(buff);
+	cpy_len = scnprintf(buff, sizeof(buff), "%s: %llu\n", name, count);
 
-	if (pcur + cpy_len > start + OCRDMA_MAX_DBGFS_MEM) {
+	if (pcur + cpy_len >= start + OCRDMA_MAX_DBGFS_MEM) {
 		pr_err("%s: No space in stats buff\n", __func__);
 		return 0;
 	}
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2020-04-17 15:56 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-03-28  7:30 [PATCH] RDMA/ocrdma: Fix an off-by-one issue in 'ocrdma_add_stat' Christophe JAILLET
2020-04-14 18:34 ` Jason Gunthorpe
2020-04-16 13:08   ` Dan Carpenter
2020-04-16 18:47     ` Jason Gunthorpe
2020-04-17 11:26       ` Dan Carpenter
2020-04-17 12:25         ` Jason Gunthorpe
2020-04-17 13:09           ` Dan Carpenter
2020-04-17 13:48             ` Jason Gunthorpe
2020-04-17 15:13               ` Dan Carpenter
2020-04-17 15:56                 ` Jason Gunthorpe
2020-04-17 13:28   ` Marion & Christophe JAILLET
2020-04-17 13:50     ` Jason Gunthorpe
2020-04-17 14:39       ` Christophe JAILLET

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).