Browse Source

* fix valgrind checks in f2b_cmd_*() & improve testing

master
Alex 'AdUser' Z 4 years ago
parent
commit
9f7a8dc8a6
  1. 16
      src/commands.c
  2. 18
      t/t_cmd.c

16
src/commands.c

@ -137,22 +137,22 @@ f2b_cmd_create(const char *line) {
assert(line != NULL);
while (isspace(*line)) line++;
if (strlen(line) <= 0)
return NULL; /* empty string */
if ((cmd = calloc(1, sizeof(f2b_cmd_t))) == NULL)
return NULL;
if (f2b_buf_alloc(&cmd->data, strlen(line))) {
if (f2b_cmd_parse(cmd, line))
return cmd;
free(cmd);
cmd = NULL;
}
return cmd;
return NULL;
}
void
f2b_cmd_destroy(f2b_cmd_t *cmd) {
if (!cmd) return;
f2b_buf_free(&cmd->data);
free(cmd);
}
@ -168,6 +168,9 @@ f2b_cmd_parse(f2b_cmd_t *cmd, const char *src) {
while (isblank(*src))
src++;
if (strlen(src) == 0)
return false; /* empty string */
f2b_buf_alloc(&cmd->data, strlen(src) + 1);
f2b_buf_append(&cmd->data, src, 0);
@ -234,6 +237,7 @@ f2b_cmd_parse(f2b_cmd_t *cmd, const char *src) {
}
}
cmd->type = CMD_UNKNOWN;
memset(cmd->args, 0x0, sizeof(cmd->args));
f2b_buf_free(&cmd->data);
return false;

18
t/t_cmd.c

@ -4,6 +4,10 @@
int main() {
f2b_cmd_t cmd;
f2b_cmd_t *c;
assert(f2b_cmd_parse(&cmd, "") == false);
assert(f2b_cmd_parse(&cmd, " ") == false);
assert(f2b_cmd_parse(&cmd, "status") == true);
assert(cmd.type = CMD_STATUS);
@ -37,5 +41,19 @@ int main() {
assert(strcmp(cmd.args[4], "7200") == 0);
f2b_buf_free(&cmd.data);
c = f2b_cmd_create("");
assert(c == NULL);
c = f2b_cmd_create(" ");
assert(c == NULL);
c = f2b_cmd_create("\n\r\n");
assert(c == NULL);
c = f2b_cmd_create("test");
assert(c == NULL); /* no such command */
c = f2b_cmd_create("help");
assert(c != NULL);
f2b_cmd_destroy(c);
f2b_buf_free(&cmd.data);
return EXIT_SUCCESS;
}

Loading…
Cancel
Save