[ofa-general] [PATCH] infiniband-diags: fix snprintf() usage

Sasha Khapyorsky sashak at voltaire.com
Sun Jun 29 09:02:14 PDT 2008


snprintf() can return the value larger than provided buffer size - check
is needed.

Signed-off-by: Sasha Khapyorsky <sashak at voltaire.com>
---
 infiniband-diags/src/ibroute.c   |    6 +++++-
 infiniband-diags/src/ibsysstat.c |    2 ++
 2 files changed, 7 insertions(+), 1 deletions(-)

diff --git a/infiniband-diags/src/ibroute.c b/infiniband-diags/src/ibroute.c
index 3a6907b..9f32b39 100644
--- a/infiniband-diags/src/ibroute.c
+++ b/infiniband-diags/src/ibroute.c
@@ -108,7 +108,11 @@ dump_mlid(char *str, int strlen, int mlid, int nports,
 			mask = ntohs(mft[i][mlid%IB_MLIDS_IN_BLOCK]);
 			if (mask)
 				nonzero++;
-			n += snprintf(str + n, strlen -n, "%04hx", mask);
+			n += snprintf(str + n, strlen - n, "%04hx", mask);
+			if (n >= strlen) {
+				n = strlen;
+				break;
+			}
 		}
 		if (!nonzero && !dump_all) {
 			str[0] = 0;
diff --git a/infiniband-diags/src/ibsysstat.c b/infiniband-diags/src/ibsysstat.c
index 8e00baf..2418c9e 100644
--- a/infiniband-diags/src/ibsysstat.c
+++ b/infiniband-diags/src/ibsysstat.c
@@ -100,6 +100,8 @@ mk_reply(int attr, void *data, int sz)
 		for (i = 0; i < host_ncpu && sz > 0; i++) {
 			n = snprintf(s, sz, "cpu %d: model %s MHZ %s\n",
 				     i, cpus[i].model, cpus[i].mhz);
+			if (n >= sz)
+				break;
 			sz -= n;
 			s += n;
 		}
-- 
1.5.5.1.178.g1f811




More information about the general mailing list