nserver

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit 33159496912aa5e65314a73941f191c06fe3d581
parent 8c164789ee0e7cbfc745f568791cd830bdebcd89
Author: rsiddharth <s@ricketyspace.net>
Date:   Mon, 23 Dec 2019 21:02:39 -0500

nserver: Update check_cmd.

* nserver/src/ncmd.c (check_cmd): Add argument err.
* nserver/src/ncmd.h (check_cmd): Update function declaration.
* nserver/tests/ncmd_tests.c (test_check_cmd): Update test.

Diffstat:
nserver/src/ncmd.c | 19++++++++++---------
nserver/src/ncmd.h | 2+-
nserver/tests/ncmd_tests.c | 26++++++++++++++------------
3 files changed, 25 insertions(+), 22 deletions(-)

diff --git a/nserver/src/ncmd.c b/nserver/src/ncmd.c @@ -20,10 +20,9 @@ int sanitize(char *cmd) return -1; } -char *check_cmd(char *cmd) +int check_cmd(char *cmd, char *err) { - char *err = NULL; - + check_mem(err); check(cmd != NULL, "cmd is NULL"); int rc = sanitize(cmd); @@ -31,19 +30,21 @@ char *check_cmd(char *cmd) size_t len = strlen(cmd); if (len >= CMD_MIN_SIZE && len <= CMD_MAX_SIZE) { - return NULL; + return 0; } if (len == 0) { - err = "closing connection\n"; + strncpy(err, "closing connection\n", RSP_SIZE); + return -1; } else { - err = "command size invalid\n"; + strncpy(err, "command size invalid\n", RSP_SIZE); + return -1; } - return err; + return 0; error: - err = "internal error\n"; - return err; + strncpy(err, "internal error\n", RSP_SIZE); + return -1; } diff --git a/nserver/src/ncmd.h b/nserver/src/ncmd.h @@ -22,7 +22,7 @@ enum FUNCTIONS { }; int sanitize(char *cmd); -char *check_cmd(char *cmd); +int check_cmd(char *cmd, char *err); struct bstrList *cmd_parts(char *cmd); diff --git a/nserver/tests/ncmd_tests.c b/nserver/tests/ncmd_tests.c @@ -55,30 +55,31 @@ char *test_sanitize() char *test_check_cmd() { - char *cmd = calloc(128, sizeof(char)); + char *err = (char *) calloc(RSP_SIZE, sizeof(char)); + + char *cmd = (char *) calloc(128, sizeof(char)); mu_assert(cmd != NULL, "calloc failed"); char *bacon = "/create api/bacon"; strncpy(cmd, bacon, strlen(bacon)); mu_assert(strlen(cmd) == strlen(bacon), "strncpy failed"); - char *err = check_cmd(cmd); - mu_assert(err == NULL, "check_cmd failed"); - + int rc = check_cmd(cmd, err); + mu_assert(rc == 0, "check_cmd failed"); + memset(err, '\0', RSP_SIZE); memset(cmd, '\0', 128); - char *c = "/c"; strncpy(cmd, c, strlen(c)); mu_assert(strlen(cmd) == strlen(c), "strncpy failed"); - err = check_cmd(cmd); - mu_assert(err != NULL, "check_cmd failed"); + rc = check_cmd(cmd, err); + mu_assert(rc < 0, "check_cmd failed"); mu_assert(strcmp(err, "command size invalid\n") == 0, "wrong err msg"); - + memset(err, '\0', RSP_SIZE); memset(cmd, '\0', 128); @@ -86,19 +87,20 @@ char *test_check_cmd() strncpy(cmd, empty, strlen(empty)); mu_assert(strlen(cmd) == strlen(empty), "strncpy failed"); - err = check_cmd(cmd); - mu_assert(err != NULL, "check_cmd failed"); + rc = check_cmd(cmd, err); + mu_assert(rc < 0, "check_cmd failed"); mu_assert(strcmp(err, "closing connection\n") == 0, "wrong err msg"); - err = check_cmd(NULL); - mu_assert(err != NULL, "check_cmd failed"); + rc = check_cmd(NULL, err); + mu_assert(rc < 0, "check_cmd failed"); mu_assert(strcmp(err, "internal error\n") == 0, "wrong err msg"); // Cleanup. + free(err); free(cmd); return NULL;