-
Notifications
You must be signed in to change notification settings - Fork 27
app: Fix stack corruption with binary to hex conversion #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -91,7 +91,7 @@ static struct sm_socket { | |||||
| } socks[SM_MAX_SOCKET_COUNT]; | ||||||
|
|
||||||
| static struct sm_socket *datamode_sock; /* Socket for data mode */ | ||||||
| static char hex_data[1400 + 1]; /* Buffer for hex data conversion */ | ||||||
| static uint8_t bin_data[1400]; /* Buffer for hex2bin data conversion */ | ||||||
|
|
||||||
| static struct async_poll_ctx { | ||||||
| struct k_work poll_work; /* Work to send poll URCs. */ | ||||||
|
|
@@ -1017,17 +1017,18 @@ static int do_send(struct sm_socket *sock, const uint8_t *data, int len, int fla | |||||
|
|
||||||
| static int data_send_hex(struct sm_socket *sock, const uint8_t *buf, int recv_len) | ||||||
| { | ||||||
| int consumed = 0; | ||||||
| char hex_buf[256] = {0}; | ||||||
| uint16_t data_len = recv_len < (sizeof(hex_buf) / 2) ? recv_len : (sizeof(hex_buf) / 2); | ||||||
| size_t consumed = 0; | ||||||
| char hex_buf[257] = {0}; | ||||||
| uint16_t data_len = | ||||||
| recv_len < (sizeof(hex_buf) - 1) / 2 ? recv_len : (sizeof(hex_buf) - 1) / 2; | ||||||
|
|
||||||
| /* For hex string mode, convert the received data to hex string */ | ||||||
| while (consumed < recv_len) { | ||||||
| int size = sm_util_htoa(buf + consumed, data_len, hex_buf, sizeof(hex_buf)); | ||||||
| size_t size = bin2hex(buf + consumed, data_len, hex_buf, sizeof(hex_buf)); | ||||||
|
|
||||||
| if (size < 0) { | ||||||
| if (size == 0) { | ||||||
| LOG_ERR("Failed to convert binary data to hex string"); | ||||||
| return size; | ||||||
| return -EINVAL; | ||||||
| } | ||||||
| data_send(hex_buf, size); | ||||||
| consumed += size / 2; /* size is in hex string length */ | ||||||
|
|
@@ -1680,7 +1681,7 @@ STATIC int handle_at_send(enum at_parser_cmd_type cmd_type, struct at_parser *pa | |||||
| int err = -EINVAL; | ||||||
| int fd; | ||||||
| uint16_t mode; | ||||||
| int size; | ||||||
| size_t size; | ||||||
|
||||||
| size_t size; | |
| int size; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
size to be dealt in later PR.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -182,63 +182,6 @@ bool sm_util_casecmp(const char *str1, const char *str2) | |
| return true; | ||
| } | ||
|
|
||
| bool sm_util_hexstr_check(const uint8_t *data, uint16_t data_len) | ||
| { | ||
| for (int i = 0; i < data_len; i++) { | ||
| char ch = *(data + i); | ||
|
|
||
| if ((ch < '0' || ch > '9') && | ||
| (ch < 'A' || ch > 'F') && | ||
| (ch < 'a' || ch > 'f')) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| int sm_util_htoa(const uint8_t *hex, uint16_t hex_len, char *ascii, uint16_t ascii_len) | ||
| { | ||
| if (hex == NULL || ascii == NULL) { | ||
| return -EINVAL; | ||
| } | ||
| if (ascii_len < (hex_len * 2)) { | ||
| return -EINVAL; | ||
| } | ||
|
|
||
| for (int i = 0; i < hex_len; i++) { | ||
| sprintf(ascii + (i * 2), "%02X", *(hex + i)); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This caused the stack corruption. When filling the whole buffer, the terminating null-character was written beyond the buffer. |
||
| } | ||
|
|
||
| return (hex_len * 2); | ||
| } | ||
|
|
||
| int sm_util_atoh(const char *ascii, uint16_t ascii_len, uint8_t *hex, uint16_t hex_len) | ||
| { | ||
| char hex_str[3]; | ||
|
|
||
| if (hex == NULL || ascii == NULL) { | ||
| return -EINVAL; | ||
| } | ||
| if ((ascii_len % 2) > 0) { | ||
| return -EINVAL; | ||
| } | ||
| if (ascii_len > (hex_len * 2)) { | ||
| return -EINVAL; | ||
| } | ||
| if (!sm_util_hexstr_check(ascii, ascii_len)) { | ||
| return -EINVAL; | ||
| } | ||
|
|
||
| hex_str[2] = '\0'; | ||
| for (int i = 0; (i * 2) < ascii_len; i++) { | ||
| strncpy(&hex_str[0], ascii + (i * 2), 2); | ||
| *(hex + i) = (uint8_t)strtoul(hex_str, NULL, 16); | ||
| } | ||
|
|
||
| return (ascii_len / 2); | ||
| } | ||
|
|
||
| int util_string_get(struct at_parser *parser, size_t index, char *value, size_t *len) | ||
| { | ||
| int ret; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.