linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michael Sartain <mikesart@fastmail.com>
To: Steven Rostedt <rostedt@goodmis.org>
Cc: Michael Sartain <mikesart@fastmail.com>, linux-kernel@vger.kernel.org
Subject: [PATCH v3 2/6] Fix unsigned return values being error checked as negative
Date: Wed, 14 Jun 2017 18:27:57 -0600	[thread overview]
Message-ID: <370b3b40b5a1177a6f420013abd8196ef3f51034.1497486273.git.mikesart@fastmail.com> (raw)
In-Reply-To: <cover.1497486273.git.mikesart@fastmail.com>
In-Reply-To: <cover.1497486273.git.mikesart@fastmail.com>

Functions like read4 and read8 had unsigned return types but callers were
checking for those return values being less than 0 for errors.

This patch changes those functions to return signed int error values and take a
pointer to a size parameter. Also changes several locals to match the function
types.

Signed-off-by: Michael Sartain <mikesart@fastmail.com>
---
 trace-input.c | 179 ++++++++++++++++++++++++----------------------------------
 trace-msg.c   |   5 +-
 2 files changed, 78 insertions(+), 106 deletions(-)

diff --git a/trace-input.c b/trace-input.c
index e676c85..89ddcf5 100644
--- a/trace-input.c
+++ b/trace-input.c
@@ -196,10 +196,10 @@ static const char *show_records(struct list_head *pages)
 
 static int init_cpu(struct tracecmd_input *handle, int cpu);
 
-static int do_read(struct tracecmd_input *handle, void *data, int size)
+static ssize_t do_read(struct tracecmd_input *handle, void *data, size_t size)
 {
-	int tot = 0;
-	int r;
+	ssize_t tot = 0;
+	ssize_t r;
 
 	do {
 		r = read(handle->fd, data, size - tot);
@@ -214,10 +214,10 @@ static int do_read(struct tracecmd_input *handle, void *data, int size)
 	return tot;
 }
 
-static int
-do_read_check(struct tracecmd_input *handle, void *data, int size)
+static ssize_t
+do_read_check(struct tracecmd_input *handle, void *data, size_t size)
 {
-	int ret;
+	ssize_t ret;
 
 	ret = do_read(handle, data, size);
 	if (ret < 0)
@@ -232,9 +232,9 @@ static char *read_string(struct tracecmd_input *handle)
 {
 	char buf[BUFSIZ];
 	char *str = NULL;
-	int size = 0;
-	int i;
-	int r;
+	size_t size = 0;
+	ssize_t i;
+	ssize_t r;
 
 	for (;;) {
 		r = do_read(handle, buf, BUFSIZ);
@@ -294,7 +294,7 @@ static char *read_string(struct tracecmd_input *handle)
 	return NULL;
 }
 
-static unsigned int read4(struct tracecmd_input *handle)
+static int read4(struct tracecmd_input *handle, unsigned int *size)
 {
 	struct pevent *pevent = handle->pevent;
 	unsigned int data;
@@ -302,10 +302,11 @@ static unsigned int read4(struct tracecmd_input *handle)
 	if (do_read_check(handle, &data, 4))
 		return -1;
 
-	return __data2host4(pevent, data);
+	*size = __data2host4(pevent, data);
+	return 0;
 }
 
-static unsigned long long read8(struct tracecmd_input *handle)
+static int read8(struct tracecmd_input *handle, unsigned long long *size)
 {
 	struct pevent *pevent = handle->pevent;
 	unsigned long long data;
@@ -313,13 +314,14 @@ static unsigned long long read8(struct tracecmd_input *handle)
 	if (do_read_check(handle, &data, 8))
 		return -1;
 
-	return __data2host8(pevent, data);
+	*size = __data2host8(pevent, data);
+	return 0;
 }
 
 static int read_header_files(struct tracecmd_input *handle)
 {
 	struct pevent *pevent = handle->pevent;
-	long long size;
+	unsigned long long size;
 	char *header;
 	char buf[BUFSIZ];
 
@@ -329,8 +331,7 @@ static int read_header_files(struct tracecmd_input *handle)
 	if (memcmp(buf, "header_page", 12) != 0)
 		return -1;
 
-	size = read8(handle);
-	if (size < 0)
+	if (read8(handle, &size) < 0)
 		return -1;
 
 	header = malloc(size);
@@ -355,8 +356,7 @@ static int read_header_files(struct tracecmd_input *handle)
 	if (memcmp(buf, "header_event", 13) != 0)
 		return -1;
 
-	size = read8(handle);
-	if (size < 0)
+	if (read8(handle, &size) < 0)
 		return -1;
 
 	header = malloc(size);
@@ -521,11 +521,10 @@ static int read_ftrace_files(struct tracecmd_input *handle, const char *regex)
 	regex_t epreg;
 	regex_t *sreg = NULL;
 	regex_t *ereg = NULL;
+	unsigned int count, i;
 	int print_all = 0;
 	int unique;
-	int count;
 	int ret;
-	int i;
 
 	if (regex) {
 		sreg = &spreg;
@@ -554,13 +553,11 @@ static int read_ftrace_files(struct tracecmd_input *handle, const char *regex)
 		}
 	}
 
-	count = read4(handle);
-	if (count < 0)
+	if (read4(handle, &count) < 0)
 		return -1;
 
 	for (i = 0; i < count; i++) {
-		size = read8(handle);
-		if (size < 0)
+		if (read8(handle, &size) < 0)
 			return -1;
 		ret = read_ftrace_file(handle, size, print_all, ereg);
 		if (ret < 0)
@@ -587,13 +584,13 @@ static int read_event_files(struct tracecmd_input *handle, const char *regex)
 	regex_t *sreg = NULL;
 	regex_t *ereg = NULL;
 	regex_t *reg;
-	int systems;
+	unsigned int systems;
+	unsigned int count;
+	unsigned int i, x;
 	int print_all;
 	int sys_printed;
-	int count;
 	int unique;
 	int ret;
-	int i,x;
 
 	if (regex) {
 		sreg = &spreg;
@@ -603,8 +600,7 @@ static int read_event_files(struct tracecmd_input *handle, const char *regex)
 			return -1;
 	}
 
-	systems = read4(handle);
-	if (systems < 0)
+	if (read4(handle, &systems) < 0)
 		return -1;
 
 	for (i = 0; i < systems; i++) {
@@ -637,13 +633,11 @@ static int read_event_files(struct tracecmd_input *handle, const char *regex)
 			}
 		}
 
-		count = read4(handle);
-		if (count < 0)
+		if (read4(handle, &count) < 0)
 			goto failed;
 
 		for (x=0; x < count; x++) {
-			size = read8(handle);
-			if (size < 0)
+			if (read8(handle, &size) < 0)
 				goto failed;
 
 			ret = read_event_file(handle, system, size,
@@ -675,16 +669,14 @@ static int read_event_files(struct tracecmd_input *handle, const char *regex)
 static int read_proc_kallsyms(struct tracecmd_input *handle)
 {
 	struct pevent *pevent = handle->pevent;
-	int size;
+	unsigned int size;
 	char *buf;
 
-	size = read4(handle);
+	if (read4(handle, &size) < 0)
+		return -1;
 	if (!size)
 		return 0; /* OK? */
 
-	if (size < 0)
-		return -1;
-
 	buf = malloc(size+1);
 	if (!buf)
 		return -1;
@@ -702,16 +694,14 @@ static int read_proc_kallsyms(struct tracecmd_input *handle)
 
 static int read_ftrace_printk(struct tracecmd_input *handle)
 {
-	int size;
+	unsigned int size;
 	char *buf;
 
-	size = read4(handle);
+	if (read4(handle, &size) < 0)
+		return -1;
 	if (!size)
 		return 0; /* OK? */
 
-	if (size < 0)
-		return -1;
-
 	buf = malloc(size + 1);
 	if (!buf)
 		return -1;
@@ -2268,8 +2258,8 @@ static int read_cpu_data(struct tracecmd_input *handle)
 		if (pevent->old_format)
 			kbuffer_set_old_format(handle->cpu_data[cpu].kbuf);
 
-		offset = read8(handle);
-		size = read8(handle);
+		read8(handle, &offset);
+		read8(handle, &size);
 
 		handle->cpu_data[cpu].file_offset = offset;
 		handle->cpu_data[cpu].file_size = size;
@@ -2315,8 +2305,7 @@ static int read_cpu_data(struct tracecmd_input *handle)
 static int read_data_and_size(struct tracecmd_input *handle,
 				     char **data, unsigned long long *size)
 {
-	*size = read8(handle);
-	if (*size < 0)
+	if (read8(handle, size) < 0)
 		return -1;
 	*data = malloc(*size + 1);
 	if (!*data)
@@ -2367,11 +2356,12 @@ static int read_and_parse_trace_clock(struct tracecmd_input *handle,
 int tracecmd_init_data(struct tracecmd_input *handle)
 {
 	struct pevent *pevent = handle->pevent;
+    unsigned int cpus;
 	int ret;
 
-	handle->cpus = read4(handle);
-	if (handle->cpus < 0)
+	if (read4(handle, &cpus) < 0)
 		return -1;
+	handle->cpus = cpus;
 
 	pevent_set_cpus(pevent, handle->cpus);
 
@@ -2570,6 +2560,7 @@ struct tracecmd_input *tracecmd_alloc_fd(int fd)
 {
 	struct tracecmd_input *handle;
 	char test[] = { 23, 8, 68 };
+    unsigned int page_size;
 	char *version;
 	char buf[BUFSIZ];
 
@@ -2616,7 +2607,8 @@ struct tracecmd_input *tracecmd_alloc_fd(int fd)
 	do_read_check(handle, buf, 1);
 	handle->long_size = buf[0];
 
-	handle->page_size = read4(handle);
+	read4(handle, &page_size);
+	handle->page_size = page_size;
 
 	handle->header_files_start =
 		lseek64(handle->fd, 0, SEEK_CUR);
@@ -2772,36 +2764,30 @@ void tracecmd_close(struct tracecmd_input *handle)
 	free(handle);
 }
 
-static long long read_copy_size8(struct tracecmd_input *handle, int fd)
+static int read_copy_size8(struct tracecmd_input *handle, int fd, unsigned long long *size)
 {
-	long long size;
-
 	/* read size */
-	if (do_read_check(handle, &size, 8))
+	if (do_read_check(handle, size, 8))
 		return -1;
 
-	if (__do_write_check(fd, &size, 8))
+	if (__do_write_check(fd, size, 8))
 		return -1;
 
-	size = __data2host8(handle->pevent, size);
-
-	return size;
+	*size = __data2host8(handle->pevent, *size);
+	return 0;
 }
 
-static int read_copy_size4(struct tracecmd_input *handle, int fd)
+static int read_copy_size4(struct tracecmd_input *handle, int fd, unsigned int *size)
 {
-	int size;
-
 	/* read size */
-	if (do_read_check(handle, &size, 4))
+	if (do_read_check(handle, size, 4))
 		return -1;
 
-	if (__do_write_check(fd, &size, 4))
+	if (__do_write_check(fd, size, 4))
 		return -1;
 
-	size = __data2host4(handle->pevent, size);
-
-	return size;
+	*size = __data2host4(handle->pevent, *size);
+	return 0;
 }
 
 static int read_copy_data(struct tracecmd_input *handle,
@@ -2829,7 +2815,7 @@ static int read_copy_data(struct tracecmd_input *handle,
 
 static int copy_header_files(struct tracecmd_input *handle, int fd)
 {
-	long long size;
+	unsigned long long size;
 
 	lseek64(handle->fd, handle->header_files_start, SEEK_SET);
 
@@ -2837,8 +2823,7 @@ static int copy_header_files(struct tracecmd_input *handle, int fd)
 	if (read_copy_data(handle, 12, fd) < 0)
 		return -1;
 
-	size = read_copy_size8(handle, fd);
-	if (size < 0)
+	if (read_copy_size8(handle, fd, &size) < 0)
 		return -1;
 
 	if (read_copy_data(handle, size, fd) < 0)
@@ -2848,8 +2833,7 @@ static int copy_header_files(struct tracecmd_input *handle, int fd)
 	if (read_copy_data(handle, 13, fd) < 0)
 		return -1;
 
-	size = read_copy_size8(handle, fd);
-	if (size < 0)
+	if (read_copy_size8(handle, fd, &size) < 0)
 		return -1;
 
 	if (read_copy_data(handle, size, fd) < 0)
@@ -2861,17 +2845,15 @@ static int copy_header_files(struct tracecmd_input *handle, int fd)
 static int copy_ftrace_files(struct tracecmd_input *handle, int fd)
 {
 	unsigned long long size;
-	int count;
-	int i;
+	unsigned int count;
+	unsigned int i;
 
-	count = read_copy_size4(handle, fd);
-	if (count < 0)
+	if (read_copy_size4(handle, fd, &count) < 0)
 		return -1;
 
 	for (i = 0; i < count; i++) {
 
-		size = read_copy_size8(handle, fd);
-		if (size < 0)
+		if (read_copy_size8(handle, fd, &size) < 0)
 			return -1;
 
 		if (read_copy_data(handle, size, fd) < 0)
@@ -2885,13 +2867,11 @@ static int copy_event_files(struct tracecmd_input *handle, int fd)
 {
 	unsigned long long size;
 	char *system;
-	int systems;
-	int count;
-	int ret;
-	int i,x;
+	unsigned int systems;
+	unsigned int count;
+	unsigned int i,x;
 
-	systems = read_copy_size4(handle, fd);
-	if (systems < 0)
+	if (read_copy_size4(handle, fd, &systems) < 0)
 		return -1;
 
 	for (i = 0; i < systems; i++) {
@@ -2904,17 +2884,14 @@ static int copy_event_files(struct tracecmd_input *handle, int fd)
 		}
 		free(system);
 
-		count = read_copy_size4(handle, fd);
-		if (count < 0)
+		if (read_copy_size4(handle, fd, &count) < 0)
 			return -1;
 
 		for (x=0; x < count; x++) {
-			size = read_copy_size8(handle, fd);
-			if (size < 0)
+			if (read_copy_size8(handle, fd, &size) < 0)
 				return -1;
 
-			ret = read_copy_data(handle, size, fd);
-			if (ret < 0)
+			if (read_copy_data(handle, size, fd) < 0)
 				return -1;
 		}
 	}
@@ -2924,15 +2901,13 @@ static int copy_event_files(struct tracecmd_input *handle, int fd)
 
 static int copy_proc_kallsyms(struct tracecmd_input *handle, int fd)
 {
-	int size;
+	unsigned int size;
 
-	size = read_copy_size4(handle, fd);
+	if (read_copy_size4(handle, fd, &size) < 0)
+		return -1;
 	if (!size)
 		return 0; /* OK? */
 
-	if (size < 0)
-		return -1;
-
 	if (read_copy_data(handle, size, fd) < 0)
 		return -1;
 
@@ -2941,15 +2916,13 @@ static int copy_proc_kallsyms(struct tracecmd_input *handle, int fd)
 
 static int copy_ftrace_printk(struct tracecmd_input *handle, int fd)
 {
-	int size;
+	unsigned int size;
 
-	size = read_copy_size4(handle, fd);
+	if (read_copy_size4(handle, fd, &size) < 0)
+		return -1;
 	if (!size)
 		return 0; /* OK? */
 
-	if (size < 0)
-		return -1;
-
 	if (read_copy_data(handle, size, fd) < 0)
 		return -1;
 
@@ -2958,15 +2931,13 @@ static int copy_ftrace_printk(struct tracecmd_input *handle, int fd)
 
 static int copy_command_lines(struct tracecmd_input *handle, int fd)
 {
-	unsigned long size;
+	unsigned long long size;
 
-	size = read_copy_size8(handle, fd);
+	if (read_copy_size8(handle, fd, &size) < 0)
+		return -1;
 	if (!size)
 		return 0; /* OK? */
 
-	if (size < 0)
-		return -1;
-
 	if (read_copy_data(handle, size, fd) < 0)
 		return -1;
 
diff --git a/trace-msg.c b/trace-msg.c
index f1b6546..3991985 100644
--- a/trace-msg.c
+++ b/trace-msg.c
@@ -288,7 +288,7 @@ static int tracecmd_msg_send(int fd, u32 cmd)
 
 static int msg_read(int fd, void *buf, u32 size, int *n)
 {
-	int r;
+	ssize_t r;
 
 	while (size) {
 		r = read(fd, buf + *n, size);
@@ -637,7 +637,8 @@ int tracecmd_msg_finish_sending_metadata(int fd)
 int tracecmd_msg_collect_metadata(int ifd, int ofd)
 {
 	struct tracecmd_msg msg;
-	u32 s, t, n, cmd;
+	u32 t, n, cmd;
+	ssize_t s;
 	int ret;
 
 	do {
-- 
2.11.0

  parent reply	other threads:[~2017-06-15  0:28 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-15  0:27 [PATCH v3 0/6] trace-cmd: escape sequence, EINTR, error checking bug fixes Michael Sartain
2017-06-15  0:27 ` [PATCH v3 1/6] Fix bad force_token escape sequence Michael Sartain
2018-01-17 16:28   ` [tip:perf/core] tools lib traceevent: " tip-bot for Michael Sartain
2017-06-15  0:27 ` Michael Sartain [this message]
2017-06-15  0:27 ` [PATCH v3 3/6] Handle EINTR signal interrupts for read, write, open calls Michael Sartain
2017-06-21 13:26   ` Steven Rostedt
2017-06-15  0:27 ` [PATCH v3 4/6] Fix read / write data offsets in read / write loops Michael Sartain
2017-06-21 13:29   ` Steven Rostedt
2017-06-21 17:36     ` Michael Sartain
2017-06-15  0:28 ` [PATCH v3 5/6] Fix function prototypes for __vwarning, __vpr_stat, and __vdie Michael Sartain
2017-06-15  0:28 ` [PATCH v3 6/6] Fix cases where string literals were passed as string format args Michael Sartain
2017-08-02 22:15 [PATCH V3 1/2] use direname instead of custom code Federico Vaga
2017-08-02 22:15 ` [PATCH V3 2/2] It makes the code clearer and less error prone Federico Vaga
2017-08-14 17:33   ` Steven Rostedt
2017-08-15  7:25     ` Federico Vaga
2017-11-08 18:50       ` Steven Rostedt
2017-08-02 23:48 ` [PATCH V3 1/2] trace-cmd record: use direname instead of custom code Steven Rostedt
2017-08-14 17:14 ` [PATCH V3 1/2] " Steven Rostedt
2017-09-28 20:13 [PATCH][trace-cmd] Print value of unknown symbolic fields Jan Kiszka
2017-10-03 22:46 ` Steven Rostedt
2017-10-03 23:12 ` Steven Rostedt
2017-10-16 16:55 [PATCH v2 0/4] trace-cmd: Fixes for four minor bugs Michael Sartain
2017-10-16 16:55 ` [PATCH v2 1/4] trace-cmd: Fix incorrect malloc size arg: *item instead of item Michael Sartain
2017-10-16 16:55 ` [PATCH v2 2/4] trace-cmd: Fix NULL pointer being passed to memcpy Michael Sartain
2017-10-16 16:55 ` [PATCH v2 3/4] trace-cmd: Add UL suffix to MISSING_EVENTS since ints shouldn't be left shifted by 31 Michael Sartain
2017-10-16 16:55 ` [PATCH v2 4/4] trace-cmd: Use unsigned values in Hsieh's trace_hash fast hash function Michael Sartain
2018-01-12  0:47 [PATCH 00/10] tools lib traceveent: Pull updates from trace-cmd library Steven Rostedt
2018-01-12  0:47 ` [PATCH 01/10] lib, traceevent: Fix bad force_token escape sequence Steven Rostedt
2018-01-12  0:47 ` [PATCH 02/10] lib traceevent: Show value of flags that have not been parsed Steven Rostedt
2018-01-17 16:28   ` [tip:perf/core] tools " tip-bot for Steven Rostedt (VMware)
2018-01-12  0:47 ` [PATCH 03/10] lib traceevent: Print value of unknown symbolic fields Steven Rostedt
2018-01-17 16:28   ` [tip:perf/core] tools " tip-bot for Jan Kiszka
2018-01-12  0:47 ` [PATCH 04/10] lib traceevent: Simplify pointer print logic and fix %pF Steven Rostedt
2018-01-17 16:29   ` [tip:perf/core] tools " tip-bot for Steven Rostedt (VMware)
2018-01-12  0:47 ` [PATCH 05/10] lib traceevent: Handle new pointer processing of bprint strings Steven Rostedt
2018-01-17 16:29   ` [tip:perf/core] tools " tip-bot for Steven Rostedt (VMware)
2018-01-12  0:47 ` [PATCH 06/10] lib traceevent: Show contents (in hex) of data of unrecognized type records Steven Rostedt
2018-01-17 16:30   ` [tip:perf/core] tools " tip-bot for Steven Rostedt (VMware)
2018-01-12  0:47 ` [PATCH 07/10] lib traceevent: Use asprintf when possible Steven Rostedt
2018-01-17 16:30   ` [tip:perf/core] tools " tip-bot for Federico Vaga
2018-01-12  0:47 ` [PATCH 08/10] lib traceevent: Add UL suffix to MISSING_EVENTS Steven Rostedt
2018-01-17 16:31   ` [tip:perf/core] tools " tip-bot for Michael Sartain
2018-01-12  0:47 ` [PATCH 09/10] lib traceeevent: Fix missing break in FALSE case of pevent_filter_clear_trivial() Steven Rostedt
2017-04-26 14:55   ` [PATCH] parse-events: Fix the FALSE case in pevent_filter_clear_trivial() Taeung Song
2018-01-17 16:31     ` [tip:perf/core] tools lib traceevent: Fix missing break in FALSE case of pevent_filter_clear_trivial() tip-bot for Taeung Song
2018-01-12  1:00   ` [PATCH 09/10] lib traceeevent: " Taeung Song
2018-01-12  1:14     ` Steven Rostedt
2018-01-12 16:02       ` Arnaldo Carvalho de Melo
2018-01-12  0:47 ` [PATCH 10/10] lib traceevent: Fix get_field_str() for dynamic strings Steven Rostedt
2018-01-17 16:31   ` [tip:perf/core] tools " tip-bot for Steven Rostedt (VMware)
2018-01-17  6:02 ` [PATCH 00/10] tools lib traceveent: Pull updates from trace-cmd library Namhyung Kim

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=370b3b40b5a1177a6f420013abd8196ef3f51034.1497486273.git.mikesart@fastmail.com \
    --to=mikesart@fastmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).