[RFC PATCH 2/3] lib/vsprintf.c: append "..." if the *pb[l] output has been truncated.

From: Maurizio Lombardi
Date: Wed Sep 16 2015 - 05:09:48 EST


The *pb[l] format may generate a very long string that could exaust
the output buffer capacity;
when such event happens the output could be misleading,
it may appear valid but part of it has been truncated.

This patch modifies the bitmap_*_string() functions so they will append
"..." to the output to inform the user that a truncation happened.

Signed-off-by: Maurizio Lombardi <mlombard@xxxxxxxxxx>
---
lib/vsprintf.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 48 insertions(+)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 8707d91..f49bf54 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -816,6 +816,7 @@ char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
int i, chunksz;
bool first = true;
struct printf_spec spec = *specp;
+ const char *buf_start = buf;

/* reused to print numbers */
spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
@@ -846,6 +847,29 @@ char *bitmap_string(char *buf, char *end, unsigned long *bitmap,

chunksz = CHUNKSZ;
}
+
+ if (buf >= end && buf_start != end) {
+ int spc = 0;
+ char *trunc = end - 1;
+
+ while (trunc > buf_start) {
+ if (*trunc == ',' && spc > 3) {
+ trunc++;
+ break;
+ }
+ trunc--;
+ spc++;
+ }
+
+ if (spc > 3) {
+ trunc[0] = '.';
+ trunc[1] = '.';
+ trunc[2] = '.';
+ trunc[3] = '\0';
+ } else
+ trunc[0] = '\0';
+ }
+
return buf;
}

@@ -858,6 +882,7 @@ char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
int cur, rbot, rtop;
bool first = true;
struct printf_spec spec = *specp;
+ const char *buf_start = buf;

/* reused to print numbers */
spec = (struct printf_spec){ .base = 10 };
@@ -887,6 +912,29 @@ char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,

rbot = cur;
}
+
+ if (buf >= end && buf_start != end) {
+ int spc = 0;
+ char *trunc = end - 1;
+
+ while (trunc > buf_start) {
+ if (*trunc == ',' && spc > 3) {
+ trunc++;
+ break;
+ }
+ trunc--;
+ spc++;
+ }
+
+ if (spc > 3) {
+ trunc[0] = '.';
+ trunc[1] = '.';
+ trunc[2] = '.';
+ trunc[3] = '\0';
+ } else
+ trunc[0] = '\0';
+ }
+
return buf;
}

--
Maurizio Lombardi

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/