Move over to internal (small) buffer because pbuf data is not null terminated
This commit is contained in:
2023-11-30 01:08:10 +01:00
parent 16c40ce7fe
commit f16e92b7d8

View File

@@ -196,8 +196,8 @@ static void tcp_cmd_parser(struct tcp_pcb* pcb, int argc, char** argv) {
static err_t tcp_cmd_recv_new(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err) {
int argc = 0;
char* argv[MAX_TOKENS];
char cmd[MAX_CMD_LEN];
char* argv[MAX_TOKENS];
LWIP_UNUSED_ARG(arg);
if (err == ERR_OK && p != NULL) {
@@ -205,13 +205,20 @@ static err_t tcp_cmd_recv_new(void* arg, struct tcp_pcb* pcb, struct pbuf* p, er
char* next;
// Make sure the string is null terminated
int len = p->len >= MAX_CMD_LEN ? MAX_CMD_LEN : p->len;
if (p->len >= MAX_CMD_LEN) {
LOG_WARN(TAG, "Command too long");
}
size_t len = p->tot_len >= MAX_CMD_LEN ? MAX_CMD_LEN : p->tot_len;
#if TESTING
memcpy(cmd, p->payload, len);
#else
pbuf_copy_partial(p, cmd, len, 0);
#endif
cmd[len] = '\0';
remove_newline((char*)(p->payload));
remove_newline(cmd);
// Split string into tokens by delimiter (space)
argv[0] = get_next_token((char*)(p->payload), " ", &next);
argv[0] = get_next_token(cmd, " ", &next);
argc = 1;
while (argv[argc - 1] != NULL && argc < MAX_TOKENS) {
argv[argc] = get_next_token(NULL, " ", &next);
@@ -238,40 +245,40 @@ TEST(TCP_CMD, tcp_data_cb) {
char* cmd = (char*)calloc(50, 1);
pbuf p = {.next = NULL, .payload = (void*)cmd, .tot_len = 4, .len = 0, .type_internal = 0, .flags = 0, .if_idx = 0};
strcpy(cmd, "help");
p.tot_len = strlen(cmd);
tcp_cmd_recv_new(NULL, NULL, &p, ERR_OK);
EXPECT_EQ(0, strcmp("help", (char*)p.payload));
strcpy(cmd, "text \"This is printed on the display\"");
p.tot_len = strlen(cmd);
tcp_cmd_recv_new(NULL, NULL, &p, ERR_OK);
EXPECT_EQ(0, strcmp("text", (char*)p.payload));
strcpy(cmd, "color 0xffffff");
p.tot_len = strlen(cmd);
tcp_cmd_recv_new(NULL, NULL, &p, ERR_OK);
EXPECT_EQ(0, strcmp("color", (char*)p.payload));
strcpy(cmd, "text \"This is printed on the display\"");
p.tot_len = strlen(cmd);
tcp_cmd_recv_new(NULL, NULL, &p, ERR_OK);
EXPECT_EQ(0, strcmp("text", (char*)p.payload));
strcpy(cmd, "setImage \"test.bmp\"");
p.tot_len = strlen(cmd);
tcp_cmd_recv_new(NULL, NULL, &p, ERR_OK);
EXPECT_EQ(0, strcmp("setimage", (char*)p.payload));
strcpy(cmd, "setImage \"test.gif\"");
p.tot_len = strlen(cmd);
tcp_cmd_recv_new(NULL, NULL, &p, ERR_OK);
EXPECT_EQ(0, strcmp("setimage", (char*)p.payload));
strcpy(cmd, "setGif \"test.gif\"");
p.tot_len = strlen(cmd);
tcp_cmd_recv_new(NULL, NULL, &p, ERR_OK);
EXPECT_EQ(0, strcmp("setgif", (char*)p.payload));
strcpy(cmd, "setGif \"test.bmp\"");
p.tot_len = strlen(cmd);
tcp_cmd_recv_new(NULL, NULL, &p, ERR_OK);
EXPECT_EQ(0, strcmp("setgif", (char*)p.payload));
strcpy(cmd, "exit");
p.tot_len = strlen(cmd);
tcp_cmd_recv_new(NULL, NULL, &p, ERR_OK);
EXPECT_EQ(0, strcmp("exit", (char*)p.payload));
free(cmd);
}