This is an automated email from the ASF dual-hosted git repository.

andor pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/zookeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new 4508690  ZOOKEEPER-3302: ZooKeeper C client does not compile on Fedora 
29
4508690 is described below

commit 450869006e0f62790e974305eb079e1b0450dbfd
Author: Enrico Olivelli <[email protected]>
AuthorDate: Tue Apr 9 10:07:35 2019 +0200

    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 <[email protected]>
    
    Reviewers: [email protected]
    
    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
---
 zookeeper-client/zookeeper-client-c/src/cli.c | 43 ++++++++++++++++++++-------
 1 file 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 6ca4a41..6f443cd 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");

Reply via email to