From 679fcb9fc3829eca0df014482d4cc531dcb5030f Mon Sep 17 00:00:00 2001
From: Michael Whittaker <mjwhittaker@berkeley.edu>
Date: Fri, 17 Nov 2017 11:37:07 -0800
Subject: [PATCH] A couple small lockserver client improvements.

1. Previously, typing ` `, `lock`, or `unlock` into a lockserver
   client's REPL would cause the client to crash because of some
   `strtok` calls that were returning `NULL`. Now, this bug is fixed;
   `strtok` returns are checked to be `NULL`, and no input should crash
   the client.
2. Previously, typing in an unrecognized command into the lockserver
   client's repl would print `Unknown command.. Try again!`. Now, it
   also prints out the set of legal commands `Usage: exit | q | lock
   <key> | unlock <key>`. This makes it a bit easier for someone
   tinkering around with TAPIR for the first time (like me) to know what
   to type.
---
 lockserver/client.cc | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/lockserver/client.cc b/lockserver/client.cc
index ccce2d6..45e2266 100644
--- a/lockserver/client.cc
+++ b/lockserver/client.cc
@@ -30,6 +30,17 @@
 
 #include "lockserver/client.h"
 
+namespace {
+
+void
+usage()
+{
+    printf("Unknown command.. Try again!\n");
+    printf("Usage: exit | q | lock <key> | unlock <key>\n");
+}
+
+} // namespace
+
 int
 main(int argc, char **argv)
 {
@@ -68,14 +79,18 @@ main(int argc, char **argv)
             cmd[clen++] = c;
         cmd[clen] = '\0';
 
-        if (clen == 0) continue;
         tok = strtok(cmd, " ,.-");
+        if (tok == NULL) continue;
 
         if (strcasecmp(tok, "exit") == 0 || strcasecmp(tok, "q") == 0) {
             printf("Exiting..\n");
             break;
         } else if (strcasecmp(tok, "lock") == 0) {
             tok = strtok(NULL, " ,.-");
+            if (tok == NULL) {
+                usage();
+                continue;
+            }
             key = string(tok);
             status = locker.lock(key);
 
@@ -86,11 +101,15 @@ main(int argc, char **argv)
             }
         } else if (strcasecmp(tok, "unlock") == 0) {
             tok = strtok(NULL, " ,.-");
+            if (tok == NULL) {
+                usage();
+                continue;
+            }
             key = string(tok);
             locker.unlock(key);
             printf("Unlock Successful\n");
         } else {
-            printf("Unknown command.. Try again!\n");
+            usage();
         }
         fflush(stdout);
     }
-- 
GitLab