added checks on the return values of read/write

This commit is contained in:
Christian Gießen
2015-10-31 09:18:36 +01:00
parent f6ecdab9c5
commit 5ceb31a97e

15
fiche.c
View File

@@ -92,7 +92,8 @@ void *thread_connection(void *args)
{ {
display_info(data, NULL, "Rejected connection from unknown user."); display_info(data, NULL, "Rejected connection from unknown user.");
save_log(NULL, data.ip_address, data.hostname); save_log(NULL, data.ip_address, data.hostname);
write(connection_socket, "You are not whitelisted!\n", 26); if (write(connection_socket, "You are not whitelisted!\n", 26) < 0)
error("Error writing on stream socket");
close(connection_socket); close(connection_socket);
pthread_exit(NULL); pthread_exit(NULL);
} }
@@ -101,7 +102,8 @@ void *thread_connection(void *args)
{ {
display_info(data, NULL, "Rejected connection from banned user."); display_info(data, NULL, "Rejected connection from banned user.");
save_log(NULL, data.ip_address, data.hostname); save_log(NULL, data.ip_address, data.hostname);
write(connection_socket, "You are banned!\n", 17); if (write(connection_socket, "You are banned!\n", 17) < 0)
error("Error writing on stream socket");
close(connection_socket); close(connection_socket);
pthread_exit(NULL); pthread_exit(NULL);
} }
@@ -116,13 +118,15 @@ void *thread_connection(void *args)
save_log(slug, data.ip_address, data.hostname); save_log(slug, data.ip_address, data.hostname);
char response[strlen(slug) + strlen(DOMAIN) + 2]; char response[strlen(slug) + strlen(DOMAIN) + 2];
snprintf(response, sizeof response, "%s%s\n", DOMAIN, slug); snprintf(response, sizeof response, "%s%s\n", DOMAIN, slug);
write(connection_socket, response, strlen(response)); if (write(connection_socket, response, strlen(response)) < 0)
error("Error writing on stream socket");
} }
else else
{ {
display_info(data, NULL, "Invalid connection."); display_info(data, NULL, "Invalid connection.");
save_log(NULL, data.ip_address, data.hostname); save_log(NULL, data.ip_address, data.hostname);
write(connection_socket, "Use netcat.\n", 12); if (write(connection_socket, "Use netcat.\n", 12) < 0)
error("Error writing on stream socket");
} }
close(connection_socket); close(connection_socket);
@@ -254,7 +258,8 @@ void load_list(char *file_path, int type)
fseek(fp, 0, SEEK_SET); fseek(fp, 0, SEEK_SET);
char *buffer = malloc(fsize + 1); char *buffer = malloc(fsize + 1);
fread(buffer, fsize, 1, fp); if (fread(buffer, fsize, 1, fp) != fsize)
error("reading list failed");
fclose(fp); fclose(fp);
buffer[fsize] = 0; buffer[fsize] = 0;