diff mbox series

[v3,1/2] Adding STRTOULL_MUL macro

Message ID 1533637102-13633-1-git-send-email-angelo@amarulasolutions.com
State Changes Requested
Headers show
Series [v3,1/2] Adding STRTOULL_MUL macro | expand

Commit Message

Angelo Compagnucci Aug. 7, 2018, 10:18 a.m. UTC
This macro is useful in that cases in which a string should be converted
to an ull but accordingly to the size suffixes.

This patch refactor also the places where this macro could be used.

Signed-off-by: Angelo Compagnucci <angelo@amarulasolutions.com>
---
 corelib/lua_interface.c | 12 +-----------
 include/swupdate.h      | 13 +++++++++++++
 parser/parse_external.c | 12 +-----------
 parser/parser.c         | 12 +-----------
 4 files changed, 16 insertions(+), 33 deletions(-)

Comments

Stefano Babic Aug. 7, 2018, 10:31 a.m. UTC | #1
Hi Angelo,

On 07/08/2018 12:18, Angelo Compagnucci wrote:
> This macro is useful in that cases in which a string should be converted
> to an ull but accordingly to the size suffixes.
> 
> This patch refactor also the places where this macro could be used.
> 
> Signed-off-by: Angelo Compagnucci <angelo@amarulasolutions.com>
> ---
>  corelib/lua_interface.c | 12 +-----------
>  include/swupdate.h      | 13 +++++++++++++
>  parser/parse_external.c | 12 +-----------
>  parser/parser.c         | 12 +-----------
>  4 files changed, 16 insertions(+), 33 deletions(-)
> 
> diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
> index 6104e06..4676c42 100644
> --- a/corelib/lua_interface.c
> +++ b/corelib/lua_interface.c
> @@ -280,17 +280,7 @@ static void lua_string_to_img(struct img_type *img, const char *key,
>  	if (!strncmp(key, offset, sizeof(offset))) {
>  		strncpy(seek_str, value,
>  			sizeof(seek_str));
> -		/* convert the offset handling multiplicative suffixes */
> -		if (strnlen(seek_str, MAX_SEEK_STRING_SIZE) != 0) {
> -			errno = 0;
> -			img->seek = ustrtoull(seek_str, &endp, 0);
> -			if (seek_str == endp || (img->seek == ULLONG_MAX && \
> -					errno == ERANGE)) {
> -				ERROR("offset argument: ustrtoull failed");
> -				return;
> -			}
> -		} else
> -			img->seek = 0;
> +		STRTOULL_MUL(seek_str, img->seek);
>  	}
>  }
>  
> diff --git a/include/swupdate.h b/include/swupdate.h
> index 455e4ad..4d2343d 100644
> --- a/include/swupdate.h
> +++ b/include/swupdate.h
> @@ -150,6 +150,19 @@ struct swupdate_cfg {
>  	} \
>  } while(0)
>  
> +/* convert the src_str handling multiplicative suffixes */
> +#define STRTOULL_MUL(src_str, dest_ull)                                        \
> +	if (strnlen(src_str, MAX_SEEK_STRING_SIZE) != 0) {                     \
> +		errno = 0;                                                     \
> +		dest_ull = ustrtoull(src_str, &endp, 0);                       \
> +		if (src_str == endp || (dest_ull == ULLONG_MAX &&              \
> +			errno == ERANGE)) {                                    \
> +			ERROR("offset argument: ustrtoull failed");            \
> +			return -1;                                             \
> +		}                                                              \
> +	} else                                                                 \
> +		dest_ull = 0;
> +


but in this case you get the ERROR with "offset" even when you want to
get the size of UBI. The ERROR should not be part here.


I propose:

- you change ustrtoull() instead of adding the macro
- ustrtoull checks for the string and sets errno in case of error
- the caller check errno and raise ERROR

	

>  off_t extract_sw_description(int fd, const char *descfile, off_t start);
>  int cpio_scan(int fd, struct swupdate_cfg *cfg, off_t start);
>  struct swupdate_cfg *get_swupdate_cfg(void);
> diff --git a/parser/parse_external.c b/parser/parse_external.c
> index 66ba3d5..d3330cd 100644
> --- a/parser/parse_external.c
> +++ b/parser/parse_external.c
> @@ -69,17 +69,7 @@ static void sw_append_stream(struct img_type *img, const char *key,
>  	if (!strncmp(key, offset, sizeof(offset))) {
>  		strncpy(seek_str, value,
>  			sizeof(seek_str));
> -		/* convert the offset handling multiplicative suffixes */
> -		if (strnlen(seek_str, MAX_SEEK_STRING_SIZE) != 0) {
> -			errno = 0;
> -			img->seek = ustrtoull(seek_str, &endp, 0);
> -			if (seek_str == endp || (img->seek == ULLONG_MAX && \
> -					errno == ERANGE)) {
> -				ERROR("offset argument: ustrtoull failed");
> -				return;
> -			}
> -		} else
> -			img->seek = 0;
> +		STRTOULL_MUL(seek_str, img->seek);
>  	}
>  	if (!strcmp(key, "script"))
>  		img->is_script = 1;
> diff --git a/parser/parser.c b/parser/parser.c
> index 7329810..851802e 100644
> --- a/parser/parser.c
> +++ b/parser/parser.c
> @@ -252,17 +252,7 @@ static int parse_common_attributes(parsertype p, void *elem, struct img_type *im
>  	GET_FIELD_STRING(p, elem, "data", image->type_data);
>  	get_hash_value(p, elem, image->sha256);
>  
> -	/* convert the offset handling multiplicative suffixes */
> -	if (strnlen(seek_str, MAX_SEEK_STRING_SIZE) != 0) {
> -		errno = 0;
> -		image->seek = ustrtoull(seek_str, &endp, 0);
> -		if (seek_str == endp || (image->seek == ULLONG_MAX && \
> -			errno == ERANGE)) {
> -			ERROR("offset argument: ustrtoull failed");
> -			return -1;
> -		}
> -	} else
> -		image->seek = 0;
> +	STRTOULL_MUL(seek_str, image->seek);
>  
>  	get_field(p, elem, "compressed", &image->compressed);
>  	get_field(p, elem, "installed-directly", &image->install_directly);
> 

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index 6104e06..4676c42 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -280,17 +280,7 @@  static void lua_string_to_img(struct img_type *img, const char *key,
 	if (!strncmp(key, offset, sizeof(offset))) {
 		strncpy(seek_str, value,
 			sizeof(seek_str));
-		/* convert the offset handling multiplicative suffixes */
-		if (strnlen(seek_str, MAX_SEEK_STRING_SIZE) != 0) {
-			errno = 0;
-			img->seek = ustrtoull(seek_str, &endp, 0);
-			if (seek_str == endp || (img->seek == ULLONG_MAX && \
-					errno == ERANGE)) {
-				ERROR("offset argument: ustrtoull failed");
-				return;
-			}
-		} else
-			img->seek = 0;
+		STRTOULL_MUL(seek_str, img->seek);
 	}
 }
 
diff --git a/include/swupdate.h b/include/swupdate.h
index 455e4ad..4d2343d 100644
--- a/include/swupdate.h
+++ b/include/swupdate.h
@@ -150,6 +150,19 @@  struct swupdate_cfg {
 	} \
 } while(0)
 
+/* convert the src_str handling multiplicative suffixes */
+#define STRTOULL_MUL(src_str, dest_ull)                                        \
+	if (strnlen(src_str, MAX_SEEK_STRING_SIZE) != 0) {                     \
+		errno = 0;                                                     \
+		dest_ull = ustrtoull(src_str, &endp, 0);                       \
+		if (src_str == endp || (dest_ull == ULLONG_MAX &&              \
+			errno == ERANGE)) {                                    \
+			ERROR("offset argument: ustrtoull failed");            \
+			return -1;                                             \
+		}                                                              \
+	} else                                                                 \
+		dest_ull = 0;
+
 off_t extract_sw_description(int fd, const char *descfile, off_t start);
 int cpio_scan(int fd, struct swupdate_cfg *cfg, off_t start);
 struct swupdate_cfg *get_swupdate_cfg(void);
diff --git a/parser/parse_external.c b/parser/parse_external.c
index 66ba3d5..d3330cd 100644
--- a/parser/parse_external.c
+++ b/parser/parse_external.c
@@ -69,17 +69,7 @@  static void sw_append_stream(struct img_type *img, const char *key,
 	if (!strncmp(key, offset, sizeof(offset))) {
 		strncpy(seek_str, value,
 			sizeof(seek_str));
-		/* convert the offset handling multiplicative suffixes */
-		if (strnlen(seek_str, MAX_SEEK_STRING_SIZE) != 0) {
-			errno = 0;
-			img->seek = ustrtoull(seek_str, &endp, 0);
-			if (seek_str == endp || (img->seek == ULLONG_MAX && \
-					errno == ERANGE)) {
-				ERROR("offset argument: ustrtoull failed");
-				return;
-			}
-		} else
-			img->seek = 0;
+		STRTOULL_MUL(seek_str, img->seek);
 	}
 	if (!strcmp(key, "script"))
 		img->is_script = 1;
diff --git a/parser/parser.c b/parser/parser.c
index 7329810..851802e 100644
--- a/parser/parser.c
+++ b/parser/parser.c
@@ -252,17 +252,7 @@  static int parse_common_attributes(parsertype p, void *elem, struct img_type *im
 	GET_FIELD_STRING(p, elem, "data", image->type_data);
 	get_hash_value(p, elem, image->sha256);
 
-	/* convert the offset handling multiplicative suffixes */
-	if (strnlen(seek_str, MAX_SEEK_STRING_SIZE) != 0) {
-		errno = 0;
-		image->seek = ustrtoull(seek_str, &endp, 0);
-		if (seek_str == endp || (image->seek == ULLONG_MAX && \
-			errno == ERANGE)) {
-			ERROR("offset argument: ustrtoull failed");
-			return -1;
-		}
-	} else
-		image->seek = 0;
+	STRTOULL_MUL(seek_str, image->seek);
 
 	get_field(p, elem, "compressed", &image->compressed);
 	get_field(p, elem, "installed-directly", &image->install_directly);