Patchwork [26/26] lib: fwts_alloc: use ptrdiff_t to ensure non-mixed type comparison

login
register
mail settings
Submitter Colin King
Date Oct. 14, 2012, 8:32 p.m.
Message ID <1350246738-31699-27-git-send-email-colin.king@canonical.com>
Download mbox | patch
Permalink /patch/191414/
State Accepted
Headers show

Comments

Colin King - Oct. 14, 2012, 8:32 p.m.
From: Colin Ian King <colin.king@canonical.com>

When comparing difference of two pointers we should compare to
a ptrdiff_t to avoid any messy non-mixed type comparisons.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/lib/src/fwts_alloc.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Alex Hung - Oct. 15, 2012, 7:10 a.m.
On 10/15/2012 04:32 AM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> When comparing difference of two pointers we should compare to
> a ptrdiff_t to avoid any messy non-mixed type comparisons.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/lib/src/fwts_alloc.c |    3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/src/lib/src/fwts_alloc.c b/src/lib/src/fwts_alloc.c
> index 4495ad4..5c61478 100644
> --- a/src/lib/src/fwts_alloc.c
> +++ b/src/lib/src/fwts_alloc.c
> @@ -20,6 +20,7 @@
>   #include <string.h>
>   #include <stdio.h>
>   #include <unistd.h>
> +#include <stddef.h>
>
>   #include "fwts_alloc.h"
>
> @@ -75,7 +76,7 @@ static void *fwts_low_mmap(const size_t requested_size)
>
>   		if ((last_addr_end != NULL) &&
>   		    (last_addr_end < (void*)LIMIT_2GB)) {
> -			if ((addr_start - last_addr_end) > requested_size) {
> +			if ((addr_start - last_addr_end) > (ptrdiff_t)requested_size) {
>   				void *addr = last_addr_end;
>   				ret = mmap(addr, requested_size, PROT_READ | PROT_WRITE,
>   					MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
>

Acked-by: Alex Hung <alex.hung@canonical.com>
Keng-Yu Lin - Oct. 17, 2012, 7:57 a.m.
On Mon, Oct 15, 2012 at 4:32 AM, Colin King <colin.king@canonical.com> wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> When comparing difference of two pointers we should compare to
> a ptrdiff_t to avoid any messy non-mixed type comparisons.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  src/lib/src/fwts_alloc.c |    3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/src/lib/src/fwts_alloc.c b/src/lib/src/fwts_alloc.c
> index 4495ad4..5c61478 100644
> --- a/src/lib/src/fwts_alloc.c
> +++ b/src/lib/src/fwts_alloc.c
> @@ -20,6 +20,7 @@
>  #include <string.h>
>  #include <stdio.h>
>  #include <unistd.h>
> +#include <stddef.h>
>
>  #include "fwts_alloc.h"
>
> @@ -75,7 +76,7 @@ static void *fwts_low_mmap(const size_t requested_size)
>
>                 if ((last_addr_end != NULL) &&
>                     (last_addr_end < (void*)LIMIT_2GB)) {
> -                       if ((addr_start - last_addr_end) > requested_size) {
> +                       if ((addr_start - last_addr_end) > (ptrdiff_t)requested_size) {
>                                 void *addr = last_addr_end;
>                                 ret = mmap(addr, requested_size, PROT_READ | PROT_WRITE,
>                                         MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
> --
> 1.7.10.4
>
Acked-by: Keng-Yu Lin <kengyu@canonical.com>

Patch

diff --git a/src/lib/src/fwts_alloc.c b/src/lib/src/fwts_alloc.c
index 4495ad4..5c61478 100644
--- a/src/lib/src/fwts_alloc.c
+++ b/src/lib/src/fwts_alloc.c
@@ -20,6 +20,7 @@ 
 #include <string.h>
 #include <stdio.h>
 #include <unistd.h>
+#include <stddef.h>
 
 #include "fwts_alloc.h"
 
@@ -75,7 +76,7 @@  static void *fwts_low_mmap(const size_t requested_size)
 
 		if ((last_addr_end != NULL) &&
 		    (last_addr_end < (void*)LIMIT_2GB)) {
-			if ((addr_start - last_addr_end) > requested_size) {
+			if ((addr_start - last_addr_end) > (ptrdiff_t)requested_size) {
 				void *addr = last_addr_end;
 				ret = mmap(addr, requested_size, PROT_READ | PROT_WRITE,
 					MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);