diff mbox series

[RFC,01/14] config: refactor parse_leasetime() - branch amount remains same

Message ID 20240509223213.97389-2-newtwen+github@gmail.com
State Changes Requested
Delegated to: Ansuel Smith
Headers show
Series odhcpd config value clamping | expand

Commit Message

Paul Donald May 9, 2024, 10:30 p.m. UTC
From: Paul Donald <newtwen@gmail.com>

Also make 's' value a noop.

Signed-off-by: Paul Donald <newtwen@gmail.com>
---
 src/config.c | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)

Comments

Christian Marangi Nov. 16, 2024, 2:41 p.m. UTC | #1
On Fri, May 10, 2024 at 12:30:33AM +0200, Paul Donald wrote:
> From: Paul Donald <newtwen@gmail.com>
> 
> Also make 's' value a noop.
> 
> Signed-off-by: Paul Donald <newtwen@gmail.com>
> ---
>  src/config.c | 20 ++++++++------------
>  1 file changed, 8 insertions(+), 12 deletions(-)
> 
> diff --git a/src/config.c b/src/config.c
> index 62d4857..346d74a 100644
> --- a/src/config.c
> +++ b/src/config.c
> @@ -356,18 +356,14 @@ static double parse_leasetime(struct blob_attr *c) {
>  	double time = strcmp(val, "infinite") ? strtod(val, &endptr) : UINT32_MAX;
>  
>  	if (time && endptr && endptr[0]) {
> -		if (endptr[0] == 's')
> -			time *= 1;
> -		else if (endptr[0] == 'm')
> -			time *= 60;
> -		else if (endptr[0] == 'h')
> -			time *= 3600;
> -		else if (endptr[0] == 'd')
> -			time *= 24 * 3600;
> -		else if (endptr[0] == 'w')
> -			time *= 7 * 24 * 3600;
> -		else
> -			goto err;
> +		switch(endptr[0]) {
> +			case 's': break;
> +			case 'm': time *= 60; break;
> +			case 'h': time *= 3600; break;
> +			case 'd': time *= 24 * 3600; break;
> +			case 'w': time *= 7 * 24 * 3600; break;
> +			default: goto err;
> +		}
>  	}
> 

Maybe we can use a fallthrough logic for this?

case 'w':
	time *= 7
	fallthrough;
case 'd':
	time *= 24
	fallthrough;
...
case 's':
	break;

And maybe add comments w = week d = day...
diff mbox series

Patch

diff --git a/src/config.c b/src/config.c
index 62d4857..346d74a 100644
--- a/src/config.c
+++ b/src/config.c
@@ -356,18 +356,14 @@  static double parse_leasetime(struct blob_attr *c) {
 	double time = strcmp(val, "infinite") ? strtod(val, &endptr) : UINT32_MAX;
 
 	if (time && endptr && endptr[0]) {
-		if (endptr[0] == 's')
-			time *= 1;
-		else if (endptr[0] == 'm')
-			time *= 60;
-		else if (endptr[0] == 'h')
-			time *= 3600;
-		else if (endptr[0] == 'd')
-			time *= 24 * 3600;
-		else if (endptr[0] == 'w')
-			time *= 7 * 24 * 3600;
-		else
-			goto err;
+		switch(endptr[0]) {
+			case 's': break;
+			case 'm': time *= 60; break;
+			case 'h': time *= 3600; break;
+			case 'd': time *= 24 * 3600; break;
+			case 'w': time *= 7 * 24 * 3600; break;
+			default: goto err;
+		}
 	}
 
 	if (time < 60)