diff mbox series

putmem: Fix Floating point exception issue

Message ID 20180705144115.19872-1-hegdevasant@linux.vnet.ibm.com
State Accepted
Headers show
Series putmem: Fix Floating point exception issue | expand

Commit Message

Vasant Hegde July 5, 2018, 2:41 p.m. UTC
Validate read() before calling __adu_putmem().
Also check end size in progress_tick().

Signed-off-by: Vasant Hegde <hegdevasant@linux.vnet.ibm.com>
---
 src/mem.c      | 3 +++
 src/progress.c | 3 +++
 2 files changed, 6 insertions(+)

Comments

Alistair Popple July 12, 2018, 5:35 a.m. UTC | #1
Thanks Vasant,

On Thursday, 5 July 2018 8:11:15 PM AEST Vasant Hegde wrote:
> Validate read() before calling __adu_putmem().
> Also check end size in progress_tick().

These should really be split into two patches. I have taken the first hunk of
this patch which validates the read and prevents the floating point exception.
There are a few comments on the second hunk which really needs to be submitted
as a seperate patch. Thanks!

> diff --git a/src/progress.c b/src/progress.c
> index fe443b9..e57b8de 100644
> --- a/src/progress.c
> +++ b/src/progress.c
> @@ -51,6 +51,9 @@ void progress_tick(uint64_t cur, uint64_t end)
>  	uint64_t pcent;
>  	double sec;
>  
> +	if (end == 0)
> +		return;

IMHO calling progress_tick() with cur > end or end == 0 is a programming error
and not something which should just be ignored. We shouldn't segfault either
though so we should add an assert statement to validate the arguments.

eg:
	assert(cur <= end && end);

Note that this will also require a fix in __adu_putmem() where we call
pdbg_progress_tick(size, size);

- Alistair

>  	pcent = (cur * 100) / end;
>  	if (progress_pcent == pcent && cur < progress_n_upd &&
>  	    cur < end)
>
diff mbox series

Patch

diff --git a/src/mem.c b/src/mem.c
index b2ab917..4b2c829 100644
--- a/src/mem.c
+++ b/src/mem.c
@@ -90,6 +90,9 @@  static int putmem(uint64_t addr, struct mem_flags flags)
 	progress_init();
 	do {
 		read_size = read(STDIN_FILENO, buf, PUTMEM_BUF_SIZE);
+		if (read_size <= 0)
+			break;
+
 		if (__adu_putmem(adu_target, addr, buf, read_size, flags.ci)) {
 			rc = 0;
 			printf("Unable to write memory.\n");
diff --git a/src/progress.c b/src/progress.c
index fe443b9..e57b8de 100644
--- a/src/progress.c
+++ b/src/progress.c
@@ -51,6 +51,9 @@  void progress_tick(uint64_t cur, uint64_t end)
 	uint64_t pcent;
 	double sec;
 
+	if (end == 0)
+		return;
+
 	pcent = (cur * 100) / end;
 	if (progress_pcent == pcent && cur < progress_n_upd &&
 	    cur < end)