[BUG] fix a bug when client connect to server with wrong port number
authorgreatim <jaewon81.lim@samsung.com>
Fri, 1 Apr 2016 11:24:36 +0000 (20:24 +0900)
committergreatim <jaewon81.lim@samsung.com>
Mon, 4 Apr 2016 09:26:15 +0000 (18:26 +0900)
ex) sdb connect localhost:7777ab
server should send the message that the port number is invalid
client should check the return string for null

Change-Id: I8cd19cdaf542e5f62a0d98a1543c65e0a4648050
Signed-off-by: greatim <jaewon81.lim@samsung.com>
src/command_function.c
src/sockets.c

index aa96f33a8203262bd81f32cada9d8f26244949b8..3b61169084aef7eb44d13c291893bb0516655de0 100644 (file)
 static const char *SDK_TOOL_PATH="/home/developer/sdk_tools";
 static const char *APP_PATH_PREFIX="/opt/apps";
 
+// the time limit to wait for connection with device
+static const int DEVICE_ONLINE_CHECK_INTERVAL = 500; // msec
+static const int DEVICE_ONLINE_CHECK_REPEAT = 6;
+
 static void __inline__ format_host_command(char* buffer, size_t  buflen, const char* command, transport_type ttype, const char* serial);
 static int get_pkgtype_file_name(const char* file_name);
 static int kill_gdbserver_if_running(const char* process_cmd);
@@ -304,6 +308,7 @@ int __connect(int argc, char ** argv) {
     char full_cmd[PATH_MAX];
     char * tmp;
     char remote_target_info[30] = { 0, };
+    int i;
 
     if (strstr(argv[1], ":") == NULL)
         snprintf(remote_target_info, sizeof remote_target_info, "%s:%d", argv[1], DEFAULT_SDB_LOCAL_TRANSPORT_PORT);
@@ -319,17 +324,21 @@ int __connect(int argc, char ** argv) {
         if(strstr(tmp, "connecting") == NULL)
             return 0;
 
-        // check if device state is online for 3 sec
-        int i = 0;
-        for (i; i < 6; i++) {
-            sdb_sleep_ms(500);
+        // check if device state is online for 3 secs
+        for (i = 0; i < DEVICE_ONLINE_CHECK_REPEAT; i++) {
+            sdb_sleep_ms(DEVICE_ONLINE_CHECK_INTERVAL);
 
             snprintf(full_cmd, sizeof full_cmd, "host-serial:%s:get-state", remote_target_info);
             tmp = sdb_query(full_cmd);
 
-            if(!strcmp(tmp, STATE_DEVICE) || !strcmp(tmp, STATE_LOCKED) || !strcmp(tmp, STATE_SUSPENDED)) {
-                printf("connected to %s\n", remote_target_info);
-                return 0;
+            if (tmp != NULL) {
+                if(!strcmp(tmp, STATE_DEVICE) || !strcmp(tmp, STATE_LOCKED) || !strcmp(tmp, STATE_SUSPENDED)) {
+                    printf("connected to %s\n", remote_target_info);
+                    return 0;
+                }
+            } else {
+                // connection error occurred
+                break;
             }
         }
     }
index 8aa5aeb2bce28da70a2d86683adf6143f598a919..30937073704314532d3715081f2aeb0fdd5f7933 100755 (executable)
@@ -64,16 +64,22 @@ static void connect_service(void* x)
     char buf[1024] = {0, };
 
     char* portstr = strchr(host, ':');
-    int port = -1;
+    long port = -1;
+    char* reststr;
 
     if(portstr) {
         *portstr++ = 0;
-        if (!sscanf(portstr, "%d", &port)) {
+
+        // use 'strtol' to make sure that port string contains only number character
+        errno = 0;
+        port = strtol(portstr, &reststr, 10);
+        LOG_DEBUG("port number(%ld), reststr(%s)\n", port, reststr);
+        if (errno != 0 || reststr[0] != '\0') {
             snprintf(buf, sizeof(buf), "bad port format '%s'", portstr);
             goto connect_done;
         }
     }
-    connect_emulator(host, port, buf, sizeof(buf));
+    connect_emulator(host, (int)port, buf, sizeof(buf));
 
 connect_done:
     free_list(socket->pkt_list, put_apacket);