Fix wrong use of pointer-to-array as an array, CID #1434191.
[claws.git] / src / plugins / clamd / libclamd / clamd-plugin.c
index a3052296b0102bee7798e82c15d0f435f45e95e6..963cbca163712c6515b728fa3bfe627ddd387c39 100644 (file)
@@ -2,7 +2,7 @@
 
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2008 Michael Rasmussen and the Claws Mail Team
+ * Copyright (C) 1999-2017 Michael Rasmussen and the Claws Mail Team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include "alertpanel.h"
 #include "clamd-plugin.h"
 
+#ifndef UNIX_PATH_MAX
+#define UNIX_PATH_MAX 108
+#endif
+
 /* needs to be generic */
 static const gchar* config_dirs[] = { 
        "/etc", 
@@ -134,7 +138,7 @@ void clamd_create_config_automatic(const gchar* path) {
                                        value = g_strdup(g_strchomp(tmp));
                                if (strcmp(clamd_tokens[0], token) == 0) {
                                        /* UNIX socket */
-                                       Socket = (Clamd_Socket *) malloc(sizeof(Clamd_Socket *));
+                                       Socket = (Clamd_Socket *) malloc(sizeof(Clamd_Socket));
                                        if (Socket) {
                                                Socket->socket.host = NULL;
                                                Socket->socket.port = -1;
@@ -150,7 +154,7 @@ void clamd_create_config_automatic(const gchar* path) {
                                else if (strcmp(clamd_tokens[1], token) == 0) {
                                        /* INET socket */
                                        if (! Socket) {
-                                               Socket = (Clamd_Socket *) malloc(sizeof(Clamd_Socket *));
+                                               Socket = (Clamd_Socket *) malloc(sizeof(Clamd_Socket));
                                                if (Socket) {
                                                        Socket->socket.path = NULL;
                                                        Socket->socket.port = -1;
@@ -301,14 +305,10 @@ static int create_socket() {
                        }
                        debug_print("socket file (create): %d\n", new_sock);
                        addr_u.sun_family = AF_UNIX;
-#ifndef UNIX_PATH_MAX
-#define UNIX_PATH_MAX 108
-#endif
                        if (strlen(Socket->socket.path) > UNIX_PATH_MAX) {
                                g_error("socket path longer than %d-char: %s",
                                        UNIX_PATH_MAX, Socket->socket.path);
                                new_sock = -2;
-#undef UNIX_PATH_MAX
                        } else {
                                memcpy(addr_u.sun_path, Socket->socket.path, 
                                                strlen(Socket->socket.path));
@@ -324,6 +324,10 @@ static int create_socket() {
                        addr_i.sin_family = AF_INET;
                        addr_i.sin_port = htons(Socket->socket.port);
                        hp = gethostbyname(Socket->socket.host);
+                       if (!hp) {
+                               g_error("fail to get host by: %s", Socket->socket.host);
+                               return new_sock;
+                       }
                        debug_print("IP socket host: %s:%d\n",
                                        Socket->socket.host, Socket->socket.port);
                        bcopy((void *)hp->h_addr, (void *)&addr_i.sin_addr, hp->h_length);
@@ -385,8 +389,8 @@ Clamd_Stat clamd_init(Clamd_Socket* config) {
        memset(buf, '\0', sizeof(buf));
        while ((n_read = read(sock, buf, BUFSIZ - 1)) > 0) {
                buf[n_read] = '\0';
-               if (buf[strlen(buf) - 1] == '\n')
-                       buf[strlen(buf) - 1] = '\0';
+               if (buf[n_read - 1] == '\n')
+                       buf[n_read - 1] = '\0';
                debug_print("Ping result: %s\n", buf);
                if (strcmp("PONG", buf) == 0)
                        connect = TRUE;
@@ -403,10 +407,10 @@ Clamd_Stat clamd_init(Clamd_Socket* config) {
            return NO_CONNECTION;
        }
        memset(buf, '\0', sizeof(buf));
-        while ((n_read = read(sock, buf, BUFSIZ - 1)) > 0) {
-           buf[n_read] = '\0';
-           if (buf[strlen(buf) - 1] == '\n')
-               buf[strlen(buf) - 1] = '\0';
+       while ((n_read = read(sock, buf, BUFSIZ - 1)) > 0) {
+               buf[n_read] = '\0';
+               if (buf[n_read - 1] == '\n')
+                       buf[n_read - 1] = '\0';
            debug_print("Version: %s\n", buf);
        }
        close(sock);
@@ -457,11 +461,10 @@ static Clamd_Stat clamd_stream_scan(int sock,
                return NO_CONNECTION;
        }
 
-       while ((count = read(fd, (void *) buf, sizeof(buf))) > 0) {
-               buf[sizeof(buf) - 1] = '\0';
-               if (buf[strlen(buf) - 1] == '\n')
-                       buf[strlen(buf) - 1] = '\0';
-               debug_print("read: %ld bytes\n", count);
+       while ((count = read(fd, (void *) buf, BUFSIZ - 1)) > 0) {
+               buf[count] = '\0';
+               if (buf[count - 1] == '\n')
+                       buf[count - 1] = '\0';
                
                debug_print("chunk size: %ld\n", count);
                chunk = htonl(count);
@@ -475,7 +478,7 @@ static Clamd_Stat clamd_stream_scan(int sock,
                        *res = g_strconcat("ERROR -> ", _("Socket write error"), NULL);
                        return SCAN_ERROR;
                }
-               memset(buf, '\0', sizeof(buf));
+               memset(buf, '\0', BUFSIZ - 1);
        }
        if (count == -1) {
                close(fd);
@@ -496,6 +499,7 @@ static Clamd_Stat clamd_stream_scan(int sock,
                *res = g_strconcat("ERROR -> ", _("Socket read error"), NULL);
                return SCAN_ERROR;
        }
+       (*res)[n_read] = '\0';
        debug_print("received: %s\n", *res);
        return OK;
 }
@@ -513,7 +517,7 @@ Clamd_Stat clamd_verify_email(const gchar* path, response* result) {
        }
        sock = create_socket();
        if (sock < 0) {
-               debug_print("no connection\n");
+               debug_print("no connection (socket create)\n");
                return NO_CONNECTION;
        }
        memset(buf, '\0', sizeof(buf));
@@ -530,23 +534,30 @@ Clamd_Stat clamd_verify_email(const gchar* path, response* result) {
                debug_print("copy to buf: %s\n", tmp);
                memcpy(&buf, tmp, BUFSIZ);
                g_free(tmp);
+               debug_print("response: %s\n", buf);
        }
        else {
                command = g_strconcat(scan, " ", path, "\n", NULL);
                debug_print("command: %s\n", command);
                if (write(sock, command, strlen(command)) == -1) {
-                       debug_print("no connection\n");
-                       stat = NO_CONNECTION;
+                       debug_print("no connection (socket write)\n");
+                       g_free(command);
+                       return NO_CONNECTION;
                }
                g_free(command);
                memset(buf, '\0', sizeof(buf));
+               /* shouldn't we read only once here? we're checking the last response line anyway */
                while ((n_read = read(sock, buf, BUFSIZ - 1)) > 0) {
                        buf[n_read] = '\0';
-                       if (buf[strlen(buf) - 1] == '\n')
-                               buf[strlen(buf) - 1] = '\0';
+                       if (buf[n_read - 1] == '\n')
+                               buf[n_read - 1] = '\0';
+                       debug_print("response: %s\n", buf);
+               }
+               if (n_read == 0) {
+                       buf[n_read] = '\0';
+                       debug_print("response: %s\n", buf);
                }
        }
-       debug_print("response: %s\n", buf);
        if (strstr(buf, "ERROR")) {
                stat = SCAN_ERROR;
                result->msg = g_strdup(buf);