diff --git a/ChangeLog.txt b/ChangeLog.txt index 078e10c0..ae3c7d48 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -23,6 +23,8 @@ Broker: - Change sys tree printing output. This format shouldn't be relied upon and may change at any time. Closes #470246. - Minimum supported libwebsockets version is now 1.3. +- Support for Windows XP has been dropped. +- Miscellaneous fixes on Windows. Client library: - Outgoing messages with QoS>1 are no longer retried after a timeout period. diff --git a/client/client_shared.c b/client/client_shared.c index 1ff9ee0b..8d9bbddf 100644 --- a/client/client_shared.c +++ b/client/client_shared.c @@ -527,6 +527,10 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c } cfg->topic_count++; cfg->topics = realloc(cfg->topics, cfg->topic_count*sizeof(char *)); + if(!cfg->topics){ + fprintf(stderr, "Error: Out of memory.\n"); + return 1; + } cfg->topics[cfg->topic_count-1] = strdup(argv[i+1]); } i++; @@ -545,6 +549,10 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c } cfg->filter_out_count++; cfg->filter_outs = realloc(cfg->filter_outs, cfg->filter_out_count*sizeof(char *)); + if(!cfg->filter_outs){ + fprintf(stderr, "Error: Out of memory.\n"); + return 1; + } cfg->filter_outs[cfg->filter_out_count-1] = strdup(argv[i+1]); } i++; @@ -854,6 +862,10 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url) } len = i-start; host = malloc(len + 1); + if(!host){ + fprintf(stderr, "Error: Out of memory.\n"); + goto cleanup; + } memcpy(host, &(str[start]), len); host[len] = '\0'; start = i+1; @@ -863,6 +875,10 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url) * socks5h://username:password@host[:port] */ len = i-start; username_or_host = malloc(len + 1); + if(!username_or_host){ + fprintf(stderr, "Error: Out of memory.\n"); + goto cleanup; + } memcpy(username_or_host, &(str[start]), len); username_or_host[len] = '\0'; start = i+1; @@ -879,6 +895,10 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url) len = i-start; password = malloc(len + 1); + if(!password){ + fprintf(stderr, "Error: Out of memory.\n"); + goto cleanup; + } memcpy(password, &(str[start]), len); password[len] = '\0'; start = i+1; @@ -891,6 +911,10 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url) } len = i-start; username = malloc(len + 1); + if(!username){ + fprintf(stderr, "Error: Out of memory.\n"); + goto cleanup; + } memcpy(username, &(str[start]), len); username[len] = '\0'; start = i+1; @@ -905,6 +929,10 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url) /* Have already seen a @ , so this must be of form * socks5h://username[:password]@host:port */ port = malloc(len + 1); + if(!port){ + fprintf(stderr, "Error: Out of memory.\n"); + goto cleanup; + } memcpy(port, &(str[start]), len); port[len] = '\0'; }else if(username_or_host){ @@ -913,10 +941,18 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url) host = username_or_host; username_or_host = NULL; port = malloc(len + 1); + if(!port){ + fprintf(stderr, "Error: Out of memory.\n"); + goto cleanup; + } memcpy(port, &(str[start]), len); port[len] = '\0'; }else{ host = malloc(len + 1); + if(!host){ + fprintf(stderr, "Error: Out of memory.\n"); + goto cleanup; + } memcpy(host, &(str[start]), len); host[len] = '\0'; } diff --git a/client/pub_client.c b/client/pub_client.c index b29dd60e..b2a2e8dd 100644 --- a/client/pub_client.c +++ b/client/pub_client.c @@ -295,6 +295,7 @@ int main(int argc, char *argv[]) int rc; int rc2; + memset(&cfg, 0, sizeof(struct mosq_config)); rc = client_config_load(&cfg, CLIENT_PUB, argc, argv); if(rc){ client_config_cleanup(&cfg); diff --git a/client/sub_client.c b/client/sub_client.c index 770fb2da..453aaff1 100644 --- a/client/sub_client.c +++ b/client/sub_client.c @@ -230,6 +230,7 @@ int main(int argc, char *argv[]) struct mosquitto *mosq = NULL; int rc; + memset(&cfg, 0, sizeof(struct mosq_config)); rc = client_config_load(&cfg, CLIENT_SUB, argc, argv); if(rc){ client_config_cleanup(&cfg); diff --git a/lib/mosquitto.c b/lib/mosquitto.c index f8575e16..afc015e9 100644 --- a/lib/mosquitto.c +++ b/lib/mosquitto.c @@ -66,7 +66,7 @@ int mosquitto_lib_version(int *major, int *minor, int *revision) int mosquitto_lib_init(void) { #ifdef WIN32 - srand(GetTickCount()); + srand(GetTickCount64()); #else struct timeval tv; @@ -74,9 +74,7 @@ int mosquitto_lib_init(void) srand(tv.tv_sec*1000 + tv.tv_usec/1000); #endif - net__init(); - - return MOSQ_ERR_SUCCESS; + return net__init(); } int mosquitto_lib_cleanup(void) diff --git a/lib/net_mosq.c b/lib/net_mosq.c index 7b636b41..e36b8cb1 100644 --- a/lib/net_mosq.c +++ b/lib/net_mosq.c @@ -79,11 +79,13 @@ Contributors: int tls_ex_index_mosq = -1; #endif -void net__init(void) +int net__init(void) { #ifdef WIN32 WSADATA wsaData; - WSAStartup(MAKEWORD(2,2), &wsaData); + if(WSAStartup(MAKEWORD(2,2), &wsaData) != 0){ + return MOSQ_ERR_UNKNOWN; + } #endif #ifdef WITH_SRV @@ -98,6 +100,7 @@ void net__init(void) tls_ex_index_mosq = SSL_get_ex_new_index(0, "client context", NULL, NULL, NULL); } #endif + return MOSQ_ERR_SUCCESS; } void net__cleanup(void) diff --git a/lib/net_mosq.h b/lib/net_mosq.h index 8b3d5589..2f64ca3a 100644 --- a/lib/net_mosq.h +++ b/lib/net_mosq.h @@ -49,7 +49,7 @@ struct mosquitto_db; #define MOSQ_MSB(A) (uint8_t)((A & 0xFF00) >> 8) #define MOSQ_LSB(A) (uint8_t)(A & 0x00FF) -void net__init(void); +int net__init(void); void net__cleanup(void); int net__socket_connect(struct mosquitto *mosq, const char *host, uint16_t port, const char *bind_address, bool blocking); diff --git a/lib/time_mosq.c b/lib/time_mosq.c index 0f85bbc4..16a9c291 100644 --- a/lib/time_mosq.c +++ b/lib/time_mosq.c @@ -30,33 +30,10 @@ Contributors: #include "mosquitto.h" #include "time_mosq.h" -#ifdef WIN32 -static bool tick64 = false; - -void _windows_time_version_check(void) -{ - OSVERSIONINFO vi; - - tick64 = false; - - memset(&vi, 0, sizeof(OSVERSIONINFO)); - vi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); - if(GetVersionEx(&vi)){ - if(vi.dwMajorVersion > 5){ - tick64 = true; - } - } -} -#endif - time_t mosquitto_time(void) { #ifdef WIN32 - if(tick64){ - return GetTickCount64()/1000; - }else{ - return GetTickCount()/1000; /* FIXME - need to deal with overflow. */ - } + return GetTickCount64()/1000; #elif _POSIX_TIMERS>0 && defined(_POSIX_MONOTONIC_CLOCK) struct timespec tp; diff --git a/src/mosquitto.c b/src/mosquitto.c index aa6e80c2..67187e3d 100644 --- a/src/mosquitto.c +++ b/src/mosquitto.c @@ -444,7 +444,7 @@ int main(int argc, char *argv[]) } #ifdef WIN32 -int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) +int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { char **argv; int argc = 1; diff --git a/src/mosquitto_passwd.c b/src/mosquitto_passwd.c index 0f6af4fa..e504f1fe 100644 --- a/src/mosquitto_passwd.c +++ b/src/mosquitto_passwd.c @@ -211,7 +211,7 @@ int gets_quiet(char *s, int len) { #ifdef WIN32 HANDLE h; - DWORD con_orig, con_quiet; + DWORD con_orig, con_quiet = 0; DWORD read_len = 0; memset(s, 0, len); @@ -418,6 +418,10 @@ int main(int argc, char *argv[]) } backup_file = malloc(strlen(password_file)+5); + if(!backup_file){ + fprintf(stderr, "Error: Out of memory.\n"); + return 1; + } snprintf(backup_file, strlen(password_file)+5, "%s.tmp", password_file); if(create_backup(backup_file, fptr)){ diff --git a/src/service.c b/src/service.c index 1e22d5c9..96147f22 100644 --- a/src/service.c +++ b/src/service.c @@ -56,6 +56,7 @@ void __stdcall service_main(DWORD dwArgc, LPTSTR *lpszArgv) service_handle = RegisterServiceCtrlHandler("mosquitto", service_handler); if(service_handle){ + memset(conf_path, 0, MAX_PATH + 20); rc = GetEnvironmentVariable("MOSQUITTO_DIR", conf_path, MAX_PATH); if(!rc || rc == MAX_PATH){ service_status.dwCurrentState = SERVICE_STOPPED; @@ -91,7 +92,11 @@ void service_install(void) char exe_path[MAX_PATH + 5]; SERVICE_DESCRIPTION svc_desc; - GetModuleFileName(NULL, exe_path, MAX_PATH); + memset(exe_path, 0, MAX_PATH+5); + if(GetModuleFileName(NULL, exe_path, MAX_PATH) == MAX_PATH){ + fprintf(stderr, "Error: Path too long.\n"); + return; + } strcat(exe_path, " run"); sc_manager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);