[PATCH] Add a check to ensure event codes in events files fit in an integer

Maynard Johnson <[email protected]>
Newsgroups gmane.linux.oprofile
Message-ID <[email protected]>
This patch has already been pushed upstream.

-----------------------------------------------------------------


Add a check to ensure event codes in events files fit in an integer

The read_events() function in libop/op_events.c is enhanced to
validate that the hex codes found in events files are not too big
to fit in an integer, since the rest of oprofile assumes hex codes
are integers.  This check will execute whenever ocount or operf is
run, as well as when doing 'make check' or 'make distcheck'.

Signed-off-by: Maynard Johnson <[email protected]>
---
 libop/op_events.c |   17 ++++++++++++++++-
 1 files changed, 16 insertions(+), 1 deletions(-)

diff --git a/libop/op_events.c b/libop/op_events.c
index 29dc2f3..99266c6 100644
--- a/libop/op_events.c
+++ b/libop/op_events.c
@@ -498,6 +498,7 @@ static void read_events(char const * file)
 	int seen_event, seen_counters, seen_um, seen_minimum, seen_name, seen_ext;
 	FILE * fp = fopen(file, "r");
 	int tags;
+	int fail = 0;
 
 	if (!fp) {
 		fprintf(stderr, "oprofile: could not open event description file %s\n", file);
@@ -510,6 +511,8 @@ static void read_events(char const * file)
 	line = op_get_line(fp);
 
 	while (line) {
+		int bad_val = 0;
+		u64 tmp_val = 0ULL;
 		if (empty_line(line) || comment_line(line))
 			goto next;
 
@@ -535,11 +538,21 @@ static void read_events(char const * file)
 				if (strchr(value, '.') != NULL)
 					parse_error("invalid event name");
 				event->name = value;
+				if (bad_val) {
+					fprintf(stderr, "Event %s event code (0x%llx) is too big to fit in an int\n",
+					        event->name, tmp_val);
+					fail = 1;
+					bad_val = 0;
+				}
 			} else if (strcmp(name, "event") == 0) {
 				if (seen_event)
 					parse_error("duplicate event: tag");
 				seen_event = 1;
-				event->val = parse_hex(value);
+				tmp_val = parse_long_hex(value);
+				if (tmp_val > 0xffffffff)
+					bad_val = 1;
+				else
+					event->val = (u32)tmp_val;
 				free(value);
 			} else if (strcmp(name, "counters") == 0) {
 				if (seen_counters)
@@ -599,6 +612,8 @@ next:
 	}
 
 	fclose(fp);
+	if (fail)
+		exit(EXIT_FAILURE);
 }
 
 
-- 
1.6.2.rc2


------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.