Permalink
Browse files

Fixed memory leaks

  • Loading branch information...
1 parent 1559f52 commit db5660e0b1cac97bc8d8503fbe2883262b5c3dab @SirCmpwn committed Apr 26, 2013
Showing with 34 additions and 0 deletions.
  1. +34 −0 netfuck-c/netfuck.c
View
@@ -44,18 +44,27 @@ int main(int argc, char** argv) { // Usage: netfuck file.bf endpoint:port
while (*port && *port != ':') port++;
if (!*port) {
printf("Invalid endpoint specified. Must be in address:port format.\n");
+ free(executable);
+ free(memory);
+ free(host);
return 1;
}
*port++ = 0;
for (int i = 0; i < strlen(port); i++) {
if (port[i] < '0' || port[i] > '9') {
printf("Invalid port specified. Must be an integer between 0 and 65535.\n");
+ free(executable);
+ free(memory);
+ free(host);
return 1;
}
}
i = atoi(port);
if (i < 0 || i > 0xFFFF) {
printf("Invalid port specified. Must be an integer between 0 and 65535.\n");
+ free(executable);
+ free(memory);
+ free(host);
return 1;
}
struct addrinfo *result, *server, hints;
@@ -64,6 +73,9 @@ int main(int argc, char** argv) { // Usage: netfuck file.bf endpoint:port
hints.ai_socktype = SOCK_DGRAM;
if (getaddrinfo(host, port, &hints, &result)) {
printf("Error looking up %s.\n", host);
+ free(executable);
+ free(memory);
+ free(host);
return 1;
}
@@ -75,17 +87,29 @@ int main(int argc, char** argv) { // Usage: netfuck file.bf endpoint:port
}
if (server == NULL) {
printf("Unable to resolve %s\n", argv[2]);
+ free(executable);
+ free(memory);
+ free(host);
+ freeaddrinfo(result);
return 1;
}
// Open socket
int sockfd = socket(server->ai_family, server->ai_socktype, server->ai_protocol);
if (sockfd < 0) {
printf("Failed to create socket.\n");
+ free(executable);
+ free(memory);
+ free(host);
+ freeaddrinfo(result);
return 1;
}
if (connect(sockfd, server->ai_addr, server->ai_addrlen) < 0) {
printf("Failed to connect to %s.\n", argv[2]);
+ free(executable);
+ free(memory);
+ free(host);
+ freeaddrinfo(result);
return 1;
}
@@ -120,6 +144,11 @@ int main(int argc, char** argv) { // Usage: netfuck file.bf endpoint:port
if (!memory[pointer]) {
if (!seek_next()) {
printf("Error: Unmatched '['\n");
+ shutdown(sockfd, SHUT_RDWR);
+ free(memory);
+ free(executable);
+ free(host);
+ freeaddrinfo(result);
return 1;
}
}
@@ -128,6 +157,11 @@ int main(int argc, char** argv) { // Usage: netfuck file.bf endpoint:port
if (memory[pointer]) {
if (!seek_prev()) {
printf("Error: Unmatched ']'\n");
+ shutdown(sockfd, SHUT_RDWR);
+ free(memory);
+ free(executable);
+ free(host);
+ freeaddrinfo(result);
return 1;
}
}

0 comments on commit db5660e

Please sign in to comment.