threads / patch / 26661

patchtransport-helper.c: fix check for size_t < 0

Subject: [PATCH] transport-helper.c: fix check for size_t < 0

## tl;dr

3 messages between Mar 4, 2011 and Mar 4, 2011. Diffs are folded; open one to read it.

replies: 2people: 2as markdown or json

Nicolas Kaiser· Mar 4, 2011, 19:28 UTC · lore

'bytes' is unsigned of type size_t, and can't be negative. But the assigned write() returns ssize_t, and -1 on error. For testing < 0, 'bytes' needs to be of a signed type.

Signed-off-by: Nicolas Kaiser <nikai@nikai.net>
---
Testsuite did not regress at my place.
 transport-helper.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
Show changes to transport-helper.c +2 −2
diff --git a/transport-helper.c b/transport-helper.c
index 4e4754c..710b6f1 100644
--- a/transport-helper.c
+++ b/transport-helper.c
@@ -973,7 +973,7 @@ static int udt_do_read(struct unidirectional_transfer *t)
  */
 static int udt_do_write(struct unidirectional_transfer *t)
 {
-	size_t bytes;
+	int bytes;
 
 	if (t->bufuse == 0)
 		return 0;	/* Nothing to write. */
@@ -989,7 +989,7 @@ static int udt_do_write(struct unidirectional_transfer *t)
 		if (t->bufuse)
 			memmove(t->buf, t->buf + bytes, t->bufuse);
 		transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
-			(int)bytes, t->dest_name, (int)t->bufuse);
+			bytes, t->dest_name, (int)t->bufuse);
 	}
 	return 0;
 }
-- 
1.7.3.4
Piotr Krukowiecki· Mar 4, 2011, 20:20 UTC · re: Nicolas Kaiser · lore

Re: [PATCH] transport-helper.c: fix check for size_t < 0

On Fri, Mar 4, 2011 at 8:28 PM, Nicolas Kaiser <nikai@nikai.net> wrote:
> 'bytes' is unsigned of type size_t, and can't be negative.
> But the assigned write() returns ssize_t, and -1 on error.
> For testing < 0, 'bytes' needs to be of a signed type.

You are right that, but the fix should be to use ssize_t not plain "int". (udt_do_read() correctly uses ssize_t)

Not providing patch since the change is trivial.
Show 32 quoted lines
>
> Signed-off-by: Nicolas Kaiser <nikai@nikai.net>
> ---
> Testsuite did not regress at my place.
>
>  transport-helper.c |    4 ++--
>  1 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/transport-helper.c b/transport-helper.c
> index 4e4754c..710b6f1 100644
> --- a/transport-helper.c
> +++ b/transport-helper.c
> @@ -973,7 +973,7 @@ static int udt_do_read(struct unidirectional_transfer *t)
>  */
>  static int udt_do_write(struct unidirectional_transfer *t)
>  {
> -       size_t bytes;
> +       int bytes;
>
>        if (t->bufuse == 0)
>                return 0;       /* Nothing to write. */
> @@ -989,7 +989,7 @@ static int udt_do_write(struct unidirectional_transfer *t)
>                if (t->bufuse)
>                        memmove(t->buf, t->buf + bytes, t->bufuse);
>                transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
> -                       (int)bytes, t->dest_name, (int)t->bufuse);
> +                       bytes, t->dest_name, (int)t->bufuse);
>        }
>        return 0;
>  }
> --
> 1.7.3.4
-- 
Piotrek
Nicolas Kaiser· Mar 4, 2011, 23:16 UTC · re: Piotr Krukowiecki · lore

[PATCH] transport-helper.c: fix check for size_t < 0 v2

'bytes' is unsigned of type size_t, and can't be negative. But the assigned write() returns ssize_t, and -1 on error. For testing < 0, 'bytes' needs to be of a signed type.

Signed-off-by: Nicolas Kaiser <nikai@nikai.net>
---
* Piotr Krukowiecki <piotr.krukowiecki@gmail.com>:
> You are right that, but the fix should be to use ssize_t not  plain "int".
> (udt_do_read() correctly uses ssize_t)

Indeed, thanks. Here's a version with ssize_t. Testsuite did not regress at my place.

 transport-helper.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
Show changes to transport-helper.c +1 −1
diff --git a/transport-helper.c b/transport-helper.c
index 4e4754c..ba06b70 100644
--- a/transport-helper.c
+++ b/transport-helper.c
@@ -973,7 +973,7 @@ static int udt_do_read(struct unidirectional_transfer *t)
  */
 static int udt_do_write(struct unidirectional_transfer *t)
 {
-	size_t bytes;
+	ssize_t bytes;
 
 	if (t->bufuse == 0)
 		return 0;	/* Nothing to write. */
-- 
1.7.3.4

← back to recent threads