Create helper function for optional hex input
authorChristof Schmitt <cs@samba.org>
Mon, 6 Jul 2015 20:07:33 +0000 (13:07 -0700)
committerMartin Schwenke <martin@meltin.net>
Wed, 24 Feb 2016 10:44:28 +0000 (21:44 +1100)
Signed-off-by: Christof Schmitt <cs@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
(Imported from commit 663db9fbb028fe524bb0eef09398c62bf4fb08d4)

tools/ctdb.c

index 23765aec89ed0483d3772775f772ff6dae3468d0..eded98f73d87c711ba0fad0c4fe5c50844a33996 100644 (file)
@@ -171,12 +171,11 @@ static int h2i(char h)
        return h - '0';
 }
 
-static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
+static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str, size_t len)
 {
-       int i, len;
+       int i;
        TDB_DATA key = {NULL, 0};
 
-       len = strlen(str);
        if (len & 0x01) {
                DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
                return key;
@@ -191,6 +190,20 @@ static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
        return key;
 }
 
+static TDB_DATA strtodata(TALLOC_CTX *mem_ctx, const char *str, size_t len)
+{
+       TDB_DATA key;
+
+       if (!strncmp(str, "0x", 2)) {
+               key = hextodata(mem_ctx, str + 2, len - 2);
+       } else {
+               key.dptr  = talloc_memdup(mem_ctx, str, len);
+               key.dsize = len;
+       }
+
+       return key;
+}
+
 /* Parse a nodestring.  Parameter dd_ok controls what happens to nodes
  * that are disconnected or deleted.  If dd_ok is true those nodes are
  * included in the output list of nodes.  If dd_ok is false, those
@@ -4081,15 +4094,10 @@ static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv
                return -1;
        }
 
-       if (!strncmp(argv[1], "0x", 2)) {
-               key = hextodata(tmp_ctx, argv[1] + 2);
-               if (key.dsize == 0) {
-                       printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
-                       return -1;
-               }
-       } else {
-               key.dptr  = discard_const(argv[1]);
-               key.dsize = strlen(argv[1]);
+       key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
+       if (key.dptr == NULL) {
+               printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
+               return -1;
        }
 
        data = tdb_fetch(tdb, key);
@@ -4148,26 +4156,16 @@ static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv
                return -1;
        }
 
-       if (!strncmp(argv[1], "0x", 2)) {
-               key = hextodata(tmp_ctx, argv[1] + 2);
-               if (key.dsize == 0) {
-                       printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
-                       return -1;
-               }
-       } else {
-               key.dptr  = discard_const(argv[1]);
-               key.dsize = strlen(argv[1]);
+       key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
+       if (key.dptr == NULL) {
+               printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
+               return -1;
        }
 
-       if (!strncmp(argv[2], "0x", 2)) {
-               value = hextodata(tmp_ctx, argv[2] + 2);
-               if (value.dsize == 0) {
-                       printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
-                       return -1;
-               }
-       } else {
-               value.dptr  = discard_const(argv[2]);
-               value.dsize = strlen(argv[2]);
+       value = strtodata(tmp_ctx, argv[2], strlen(argv[2]));
+       if (value.dptr == NULL) {
+               printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
+               return -1;
        }
 
        ZERO_STRUCT(header);
@@ -4281,15 +4279,10 @@ static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv
                return -1;
        }
 
-       if (!strncmp(argv[1], "0x", 2)) {
-               key = hextodata(tmp_ctx, argv[1] + 2);
-               if (key.dsize == 0) {
-                       printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
-                       return -1;
-               }
-       } else {
-               key.dptr  = discard_const(argv[1]);
-               key.dsize = strlen(argv[1]);
+       key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
+       if (key.dptr == NULL) {
+               printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
+               return -1;
        }
 
        ret = ctdb_transaction_store(h, key, data);