diff mbox

linux-generic: odp_coremask: add ODP_ERR for bad str length

Message ID 1420233974-2790-1-git-send-email-mike.holmes@linaro.org
State Rejected
Headers show

Commit Message

Mike Holmes Jan. 2, 2015, 9:26 p.m. UTC
Provide feed back on string length error, matching the handling for the other
APIs in the file.
Improve the readability of the code.

Signed-off-by: Mike Holmes <mike.holmes@linaro.org>
---
 platform/linux-generic/odp_coremask.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

Comments

Ola Liljedahl Jan. 5, 2015, 1:27 p.m. UTC | #1
On 2 January 2015 at 22:26, Mike Holmes <mike.holmes@linaro.org> wrote:
> Provide feed back on string length error, matching the handling for the other
> APIs in the file.
> Improve the readability of the code.
>
> Signed-off-by: Mike Holmes <mike.holmes@linaro.org>
> ---
>  platform/linux-generic/odp_coremask.c | 10 ++++++----
>  1 file changed, 6 insertions(+), 4 deletions(-)
>
> diff --git a/platform/linux-generic/odp_coremask.c b/platform/linux-generic/odp_coremask.c
> index 54cd333..5b4e961 100644
> --- a/platform/linux-generic/odp_coremask.c
> +++ b/platform/linux-generic/odp_coremask.c
> @@ -10,19 +10,21 @@
>  #include <stdlib.h>
>  #include <string.h>
>
> -#define MAX_CORE_NUM   64
> +#define MAX_CORE_NUM 64
> +#define MASK_STR_LEN 18
> +#define BASE_16 16
>
>
>  void odp_coremask_from_str(const char *str, odp_coremask_t *mask)
>  {
>         uint64_t mask_u64;
>
> -       if (strlen(str) > 18) {
> -               /* more than 64 bits */
> +       if (strlen(str) > MASK_STR_LEN) {
> +               ODP_ERR("string longer than reqired for bit mask supported");
I don't think these checks should be done (neither flavor). Maybe the
user is passing in a longer string (e.g. part of the command line).
For many (valid) core masks, the hex string might be shorter and still
contain unrecognized trailing characters.

>                 return;
>         }
>
> -       mask_u64 = strtoull(str, NULL, 16);
> +       mask_u64 = strtoull(str, NULL, BASE_16);
Better to check here that the hex number is terminated by some white
space character. If not report an error. If think this type of
functions which parse text (which may come from the command line or a
config file etc) should have a return value which indicates the
success of the operation. Invalid data that comes from external
sources should not be able to crash the program. Invalid data which
comes from internal sources should crash the program.


>
>         odp_coremask_from_u64(&mask_u64, 1, mask);
>  }
> --
> 2.1.0
>
>
> _______________________________________________
> lng-odp mailing list
> lng-odp@lists.linaro.org
> http://lists.linaro.org/mailman/listinfo/lng-odp
diff mbox

Patch

diff --git a/platform/linux-generic/odp_coremask.c b/platform/linux-generic/odp_coremask.c
index 54cd333..5b4e961 100644
--- a/platform/linux-generic/odp_coremask.c
+++ b/platform/linux-generic/odp_coremask.c
@@ -10,19 +10,21 @@ 
 #include <stdlib.h>
 #include <string.h>
 
-#define MAX_CORE_NUM	64
+#define MAX_CORE_NUM 64
+#define MASK_STR_LEN 18
+#define BASE_16 16
 
 
 void odp_coremask_from_str(const char *str, odp_coremask_t *mask)
 {
 	uint64_t mask_u64;
 
-	if (strlen(str) > 18) {
-		/* more than 64 bits */
+	if (strlen(str) > MASK_STR_LEN) {
+		ODP_ERR("string longer than reqired for bit mask supported");
 		return;
 	}
 
-	mask_u64 = strtoull(str, NULL, 16);
+	mask_u64 = strtoull(str, NULL, BASE_16);
 
 	odp_coremask_from_u64(&mask_u64, 1, mask);
 }