diff options
author | Josh Stone <jistone@redhat.com> | 2010-02-15 21:27:37 -0800 |
---|---|---|
committer | Josh Stone <jistone@redhat.com> | 2010-02-16 15:55:01 -0800 |
commit | d9f58253e30ea80e57d8f54e41e9cd114cc13973 (patch) | |
tree | 557c38cd069499be0defe734595af161a166bd98 /runtime | |
parent | 4fa8e6497405fd4f121a3eee0c6d772aaeeef6d8 (diff) | |
download | systemtap-steved-d9f58253e30ea80e57d8f54e41e9cd114cc13973.tar.gz systemtap-steved-d9f58253e30ea80e57d8f54e41e9cd114cc13973.tar.xz systemtap-steved-d9f58253e30ea80e57d8f54e41e9cd114cc13973.zip |
Use clamping to more easily normalize input values
The kernel has min/max/clamp macros to make range comparisons easier.
Clamp is a newer invention, but we can define it for older kernels in
terms of min and max.
Diffstat (limited to 'runtime')
-rw-r--r-- | runtime/runtime.h | 8 | ||||
-rw-r--r-- | runtime/string.c | 4 | ||||
-rw-r--r-- | runtime/vsprintf.c | 12 |
3 files changed, 16 insertions, 8 deletions
diff --git a/runtime/runtime.h b/runtime/runtime.h index 91c48d57..c51520d0 100644 --- a/runtime/runtime.h +++ b/runtime/runtime.h @@ -50,6 +50,14 @@ #define stp_for_each_cpu(cpu) for_each_cpu_mask((cpu), cpu_possible_map) #endif +#ifndef clamp +#define clamp(val, low, high) min(max(low, val), high) +#endif + +#ifndef clamp_t +#define clamp_t(type, val, low, high) min_t(type, max_t(type, low, val), high) +#endif + static void _stp_dbug (const char *func, int line, const char *fmt, ...) __attribute__ ((format (printf, 3, 4))); static void _stp_error (const char *fmt, ...) __attribute__ ((format (printf, 1, 2))); static void _stp_warn (const char *fmt, ...) __attribute__ ((format (printf, 1, 2))); diff --git a/runtime/string.c b/runtime/string.c index 1d4cb255..afef191a 100644 --- a/runtime/string.c +++ b/runtime/string.c @@ -68,10 +68,10 @@ static void _stp_text_str(char *outstr, char *in, int len, int quoted, int user) { char c, *out = outstr; - if (len == 0 || len > MAXSTRINGLEN-1) + if (len <= 0 || len > MAXSTRINGLEN-1) len = MAXSTRINGLEN-1; if (quoted) { - len -= 2; + len = max(len, 5) - 2; *out++ = '"'; } diff --git a/runtime/vsprintf.c b/runtime/vsprintf.c index 5875d509..3f5f2745 100644 --- a/runtime/vsprintf.c +++ b/runtime/vsprintf.c @@ -269,7 +269,7 @@ static int _stp_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) /* get field width */ field_width = -1; if (isdigit(*fmt_copy)) - field_width = skip_atoi(&fmt_copy); + field_width = clamp(skip_atoi(&fmt_copy), 0, STP_BUFFER_SIZE); else if (*fmt_copy == '*') { ++fmt_copy; /* it's the next argument */ @@ -278,6 +278,7 @@ static int _stp_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) field_width = -field_width; flags |= STP_LEFT; } + field_width = clamp(field_width, 0, STP_BUFFER_SIZE); } /* get the precision */ @@ -291,8 +292,7 @@ static int _stp_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) /* it's the next argument */ precision = va_arg(args_copy, int); } - if (precision < 0) - precision = 0; + precision = clamp(precision, 0, STP_BUFFER_SIZE); } /* get the conversion qualifier */ @@ -511,7 +511,7 @@ static int _stp_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) /* get field width */ field_width = -1; if (isdigit(*fmt)) - field_width = skip_atoi(&fmt); + field_width = clamp(skip_atoi(&fmt), 0, (int)size); else if (*fmt == '*') { ++fmt; /* it's the next argument */ @@ -520,6 +520,7 @@ static int _stp_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) field_width = -field_width; flags |= STP_LEFT; } + field_width = clamp(field_width, 0, (int)size); } /* get the precision */ @@ -533,8 +534,7 @@ static int _stp_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) /* it's the next argument */ precision = va_arg(args, int); } - if (precision < 0) - precision = 0; + precision = clamp(precision, 0, (int)size); } /* get the conversion qualifier */ |