From feb20edda79ed1184b1fb508b03a08712014b0f8 Mon Sep 17 00:00:00 2001 From: Pawel Jakub Dawidek Date: Fri, 3 Feb 2012 09:00:39 +0100 Subject: [PATCH 1/2] Plug memory leak in case of an empty file and terminate string with '\0'. --- libfreerdp-core/certificate.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libfreerdp-core/certificate.c b/libfreerdp-core/certificate.c index ec50b4ed0..5eb877146 100644 --- a/libfreerdp-core/certificate.c +++ b/libfreerdp-core/certificate.c @@ -610,13 +610,17 @@ int certificate_data_match(rdpCertificateStore* certificate_store, rdpCertificat size = ftell(fp); fseek(fp, 0, SEEK_SET); - data = (char*) xmalloc(size + 1); - length = fread(data, size, 1, fp); - if (size < 1) return match; + data = (char*) xmalloc(size + 2); + if (fread(data, size, 1, fp) != 1) { + xfree(data); + return match; + } + data[size] = '\n'; + data[size + 1] = '\0'; pline = strtok(data, "\n"); while (pline != NULL) From 1619b4378179906777feae489e1f6db58801e73a Mon Sep 17 00:00:00 2001 From: Pawel Jakub Dawidek Date: Fri, 3 Feb 2012 14:44:45 +0100 Subject: [PATCH 2/2] Fix problem where we check errno even for status == 0. This way when connection was properly terminated, but errno had EAGAIN value from before, we looped idenfiniately. --- libfreerdp-core/tcp.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/libfreerdp-core/tcp.c b/libfreerdp-core/tcp.c index 281ddae23..095f369af 100644 --- a/libfreerdp-core/tcp.c +++ b/libfreerdp-core/tcp.c @@ -185,7 +185,12 @@ int tcp_read(rdpTcp* tcp, uint8* data, int length) status = recv(tcp->sockfd, data, length, 0); - if (status <= 0) + if (status == 0) + { + /* Peer disconnected. */ + return -1; + } + else if (status < 0) { #ifdef _WIN32 int wsa_error = WSAGetLastError(); @@ -194,17 +199,13 @@ int tcp_read(rdpTcp* tcp, uint8* data, int length) if (wsa_error == WSAEWOULDBLOCK) return 0; - /* When peer disconnects we get status 0 with no error. */ - if (status < 0) - printf("recv() error: %d\n", wsa_error); + printf("recv() error: %d\n", wsa_error); #else /* No data available */ if (errno == EAGAIN || errno == EWOULDBLOCK) return 0; - /* When peer disconnects we get status 0 with no error. */ - if (status < 0) - perror("recv"); + perror("recv"); #endif return -1; }