diff mbox series

[2/2] meta: hour: Fix integer overflow error

Message ID 20190623162544.11604-2-a@juaristi.eus
State Superseded
Delegated to: Pablo Neira
Headers show
Series [1/2] meta: Introduce new conditions 'time', 'day' and 'hour' | expand

Commit Message

Ander Juaristi June 23, 2019, 4:25 p.m. UTC
This patch fixes an overflow error that would happen when introducing edge times
whose second representation is smaller than the value of the tm_gmtoff field, such
as 00:00.

Signed-off-by: Ander Juaristi <a@juaristi.eus>
---
 src/meta.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

Comments

Pablo Neira Ayuso June 25, 2019, 12:21 a.m. UTC | #1
On Sun, Jun 23, 2019 at 06:25:44PM +0200, Ander Juaristi wrote:
> This patch fixes an overflow error that would happen when introducing edge times
> whose second representation is smaller than the value of the tm_gmtoff field, such
> as 00:00.

Please, send a v2 that includes a test for this to tests/py/

Thanks.

> Signed-off-by: Ander Juaristi <a@juaristi.eus>
> ---
>  src/meta.c | 10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/src/meta.c b/src/meta.c
> index 31a86b2..39e551c 100644
> --- a/src/meta.c
> +++ b/src/meta.c
> @@ -565,7 +565,7 @@ static void hour_type_print(const struct expr *expr, struct output_ctx *octx)
>  		cur_tm = localtime(&ts);
>  
>  		if (cur_tm)
> -			seconds += cur_tm->tm_gmtoff;
> +			seconds = (seconds + cur_tm->tm_gmtoff) % 86400;
>  
>  		__hour_type_print_r(0, 0, seconds, out);
>  		nft_print(octx, "\"%s\"", out);
> @@ -616,8 +616,12 @@ convert:
>  		result = tm.tm_hour * 3600 + tm.tm_min * 60 + tm.tm_sec;
>  
>  	/* Substract tm_gmtoff to get the current time */
> -	if (cur_tm)
> -		result -= cur_tm->tm_gmtoff;
> +	if (cur_tm) {
> +		if (result >= cur_tm->tm_gmtoff)
> +			result -= cur_tm->tm_gmtoff;
> +		else
> +			result = 86400 - cur_tm->tm_gmtoff + result;
> +	}
>  
>  success:
>  	*res = constant_expr_alloc(&sym->location, sym->dtype,
> -- 
> 2.17.1
>
diff mbox series

Patch

diff --git a/src/meta.c b/src/meta.c
index 31a86b2..39e551c 100644
--- a/src/meta.c
+++ b/src/meta.c
@@ -565,7 +565,7 @@  static void hour_type_print(const struct expr *expr, struct output_ctx *octx)
 		cur_tm = localtime(&ts);
 
 		if (cur_tm)
-			seconds += cur_tm->tm_gmtoff;
+			seconds = (seconds + cur_tm->tm_gmtoff) % 86400;
 
 		__hour_type_print_r(0, 0, seconds, out);
 		nft_print(octx, "\"%s\"", out);
@@ -616,8 +616,12 @@  convert:
 		result = tm.tm_hour * 3600 + tm.tm_min * 60 + tm.tm_sec;
 
 	/* Substract tm_gmtoff to get the current time */
-	if (cur_tm)
-		result -= cur_tm->tm_gmtoff;
+	if (cur_tm) {
+		if (result >= cur_tm->tm_gmtoff)
+			result -= cur_tm->tm_gmtoff;
+		else
+			result = 86400 - cur_tm->tm_gmtoff + result;
+	}
 
 success:
 	*res = constant_expr_alloc(&sym->location, sym->dtype,