From 93cadfb21511c29366ae8754fe09bd17ef12b9da Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Sun, 3 May 2020 12:27:54 +1000 Subject: unpack: consistent error messages between pack and unpack Signed-off-by: Steve Bennett --- jim-pack.c | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'jim-pack.c') diff --git a/jim-pack.c b/jim-pack.c index 61741fc..6518192 100644 --- a/jim-pack.c +++ b/jim-pack.c @@ -293,20 +293,24 @@ static int Jim_UnpackCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) if (Jim_GetWide(interp, argv[3], &pos) != JIM_OK) { return JIM_ERR; } + if (pos < 0 || (option == OPT_STR && pos % 8)) { + Jim_SetResultFormatted(interp, "bad bitoffset: %#s", argv[3]); + return JIM_ERR; + } if (Jim_GetWide(interp, argv[4], &width) != JIM_OK) { return JIM_ERR; } + if (width < 0 || (option == OPT_STR && width % 8) || (option != OPT_STR && width > sizeof(jim_wide) * 8) || + ((option == OPT_FLOATLE || option == OPT_FLOATBE) && width != 32 && width != 64)) { + Jim_SetResultFormatted(interp, "bad bitwidth: %#s", argv[4]); + return JIM_ERR; + } if (option == OPT_STR) { int len; const char *str = Jim_GetString(argv[1], &len); - if (width % 8 || pos % 8) { - Jim_SetResultString(interp, "string field is not on a byte boundary", -1); - return JIM_ERR; - } - - if (pos >= 0 && width > 0 && pos < len * 8) { + if (pos < len * 8) { if (pos + width > len * 8) { width = len * 8 - pos; } @@ -319,12 +323,7 @@ static int Jim_UnpackCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) const unsigned char *str = (const unsigned char *)Jim_GetString(argv[1], &len); jim_wide result = 0; - if (width > sizeof(jim_wide) * 8) { - Jim_SetResultFormatted(interp, "int field is too wide: %#s", argv[4]); - return JIM_ERR; - } - - if (pos >= 0 && width > 0 && pos < len * 8) { + if (pos < len * 8) { if (pos + width > len * 8) { width = len * 8 - pos; } @@ -344,11 +343,8 @@ static int Jim_UnpackCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) double fresult; if (width == 32) { fresult = (double) JimIntToFloat(result); - } else if (width == 64) { - fresult = JimIntToDouble(result); } else { - Jim_SetResultFormatted(interp, "float field has bad bitwidth: %#s", argv[4]); - return JIM_ERR; + fresult = JimIntToDouble(result); } Jim_SetResult(interp, Jim_NewDoubleObj(interp, fresult)); } else { -- cgit v1.1