* [PATCH 1/4] lib/vsprintf.c: Consume 'p' in format_decode
@ 2015-01-08 12:14 Rasmus Villemoes
2015-01-08 12:14 ` [PATCH 2/4] lib/vsprintf.c: Improve sanity check in vsnprintf() Rasmus Villemoes
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Rasmus Villemoes @ 2015-01-08 12:14 UTC (permalink / raw)
To: Andrew Morton, Jiri Kosina, Randy Dunlap; +Cc: Rasmus Villemoes, linux-kernel
It seems a little simpler to consume the p from a %p specifier in
format_decode, just as it is done for the surrounding %c, %s and %%
cases.
While there, delete a redundant and misplaced comment.
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
lib/vsprintf.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index ec337f64f52d..98ad170b10e0 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1604,8 +1604,7 @@ qualifier:
case 'p':
spec->type = FORMAT_TYPE_PTR;
- return fmt - start;
- /* skip alnum */
+ return ++fmt - start;
case '%':
spec->type = FORMAT_TYPE_PERCENT_CHAR;
@@ -1794,7 +1793,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
break;
case FORMAT_TYPE_PTR:
- str = pointer(fmt+1, str, end, va_arg(args, void *),
+ str = pointer(fmt, str, end, va_arg(args, void *),
spec);
while (isalnum(*fmt))
fmt++;
@@ -2232,7 +2231,7 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
}
case FORMAT_TYPE_PTR:
- str = pointer(fmt+1, str, end, get_arg(void *), spec);
+ str = pointer(fmt, str, end, get_arg(void *), spec);
while (isalnum(*fmt))
fmt++;
break;
--
2.1.3
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 2/4] lib/vsprintf.c: Improve sanity check in vsnprintf()
2015-01-08 12:14 [PATCH 1/4] lib/vsprintf.c: Consume 'p' in format_decode Rasmus Villemoes
@ 2015-01-08 12:14 ` Rasmus Villemoes
2015-01-08 12:14 ` [PATCH 3/4] lib/vsprintf.c: Don't try to fix pointer wrap-around Rasmus Villemoes
2015-01-08 12:14 ` [PATCH 4/4] lib/vsprintf.c: Replace while with do-while in skip_atoi Rasmus Villemoes
2 siblings, 0 replies; 4+ messages in thread
From: Rasmus Villemoes @ 2015-01-08 12:14 UTC (permalink / raw)
To: Andrew Morton, Jiri Kosina, Randy Dunlap; +Cc: Rasmus Villemoes, linux-kernel
On 64 bit, size may very well be huge even if bit 31 happens to be
0. Somehow it doesn't feel right that one can pass a 5 GiB buffer but
not a 3 GiB one. So cap at INT_MAX as was probably the intention all
along. This is also the made-up value passed by sprintf and vsprintf.
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
lib/vsprintf.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 98ad170b10e0..cf12ba86205c 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1727,7 +1727,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
/* Reject out-of-range values early. Large positive sizes are
used for unknown buffer sizes. */
- if (WARN_ON_ONCE((int) size < 0))
+ if (WARN_ON_ONCE(size > INT_MAX))
return 0;
str = buf;
--
2.1.3
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 3/4] lib/vsprintf.c: Don't try to fix pointer wrap-around
2015-01-08 12:14 [PATCH 1/4] lib/vsprintf.c: Consume 'p' in format_decode Rasmus Villemoes
2015-01-08 12:14 ` [PATCH 2/4] lib/vsprintf.c: Improve sanity check in vsnprintf() Rasmus Villemoes
@ 2015-01-08 12:14 ` Rasmus Villemoes
2015-01-08 12:14 ` [PATCH 4/4] lib/vsprintf.c: Replace while with do-while in skip_atoi Rasmus Villemoes
2 siblings, 0 replies; 4+ messages in thread
From: Rasmus Villemoes @ 2015-01-08 12:14 UTC (permalink / raw)
To: Andrew Morton, Jiri Kosina, Randy Dunlap; +Cc: Rasmus Villemoes, linux-kernel
Actual kernel buffers can't wrap into the user address space. If
someone manages to pass a buf/size combination that wraps, it is most
likely due to a bug in the caller. Instead of trying to fix it by
using a smaller part of the buffer, bail out.
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
lib/vsprintf.c | 8 +++-----
1 file changed, 3 insertions(+), 5 deletions(-)
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index cf12ba86205c..1ec6a78f169b 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1733,11 +1733,9 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
str = buf;
end = buf + size;
- /* Make sure end is always >= buf */
- if (end < buf) {
- end = ((void *)-1);
- size = end - buf;
- }
+ /* Also bail out if buf+size wraps */
+ if (WARN_ON_ONCE(end < buf))
+ return 0;
while (*fmt) {
const char *old_fmt = fmt;
--
2.1.3
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 4/4] lib/vsprintf.c: Replace while with do-while in skip_atoi
2015-01-08 12:14 [PATCH 1/4] lib/vsprintf.c: Consume 'p' in format_decode Rasmus Villemoes
2015-01-08 12:14 ` [PATCH 2/4] lib/vsprintf.c: Improve sanity check in vsnprintf() Rasmus Villemoes
2015-01-08 12:14 ` [PATCH 3/4] lib/vsprintf.c: Don't try to fix pointer wrap-around Rasmus Villemoes
@ 2015-01-08 12:14 ` Rasmus Villemoes
2 siblings, 0 replies; 4+ messages in thread
From: Rasmus Villemoes @ 2015-01-08 12:14 UTC (permalink / raw)
To: Andrew Morton, Jiri Kosina, Randy Dunlap; +Cc: Rasmus Villemoes, linux-kernel
All callers of skip_atoi have already checked for the first character
being a digit. In this case, gcc generates simpler code for a do
while-loop.
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
lib/vsprintf.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 1ec6a78f169b..e152da53cbd2 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -114,8 +114,9 @@ int skip_atoi(const char **s)
{
int i = 0;
- while (isdigit(**s))
+ do {
i = i*10 + *((*s)++) - '0';
+ } while (isdigit(**s));
return i;
}
--
2.1.3
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2015-01-08 12:21 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-08 12:14 [PATCH 1/4] lib/vsprintf.c: Consume 'p' in format_decode Rasmus Villemoes
2015-01-08 12:14 ` [PATCH 2/4] lib/vsprintf.c: Improve sanity check in vsnprintf() Rasmus Villemoes
2015-01-08 12:14 ` [PATCH 3/4] lib/vsprintf.c: Don't try to fix pointer wrap-around Rasmus Villemoes
2015-01-08 12:14 ` [PATCH 4/4] lib/vsprintf.c: Replace while with do-while in skip_atoi Rasmus Villemoes
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®