diff options
author | Enrico Olivelli <eolivelli@apache.org> | 2019-04-09 10:07:35 +0200 |
---|---|---|
committer | Andor Molnar <andor@apache.org> | 2019-04-09 10:07:35 +0200 |
commit | 450869006e0f62790e974305eb079e1b0450dbfd (patch) | |
tree | f029b1a9bd388fcc025691053658c6f0161d7aaf /zookeeper-client | |
parent | 96c435f4a05312a357a2692650cc6eb858896af0 (diff) | |
download | zookeeper-450869006e0f62790e974305eb079e1b0450dbfd.tar.gz |
ZOOKEEPER-3302: ZooKeeper C client does not compile on Fedora 29
Use a safer value as limit for strncpy, taking into account the NULL terminator (see -Werror=stringop-truncation)
See https://issues.apache.org/jira/browse/ZOOKEEPER-3302 for details about the error
Author: Enrico Olivelli <eolivelli@apache.org>
Reviewers: andor@apache.org
Closes #846 from eolivelli/fix/ZOOKEEPER-3302-build-fedora-29 and squashes the following commits:
768ee50f6 [Enrico Olivelli] add comment
21cb65c1c [Enrico Olivelli] Drop debug
0c909e336 [Enrico Olivelli] ZOOKEEPER-3302 ZooKeeper C client does not compile on Fedora 29
2bb205f8d [Enrico Olivelli] ZOOKEEPER-3302 ZooKeeper C client does not compile on Fedora 29
Diffstat (limited to 'zookeeper-client')
-rw-r--r-- | zookeeper-client/zookeeper-client-c/src/cli.c | 43 |
1 files changed, 33 insertions, 10 deletions
diff --git a/zookeeper-client/zookeeper-client-c/src/cli.c b/zookeeper-client/zookeeper-client-c/src/cli.c index 6ca4a415f..6f443cd50 100644 --- a/zookeeper-client/zookeeper-client-c/src/cli.c +++ b/zookeeper-client/zookeeper-client-c/src/cli.c @@ -649,6 +649,35 @@ void processline(char *line) { zoo_add_auth(zh, line, ptr, ptr ? strlen(ptr) : 0, NULL, NULL); } } +/* + * Look for a command in the form 'cmd:command'. + * Strips the prefix and copies the command in buf. + * Returns 0 if the argument does not start with the prefix. + * Returns -1 in case of error (command too long). + * Returns 1 in case of success. + * + */ +int handleBatchMode(char* arg, char* buf, size_t maxlen) { + size_t cmdlen = strlen(arg); + if (cmdlen < 4) { + // too short + return 0; + } + cmdlen -= 4; + if(strncmp("cmd:", arg, 4) != 0){ + return 0; + } + // we must leave space for the NULL terminator + if (cmdlen >= maxlen) { + fprintf(stderr, + "Command length %zu exceeds max length of %zu\n", + cmdlen, + maxlen); + return -1; + } + memcpy(cmd, arg + 4, cmdlen); + return 1; +} int main(int argc, char **argv) { #ifndef THREADED @@ -677,18 +706,12 @@ int main(int argc, char **argv) { return 2; } if (argc > 2) { - if(strncmp("cmd:",argv[2],4)==0){ - size_t cmdlen = strlen(argv[2]); - if (cmdlen > sizeof(cmd)) { - fprintf(stderr, - "Command length %zu exceeds max length of %zu\n", - cmdlen, - sizeof(cmd)); + int batchModeRes = handleBatchMode(argv[2], cmd, sizeof(cmd)); + if (batchModeRes == -1) { return 2; - } - strncpy(cmd, argv[2]+4, sizeof(cmd)); + } else if(batchModeRes == 1){ batchMode=1; - fprintf(stderr,"Batch mode: %s\n",cmd); + fprintf(stderr,"Batch mode: '%s'\n",cmd); }else{ clientIdFile = argv[2]; fh = fopen(clientIdFile, "r"); |