From: greatim Date: Fri, 1 Apr 2016 11:24:36 +0000 (+0900) Subject: [BUG] fix a bug when client connect to server with wrong port number X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2e7325f85e55753952fee127991a451c65f05cd2;p=sdk%2Ftools%2Fsdb.git [BUG] fix a bug when client connect to server with wrong port number 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 --- diff --git a/src/command_function.c b/src/command_function.c index aa96f33..3b61169 100644 --- a/src/command_function.c +++ b/src/command_function.c @@ -47,6 +47,10 @@ 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; } } } diff --git a/src/sockets.c b/src/sockets.c index 8aa5aeb..3093707 100755 --- a/src/sockets.c +++ b/src/sockets.c @@ -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);