Skip to content
This repository was archived by the owner on Jan 29, 2023. It is now read-only.

Commit 977b884

Browse files
authored
Merge pull request #21 from pabloandresm/master
fix server_version memory management
2 parents 9ad0d35 + 31a9960 commit 977b884

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

src/MySQL_Generic_Connection.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,7 @@ class MySQL_Connection : public MySQL_Packet
6767

6868
virtual ~MySQL_Connection()
6969
{
70-
if (server_version)
71-
{
72-
MYSQL_LOGDEBUG("Free server_version");
73-
74-
free(server_version);
75-
}
70+
this->close();
7671
};
7772

7873
bool connect(const IPAddress& server, const uint16_t& port, char *user, char *password, char *db = NULL);

src/MySQL_Generic_Connection_Impl.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ bool MySQL_Connection::connect(const char *hostname, const uint16_t& port, char
153153
}
154154

155155
if (server_version)
156-
free(server_version); // don't need it anymore
156+
{
157+
free(server_version); // don't need it anymore
158+
server_version = NULL;
159+
}
157160

158161
return returnVal;
159162
}
@@ -236,7 +239,10 @@ Connection_Result MySQL_Connection::connectNonBlocking(const char *hostname, con
236239
}
237240

238241
if (server_version)
239-
free(server_version); // don't need it anymore
242+
{
243+
free(server_version); // don't need it anymore
244+
server_version = NULL;
245+
}
240246

241247
return returnVal;
242248
}

src/MySQL_Generic_Packet.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ class MySQL_Packet
9494

9595
free(buffer);
9696
}
97+
if (server_version)
98+
{
99+
MYSQL_LOGDEBUG("Free server_version");
100+
101+
free(server_version);
102+
}
97103
};
98104

99105
bool complete_handshake(char *user, char *password);

src/MySQL_Generic_Packet_Impl.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
MySQL_Packet::MySQL_Packet(Client *client_instance)
7373
{
7474
buffer = NULL;
75+
server_version = NULL;
7576
client = client_instance;
7677
}
7778

@@ -441,8 +442,15 @@ void MySQL_Packet::parse_handshake_packet()
441442
i++;
442443
} while (buffer[i - 1] != 0x00);
443444

444-
server_version = (char *) malloc(i - 5);
445-
strncpy(server_version, (char *) &buffer[5], i - 5);
445+
if (i>5)
446+
{
447+
server_version = (char *) malloc(i - 5);
448+
if (server_version)
449+
{
450+
strncpy(server_version, (char *) &buffer[5], i - 5);
451+
server_version[i-5-1]=0;
452+
}
453+
}
446454

447455
// Capture the first 8 characters of seed
448456
i += 4; // Skip thread id

0 commit comments

Comments
 (0)