Skip to contentSkip to conversation

trace-cmd: Fixes for four small bugs plus minor cleanup

PATCH Superseded Michael Sartain 2 participants 14 messages Aug 12, 2017 – Oct 9, 2017

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover trace-cmd: Fixes for four small bugs plus minor cleanup

Thanks much.
-Mike

---

Michael Sartain (5):
trace-cmd: Fix incorrect malloc size arg: *item instead of item
trace-cmd: Fix NULL pointer being passed to memcpy
trace-cmd: Add ULL suffix to MISSING_EVENTS since ints shouldn't be
left shifted by 31
trace-cmd: Use unsigned values in Hsieh's trace_hash fast hash
function
trace-cmd: Remove unused view_width variable

 kbuffer-parse.c    | 4 ++--
 trace-dialog.c     | 2 +-
 trace-graph.c      | 2 --
 trace-hash-local.h | 4 ++--
 trace-output.c     | 6 +++++-
 5 files changed, 10 insertions(+), 8 deletions(-)
Signature · 1 line
2.13.2
#1 lore raw

PATCH 1/5 trace-cmd: Fix incorrect malloc size arg: *item instead of item

---
 trace-dialog.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
trace-dialog.c +1 -1
index b5776cc..87e597a 100644
@@ -97,7 +97,7 @@ static void push_cursor(GdkCursor *cursor)
 {
 	struct cursor_stack *item;
 
-	item = malloc_or_die(sizeof(item));
+	item = malloc_or_die(sizeof(*item));
 	item->next = cursor_stack;
 	cursor_stack = item;
 	item->cursor = cursor;
Signature · 1 line
2.13.2
#2 lore raw

PATCH 2/5 trace-cmd: Fix NULL pointer being passed to memcpy

In reply to Michael Sartain
---
 trace-output.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
trace-output.c +5 -1
index bfe6331..84b21b0 100644
@@ -929,7 +929,11 @@ tracecmd_add_option(struct tracecmd_output *handle,
 		free(option);
 		return NULL;
 	}
-	memcpy(option->data, data, size);
+
+	/* Some IDs (like TRACECMD_OPTION_TRACECLOCK) pass NULL data */
+	if (data)
+		memcpy(option->data, data, size);
+
 	list_add_tail(&option->list, &handle->options);
 
 	return option;
Signature · 1 line
2.13.2
#3 lore raw

Re: [PATCH 2/5] trace-cmd: Fix NULL pointer being passed to memcpy

Quoted 10 lines · On Sat, 12 Aug 2017 11:30:44 -0600 Michael Sartain <[email protected]>…
---
 trace-output.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
trace-output.c +0 -0
index bfe6331..84b21b0 100644

trace-output.c tracecmd_add_option(struct tracecmd_output *handle, · line 935

Quoted 8 lines
trace-output.c +4 -1
@@ -929,7 +929,11 @@ tracecmd_add_option(struct tracecmd_output *handle,
 		free(option);
 		return NULL;
 	}
-	memcpy(option->data, data, size);
+
+	/* Some IDs (like TRACECMD_OPTION_TRACECLOCK) pass NULL data */
+	if (data)
+		memcpy(option->data, data, size);

Is this a problem, as when this happens, size should be zero. Does it crash with data=NULL and size=0, or have you seen size not be zero?

-- Steve

Quoted 4 lines
trace-output.c +1 -0
+
 	list_add_tail(&option->list, &handle->options);
 
 	return option;
#4 lore raw

Re: [PATCH 2/5] trace-cmd: Fix NULL pointer being passed to memcpy

Quoted 8 lines · Steven Rostedt
Quoted 5 lines · On Sat, 12 Aug 2017 11:30:44 -0600 Michael Sartain <[email protected]>…
---
 trace-output.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
Quoted 16 lines

trace-output.c tracecmd_add_option(struct tracecmd_output *handle, · line 935

Quoted 13 lines
trace-output.c +4 -1
index bfe6331..84b21b0 100644
@@ -929,7 +929,11 @@ tracecmd_add_option(struct tracecmd_output *handle,
 		free(option);
 		return NULL;
 	}
-	memcpy(option->data, data, size);
+
+	/* Some IDs (like TRACECMD_OPTION_TRACECLOCK) pass NULL data */
+	if (data)
+		memcpy(option->data, data, size);

Is this a problem, as when this happens, size should be zero. Does it crash with data=NULL and size=0, or have you seen size not be zero?

I got an ASAN warning, but you are correct - the size was 0 and it did not crash.

#5 lore raw

Re: [PATCH 2/5] trace-cmd: Fix NULL pointer being passed to memcpy

Quoted 11 lines · On Mon, 9 Oct 2017 16:27:10 -0600 Michael Sartain <[email protected]> wrote:
Quoted 8 lines

trace-output.c tracecmd_add_option(struct tracecmd_output *handle, · line 935

Quoted 5 lines
trace-output.c +4 -1
-	memcpy(option->data, data, size);
+
+	/* Some IDs (like TRACECMD_OPTION_TRACECLOCK) pass NULL data */
+	if (data)
+		memcpy(option->data, data, size);  

Is this a problem, as when this happens, size should be zero. Does it crash with data=NULL and size=0, or have you seen size not be zero?

I got an ASAN warning, but you are correct - the size was 0 and it did not crash.

OK, but it's almost like dividing zero from zero. Can you send another patch, but this time check if (size) instead of if (data).

-- Steve

#6 lore raw

PATCH 3/5 trace-cmd: Add ULL suffix to MISSING_EVENTS since ints shouldn't be left shifted by 31

In reply to Michael Sartain
---
 kbuffer-parse.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kbuffer-parse.c +2 -2
index 4e6e95e..dde642c 100644
@@ -24,8 +24,8 @@
 
 #include "kbuffer.h"
 
-#define MISSING_EVENTS (1 << 31)
-#define MISSING_STORED (1 << 30)
+#define MISSING_EVENTS (1ULL << 31)
+#define MISSING_STORED (1ULL << 30)
 
 #define COMMIT_MASK ((1 << 27) - 1)
 
Signature · 1 line
2.13.2
#7 lore raw

Re: [PATCH 3/5] trace-cmd: Add ULL suffix to MISSING_EVENTS since ints shouldn't be left shifted by 31

Quoted 10 lines · On Sat, 12 Aug 2017 11:30:45 -0600 Michael Sartain <[email protected]>…
---
 kbuffer-parse.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kbuffer-parse.c +0 -0
index 4e6e95e..dde642c 100644

kbuffer-parse.c line 27

Quoted 4 lines
kbuffer-parse.c +0 -1
@@ -24,8 +24,8 @@
 
 #include "kbuffer.h"
 
-#define MISSING_EVENTS (1 << 31)

Actually, why not? This could also be just UL, because it's fine to shift 31, that would give us: 0x80000000 And that bit is all we care for.

-- Steve

Quoted 6 lines
kbuffer-parse.c +2 -1
-#define MISSING_STORED (1 << 30)
+#define MISSING_EVENTS (1ULL << 31)
+#define MISSING_STORED (1ULL << 30)
 
 #define COMMIT_MASK ((1 << 27) - 1)
 
#8 lore raw

PATCH 4/5 trace-cmd: Use unsigned values in Hsieh's trace_hash fast hash function

In reply to Michael Sartain

Signed int values were being used where the original code used uint32_t types:

http://www.azillionmonkeys.com/qed/hash.html

Right shifting negative int values has implementation-defined and left shifting has undefined behavior.

On my platform (x86_64) right shifting was doing sign extension and filling high bits with 1s, which is different than the original algorithm.

---
 trace-hash-local.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
trace-hash-local.h +2 -2
index b2a1002..b3f9b06 100644
@@ -22,7 +22,7 @@
 
 static inline unsigned int trace_hash(int val)
 {
-	int hash, tmp;
+	unsigned int hash, tmp;
 
 	hash = 12546869;	/* random prime */
 
@@ -34,7 +34,7 @@ static inline unsigned int trace_hash(int val)
 	 */
 
 	hash +=	(val & 0xffff);
-	tmp = (val >> 16) ^ hash;
+	tmp = ((unsigned int)val >> 16) ^ hash;
 	hash = (hash << 16) ^ tmp;
 	hash += hash >> 11;
 
Signature · 1 line
2.13.2
#9 lore raw

Re: [PATCH 4/5] trace-cmd: Use unsigned values in Hsieh's trace_hash fast hash function

Quoted 10 lines · On Sat, 12 Aug 2017 11:30:46 -0600 Michael Sartain <[email protected]>…

Signed int values were being used where the original code used uint32_t types:

http://www.azillionmonkeys.com/qed/hash.html

Right shifting negative int values has implementation-defined and left shifting has undefined behavior.

On my platform (x86_64) right shifting was doing sign extension and filling high bits with 1s, which is different than the original algorithm.

Heh, nice catch. Although the hash was never used for anything too important. Mostly just colors of the graph.

Quoted 19 lines
---
 trace-hash-local.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
trace-hash-local.h +1 -1
index b2a1002..b3f9b06 100644
@@ -22,7 +22,7 @@
 
 static inline unsigned int trace_hash(int val)
 {
-	int hash, tmp;
+	unsigned int hash, tmp;
 
 	hash = 12546869;	/* random prime */
 

trace-hash-local.h static inline unsigned int trace_hash(int val) · line 37

Quoted 5 lines
trace-hash-local.h +1 -1
@@ -34,7 +34,7 @@ static inline unsigned int trace_hash(int val)
 	 */
 
 	hash +=	(val & 0xffff);
-	tmp = (val >> 16) ^ hash;
+	tmp = ((unsigned int)val >> 16) ^ hash;

Why not just have val be unsigned int, and not do the typecast?

-- Steve

Quoted 3 lines
trace-hash-local.h +0 -0
 	hash = (hash << 16) ^ tmp;
 	hash += hash >> 11;
 
#10 lore raw

PATCH 5/5 trace-cmd: Remove unused view_width variable

In reply to Michael Sartain
---
 trace-graph.c | 2 --
 1 file changed, 2 deletions(-)
trace-graph.c +0 -2
index 1db342f..2c49549 100644
@@ -1263,7 +1263,6 @@ static void draw_info_box(struct graph_info *ginfo, const gchar *buffer,
 	gint width, height;
 	GdkPixmap *pix;
 	static GdkGC *pix_bg;
-	gint view_width;
 	gint view_start;
 
 	if (!pix_bg) {
@@ -1284,7 +1283,6 @@ static void draw_info_box(struct graph_info *ginfo, const gchar *buffer,
 	height += PLOT_BOARDER * 2;
 
 	view_start = gtk_adjustment_get_value(ginfo->hadj);
-	view_width = gtk_adjustment_get_page_size(ginfo->hadj);
 	if (x > view_start + width)
 		x -= width;
 
Signature · 1 line
2.13.2
#11 lore raw

Re: [PATCH 5/5] trace-cmd: Remove unused view_width variable

On Sat, 12 Aug 2017 11:30:47 -0600
Michael Sartain <[email protected]> wrote:

This is an annoying warning, but I've kept from doing this because it reminds me that I have an idea to actually use that variable someday. But I've just been procrastinating on doing that. ;-)

-- Steve

Quoted 25 lines
---
 trace-graph.c | 2 --
 1 file changed, 2 deletions(-)
trace-graph.c +0 -2
index 1db342f..2c49549 100644
@@ -1263,7 +1263,6 @@ static void draw_info_box(struct graph_info *ginfo, const gchar *buffer,
 	gint width, height;
 	GdkPixmap *pix;
 	static GdkGC *pix_bg;
-	gint view_width;
 	gint view_start;
 
 	if (!pix_bg) {
@@ -1284,7 +1283,6 @@ static void draw_info_box(struct graph_info *ginfo, const gchar *buffer,
 	height += PLOT_BOARDER * 2;
 
 	view_start = gtk_adjustment_get_value(ginfo->hadj);
-	view_width = gtk_adjustment_get_page_size(ginfo->hadj);
 	if (x > view_start + width)
 		x -= width;
 
#12 lore raw
In reply to Michael Sartain

Ping? Were there any concerns with these or things needed to be done before they could be merged?

Thanks.

Quoted 24 lines · Michael Sartain

Thanks much.
-Mike

---

Michael Sartain (5):
trace-cmd: Fix incorrect malloc size arg: *item instead of item
trace-cmd: Fix NULL pointer being passed to memcpy
trace-cmd: Add ULL suffix to MISSING_EVENTS since ints shouldn't be
left shifted by 31
trace-cmd: Use unsigned values in Hsieh's trace_hash fast hash
function
trace-cmd: Remove unused view_width variable

 kbuffer-parse.c    | 4 ++--
 trace-dialog.c     | 2 +-
 trace-graph.c      | 2 --
 trace-hash-local.h | 4 ++--
 trace-output.c     | 6 +++++-
 5 files changed, 10 insertions(+), 8 deletions(-)
Signature · 1 line
2.13.2
#13 lore raw
Quoted 3 lines · On Mon, 9 Oct 2017 16:13:42 -0600 Michael Sartain <[email protected]> wrote:

Ping? Were there any concerns with these or things needed to be done before they could be merged?

Thanks for the ping, this fell back in the todo list of my emails.

I'll look at it this week.

-- Steve