Skip to content

Commit

Permalink
Merge pull request #221 from trilitech/palmer@functori@fix-codeQL
Browse files Browse the repository at this point in the history
CodeQL: fix some code quality
  • Loading branch information
ajinkyaraj-23 authored Feb 16, 2024
2 parents e243bdc + 7a2ff5e commit 4c04d78
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 67 deletions.
1 change: 1 addition & 0 deletions app/src/handle_swap.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <format.h>
#include <io.h>
#include <string.h>
#include <swap.h>

#include "handle_swap.h"
Expand Down
14 changes: 7 additions & 7 deletions app/src/parser/micheline_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ typedef enum {
} tz_micheline_parser_step_kind;

typedef enum {
TZ_CAP_STREAM_ANY = 0,
TZ_CAP_STREAM_BYTES,
TZ_CAP_STREAM_INT,
TZ_CAP_STREAM_STRING,
TZ_CAP_ADDRESS,
TZ_CAP_LIST = 62,
TZ_CAP_OR
TZ_CAP_STREAM_ANY = 0,
TZ_CAP_STREAM_BYTES = 1,
TZ_CAP_STREAM_INT = 2,
TZ_CAP_STREAM_STRING = 3,
TZ_CAP_ADDRESS = 4,
TZ_CAP_LIST = 62,
TZ_CAP_OR = 63
} tz_micheline_capture_kind;

typedef struct {
Expand Down
106 changes: 57 additions & 49 deletions app/src/parser/operation_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
See the License for the specific language governing permissions and
limitations under the License. */

#include <string.h>

#include "operation_parser.h"
#include "micheline_parser.h"
#include "num_parser.h"
Expand Down Expand Up @@ -484,6 +486,60 @@ tz_step_read_micheline(tz_parser_state *state)
tz_reraise;
}

static void
tz_format_amount(char *str)
{
int len = 0;
while (str[len]) {
len++;
}
if ((len == 1) && (str[0] == 0)) {
// just 0
goto add_currency;
}
if (len < 7) {
// less than one tez, pad left up to the '0.'
int j;
int pad = 7 - len;
for (j = len; j >= 0; j--) {
str[j + pad] = str[j];
}
for (j = 0; j < pad; j++) {
str[j] = '0';
}
len = 7;
}
int no_decimals = 1;
for (int i = 0; i < 6; i++) {
no_decimals &= (str[len - 1 - i] == '0');
}
if (no_decimals) {
// integral value, don't include the decimal part (no '.'_
str[len - 6] = 0;
len -= 6;
} else {
// more than one tez, add the '.'
for (int i = 0; i < 6; i++) {
str[len - i] = str[len - i - 1];
}
str[len - 6] = '.';
len++;
str[len] = 0;
// drop trailing non significant zeroes
while (str[len - 1] == '0') {
len--;
str[len] = 0;
}
}
add_currency:
str[len] = ' ';
str[len + 1] = 'X';
str[len + 2] = 'T';
str[len + 3] = 'Z';
len += 4;
str[len] = 0;
}

/* Read a number */
static tz_parser_result
tz_step_read_num(tz_parser_state *state)
Expand Down Expand Up @@ -524,55 +580,7 @@ tz_step_read_num(tz_parser_state *state)
break;
case TZ_OPERATION_FIELD_FEE:
case TZ_OPERATION_FIELD_AMOUNT: {
int len = 0;
while (str[len]) {
len++;
}
if ((len == 1) && (str[0] == 0)) {
// just 0
goto add_currency;
}
if (len < 7) {
// less than one tez, pad left up to the '0.'
int j;
int pad = 7 - len;
for (j = len; j >= 0; j--) {
str[j + pad] = str[j];
}
for (j = 0; j < pad; j++) {
str[j] = '0';
}
len = 7;
}
int no_decimals = 1;
for (int i = 0; i < 6; i++) {
no_decimals &= (str[len - 1 - i] == '0');
}
if (no_decimals) {
// integral value, don't include the decimal part (no '.'_
str[len - 6] = 0;
len -= 6;
} else {
// more than one tez, add the '.'
for (int i = 0; i < 6; i++) {
str[len - i] = str[len - i - 1];
}
str[len - 6] = '.';
len++;
str[len] = 0;
// drop trailing non significant zeroes
while (str[len - 1] == '0') {
len--;
str[len] = 0;
}
}
add_currency:
str[len] = ' ';
str[len + 1] = 'X';
str[len + 2] = 'T';
str[len + 3] = 'Z';
len += 4;
str[len] = 0;
tz_format_amount(str);
break;
}
default:
Expand Down
22 changes: 11 additions & 11 deletions app/src/parser/parser_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ typedef struct {
typedef enum {
// success and non blocking, should loop again
TZ_CONTINUE = 0, // fall through rest of current step
TZ_BREAK, // signals caller to return, errno should be CONTINUE
TZ_BREAK = 1, // signals caller to return, errno should be CONTINUE
// success but parsing blocked
TZ_BLO_DONE = 100, // parsing complete
TZ_BLO_FEED_ME, // blocked on read from input
TZ_BLO_IM_FULL, // blocked on output space
TZ_BLO_DONE = 100, // parsing complete
TZ_BLO_FEED_ME = 101, // blocked on read from input
TZ_BLO_IM_FULL = 102, // blocked on output space
// everything below is an error
TZ_ERR_INVALID_TAG = 200,
TZ_ERR_INVALID_OP,
TZ_ERR_INVALID_DATA,
TZ_ERR_UNSUPPORTED,
TZ_ERR_TOO_LARGE,
TZ_ERR_TOO_DEEP,
TZ_ERR_INVALID_STATE,
TZ_ERR_INVALID_TAG = 200,
TZ_ERR_INVALID_OP = 201,
TZ_ERR_INVALID_DATA = 202,
TZ_ERR_UNSUPPORTED = 203,
TZ_ERR_TOO_LARGE = 204,
TZ_ERR_TOO_DEEP = 205,
TZ_ERR_INVALID_STATE = 206,
} tz_parser_result;
#define TZ_IS_BLOCKED(code) (code >= 100)
#define TZ_IS_ERR(code) (code >= 200)
Expand Down
1 change: 1 addition & 0 deletions app/src/ui_strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <io.h>
#include <stdbool.h>
#include <string.h>

#include "globals.h"
#include "ui_strings.h"
Expand Down

0 comments on commit 4c04d78

Please sign in to comment.