]> source.dussan.org Git - rspamd.git/commitdiff
* Remove strlcpy code and replace it with glib alternative
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Wed, 3 Dec 2008 16:48:15 +0000 (19:48 +0300)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Wed, 3 Dec 2008 16:48:15 +0000 (19:48 +0300)
* Polish code and remove bugs found

15 files changed:
Makefile.in
compat/strlcpy.c [deleted file]
compat/strlcpy.h [deleted file]
configure
perl/rspamd.xs
src/controller.c
src/main.c
src/memcached.c
src/plugins/surbl.c
src/protocol.c
src/statfile.c
src/util.c
test/.depends
test/rspamd_memcached_test.c
utils/.depends

index 0bb5380d01bf0d3fb0a6059c5abb988ba142f74b..fc7078d1a57c06eb6844a6cdb117cbbd4546c1d9 100644 (file)
@@ -30,7 +30,7 @@ dist-clean: clean dist-clean-subdirs
        rm -f Makefile
        rm -f test/Makefile
        rm -f config.log
-       rm -f $(SRCDIR)/md5.h $(SRCDIR)/md5.c $(SRCDIR)/strlcpy.h $(SRCDIR)/strlcpy.c $(SRCDIR)/queue.h $(SRCDIR)/config.h $(SRCDIR)/modules.c $(SRCDIR)/modules.h
+       rm -f $(SRCDIR)/md5.h $(SRCDIR)/md5.c $(SRCDIR)/queue.h $(SRCDIR)/config.h $(SRCDIR)/modules.c $(SRCDIR)/modules.h
        cd perl && rm -f Makefile.old && rm -f Makefile.PL && cd ..
 
 creategroup:
diff --git a/compat/strlcpy.c b/compat/strlcpy.c
deleted file mode 100644 (file)
index 1c08055..0000000
+++ /dev/null
@@ -1,70 +0,0 @@
-/*     $OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $     */
-
-/*
- * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. The name of the author may not be used to endorse or promote products
- *    derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
- * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
- * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#if defined(LIBC_SCCS) && !defined(lint)
-static char *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $";
-#endif /* LIBC_SCCS and not lint */
-#include <sys/cdefs.h>
-
-#include <sys/types.h>
-#include <string.h>
-
-/*
- * Copy src to string dst of size siz.  At most siz-1 characters
- * will be copied.  Always NUL terminates (unless siz == 0).
- * Returns strlen(src); if retval >= siz, truncation occurred.
- */
-size_t
-strlcpy(dst, src, siz)
-       char *dst;
-       const char *src;
-       size_t siz;
-{
-       char *d = dst;
-       const char *s = src;
-       size_t n = siz;
-
-       /* Copy as many bytes as will fit */
-       if (n != 0 && --n != 0) {
-               do {
-                       if ((*d++ = *s++) == 0)
-                               break;
-               } while (--n != 0);
-       }
-
-       /* Not enough room in dst, add NUL and traverse rest of src */
-       if (n == 0) {
-               if (siz != 0)
-                       *d = '\0';              /* NUL-terminate dst */
-               while (*s++)
-                       ;
-       }
-
-       return(s - src - 1);    /* count does not include NUL */
-}
diff --git a/compat/strlcpy.h b/compat/strlcpy.h
deleted file mode 100644 (file)
index 74d772d..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-#ifndef STRLCPY_H
-#define STRLCPY_H
-
-#include <sys/types.h>
-
-size_t strlcpy(char *, const char*, size_t);
-
-#endif
index 6c8eb4bf27e6b50a1471c7c4c5972263c2a896bf..08d83c713c93b04af4a86d47ca3ea88a58af9895 100755 (executable)
--- a/configure
+++ b/configure
@@ -485,7 +485,7 @@ check_perl()
        echo "#include <stdlib.h>" > autotest.c
        echo "#include <EXTERN.h>" >> autotest.c
        echo "#include <perl.h>" >> autotest.c
-       echo "int main (int argc, char **argv) { PERL_SYS_INIT3(&argc,&argv,&env); return 0; }" >> autotest.c
+       echo "int main (int argc, char **argv, char **env) { PERL_SYS_INIT3(&argc,&argv,&env); return 0; }" >> autotest.c
        echo "$GCC $CFLAGS $PERLCFLAGS $PTHREAD_CFLAGS -o autotest $LDFLAGS $LIBS $PERLLDFLAGS $PTHREAD_LDFLAGS autotest.c" >>config.log
        $GCC $CFLAGS $PERLCFLAGS $PTHREAD_CFLAGS -o autotest $LDFLAGS $LIBS $PERLLDFLAGS $PTHREAD_LDFLAGS autotest.c >>config.log 2>&1
        if [ $? -eq 0 ] ; then 
@@ -891,14 +891,6 @@ if [ $? -eq 0 ] ; then
        have_opt "PIDFILE"
 fi
 
-check_function "strlcpy" "string.h"
-if [ $? -eq 1 ] ; then
-       cp $COMPAT_DIR/strlcpy.c $SRCDIR
-       cp $COMPAT_DIR/strlcpy.h $SRCDIR
-       SOURCES="$SOURCES strlcpy.c"
-       DEPS="$DEPS $SRCDIR/strlcpy.h"
-       have_opt "STRLCPY_H"
-fi
 check_function "bzero" "string.h"
 check_function "srandomdev"
 if [ $? -eq 0 ] ; then
@@ -935,7 +927,6 @@ check_include "stdint.h"
 if [ $? -eq 1 ] ; then
        check_include "inttypes.h"
 fi
-check_include "strlcpy.h"
 check_include "md5.h"
 check_include "sys/queue.h"
 if [ $? -eq 1 ] ; then 
index 1c36b06e054d08d8e892135864105fbce4071a5e..c96401033c3cb2f063b2f546f1162805d1281145 100644 (file)
@@ -9,6 +9,7 @@
 #include <EXTERN.h>
 #include <perl.h>
 #include <XSUB.h>
+#include <glib.h>
 
 #include "../src/config.h"
 #include "../src/main.h"
@@ -91,10 +92,10 @@ get_part (r, num)
        RETVAL = newHV();
        type = g_mime_content_type_to_string (part->type);
 
-       hv_store_ent (RETVAL, 
+       (void)hv_store_ent (RETVAL, 
                                newSVpv ("type", sizeof ("type") - 1), 
                                newSVpv (type, strlen(type)), 0);
-       hv_store_ent (RETVAL, 
+       (void)hv_store_ent (RETVAL, 
                                newSVpv ("content", sizeof ("content") - 1), 
                                newSVpv ((char *)part->content->data, part->content->len), 0);
     sv_2mortal((SV*)RETVAL);
@@ -217,7 +218,7 @@ read_memcached_key (r, key, datalen, callback)
     callback_data->task = r;
     ctx->callback_data = (void *)callback_data;
 
-    strlcpy (param.key, key, sizeof (param.key));
+    g_strlcpy (param.key, key, sizeof (param.key));
        param.buf = memory_pool_alloc (r->task_pool, datalen);
     if (param.buf != NULL) {
         param.bufsize = datalen;
@@ -267,7 +268,7 @@ write_memcached_key (r, key, data, expire, callback)
     callback_data->task = r;
     ctx->callback_data = (void *)callback_data;
 
-    strlcpy (param.key, key, sizeof (param.key));
+    g_strlcpy (param.key, key, sizeof (param.key));
     param.buf = data;
     param.bufsize = datalen;
     param.bufpos = 0;
@@ -312,7 +313,7 @@ delete_memcached_key (r, key, callback)
     callback_data->task = r;
     ctx->callback_data = (void *)callback_data;
 
-    strlcpy (param.key, key, sizeof (param.key));
+    g_strlcpy (param.key, key, sizeof (param.key));
     param.buf = NULL;
     param.bufsize = 0;
     param.bufpos = 0;
index 393bc234790378e04420b571611ae86e2bbee3ab..b85db1b2d25474a43e26e391ccdcd809ce268be3 100644 (file)
@@ -179,7 +179,7 @@ process_command (struct controller_command *cmd, char **cmd_args, struct control
                                }
                                /* If uptime is less than 1 minute print only seconds */
                                else if (uptime / 60 == 0) {
-                                       r = snprintf (out_buf, sizeof (out_buf), "%dsecond%s", uptime, uptime > 1 ? "s" : " ");
+                                       r = snprintf (out_buf, sizeof (out_buf), "%dsecond%s", (int)uptime, (int)uptime > 1 ? "s" : " ");
                                }
                                /* Else print the minutes and seconds. */
                                else {
index 445b0e42bb8cc1ca023d3a0af68f6cf5a28157fd..64ac455a7975e179c55daff1aba56d59db7938a2 100644 (file)
@@ -158,7 +158,7 @@ fork_worker (struct rspamd_main *rspamd, int listen_sock, int reconfig, enum pro
 }
 
 int 
-main (int argc, char **argv)
+main (int argc, char **argv, char **env)
 {
        struct rspamd_main *rspamd;
        struct module_ctx *cur_module = NULL;
index 05ae166173b0234e95564467d6c459ac9a1b599f..22dd9fbb323d262209363938cb44630839179ce0 100644 (file)
@@ -105,7 +105,9 @@ write_handler (int fd, short what, memcached_ctx_t *ctx)
                        iov[2].iov_len = ctx->param->bufsize - ctx->param->bufpos;
                        iov[3].iov_base = CRLF;
                        iov[3].iov_len = sizeof (CRLF) - 1;
-                       writev (ctx->sock, iov, 4);
+                       if (writev (ctx->sock, iov, 4) == -1) {
+                               memc_log (ctx, __LINE__, "memc_write: writev failed: %m");
+                       }
                }
                else {
                        iov[0].iov_base = read_buf;
@@ -195,10 +197,14 @@ read_handler (int fd, short what, memcached_ctx_t *ctx)
                        iov[0].iov_len = sizeof (struct memc_udp_header);
                        iov[1].iov_base = read_buf;
                        iov[1].iov_len = r;
-                       writev (ctx->sock, iov, 2);
+                       if (writev (ctx->sock, iov, 2) == -1) {
+                               memc_log (ctx, __LINE__, "memc_write: writev failed: %m");
+                       }
                }
                else {
-                       write (ctx->sock, read_buf, r);
+                       if (write (ctx->sock, read_buf, r) == -1) {
+                               memc_log (ctx, __LINE__, "memc_write: write failed: %m");
+                       }
                }
                event_del (&ctx->mem_ev);
                event_set (&ctx->mem_ev, ctx->sock, EV_READ | EV_PERSIST | EV_TIMEOUT, socket_callback, (void *)ctx);
@@ -325,9 +331,14 @@ delete_handler (int fd, short what, memcached_ctx_t *ctx)
                        iov[1].iov_base = read_buf;
                        iov[1].iov_len = r;
                        ctx->param->bufpos = writev (ctx->sock, iov, 2);
+                       if (ctx->param->bufpos == -1) {
+                               memc_log (ctx, __LINE__, "memc_write: writev failed: %m");
+                       }
                }
                else {
-                       write (ctx->sock, read_buf, r);
+                       if (write (ctx->sock, read_buf, r) == -1) {
+                               memc_log (ctx, __LINE__, "memc_write: write failed: %m");
+                       }
                }
                event_del (&ctx->mem_ev);
                event_set (&ctx->mem_ev, ctx->sock, EV_READ | EV_PERSIST | EV_TIMEOUT, socket_callback, (void *)ctx);
index c90a8419e01c820c31c17375a1ccc91eb2c0e025..07e2376bfb4b8fdeaec1077d6c87d20adb2bf9ee 100644 (file)
@@ -337,7 +337,7 @@ memcached_callback (memcached_ctx_t *ctx, memc_error_t error, void *data)
        switch (ctx->op) {
                case CMD_CONNECT:
                        if (error != OK) {
-                               msg_info ("memcached_callback: memcached returned error %s on CONNECT stage");
+                               msg_info ("memcached_callback: memcached returned error %s on CONNECT stage", memc_strerror (error));
                                memc_close_ctx (param->ctx);
                                param->task->save.saved --;
                                if (param->task->save.saved == 0) {
@@ -356,7 +356,7 @@ memcached_callback (memcached_ctx_t *ctx, memc_error_t error, void *data)
                        break;
                case CMD_READ:
                        if (error != OK) {
-                               msg_info ("memcached_callback: memcached returned error %s on READ stage");
+                               msg_info ("memcached_callback: memcached returned error %s on READ stage", memc_strerror (error));
                                memc_close_ctx (param->ctx);
                                param->task->save.saved --;
                                if (param->task->save.saved == 0) {
@@ -382,7 +382,7 @@ memcached_callback (memcached_ctx_t *ctx, memc_error_t error, void *data)
                        break;
                case CMD_WRITE:
                        if (error != OK) {
-                               msg_info ("memcached_callback: memcached returned error %s on WRITE stage");
+                               msg_info ("memcached_callback: memcached returned error %s on WRITE stage", memc_strerror (error));
                        }
                        memc_close_ctx (param->ctx);
                        param->task->save.saved --;
@@ -428,7 +428,7 @@ register_memcached_call (struct uri *url, struct worker_task *task)
        cur_param->bufsize = sizeof (int);
 
        sum_str = g_compute_checksum_for_string (G_CHECKSUM_MD5, struri (url), -1);
-       strlcpy (cur_param->key, sum_str, sizeof (cur_param->key));
+       g_strlcpy (cur_param->key, sum_str, sizeof (cur_param->key));
        g_free (sum_str);
 
        selected = (struct memcached_server *) get_upstream_by_hash ((void *)task->cfg->memcached_servers,
@@ -471,7 +471,18 @@ redirector_callback (int fd, short what, void *arg)
                                event_set (&param->ev, param->sock, EV_READ | EV_PERSIST | EV_TIMEOUT, redirector_callback, (void *)param);
                                event_add (&param->ev, &timeout);
                                r = snprintf (url_buf, sizeof (url_buf), "GET %s HTTP/1.0\r\n\r\n", struri (param->url));
-                               write (param->sock, url_buf, r);
+                               if (write (param->sock, url_buf, r) == -1) {
+                                       msg_err ("redirector_callback: write failed %m");
+                                       event_del (&param->ev);
+                                       param->task->save.saved --;
+                                       if (param->task->save.saved == 0) {
+                                               /* Call other filters */
+                                               param->task->save.saved = 1;
+                                               process_filters (param->task);
+                                       }
+                                       g_free (param);
+                                       return;
+                               }
                                param->state = STATE_READ;
                        }
                        else {
index b259f6cd96e630841e153effc0374501c0162ca5..027e3a91acc195be79b4edc7b08030f4900ef290 100644 (file)
@@ -349,7 +349,7 @@ write_check_reply (struct worker_task *task)
        char outbuf[OUTBUFSIZ];
        struct metric_result *metric_res;
 
-       r = snprintf (outbuf, sizeof (outbuf), "%s 0 %s" CRLF, (task->proto == SPAMC_PROTO) ? SPAMD_REPLY_BANNER : RSPAMD_REPLY_BANNER);
+       r = snprintf (outbuf, sizeof (outbuf), "%s 0 %s" CRLF, (task->proto == SPAMC_PROTO) ? SPAMD_REPLY_BANNER : RSPAMD_REPLY_BANNER, "OK");
        bufferevent_write (task->bev, outbuf, r);
        if (task->proto == SPAMC_PROTO) {
                /* Ignore metrics, just write report for 'default' metric */
@@ -435,7 +435,7 @@ write_process_reply (struct worker_task *task)
        char outbuf[OUTBUFSIZ];
 
        r = snprintf (outbuf, sizeof (outbuf), "%s 0 %s" CRLF "Content-Length: %zd" CRLF CRLF, 
-                                       (task->proto == SPAMC_PROTO) ? SPAMD_REPLY_BANNER : RSPAMD_REPLY_BANNER, task->msg->buf->len);
+                                       (task->proto == SPAMC_PROTO) ? SPAMD_REPLY_BANNER : RSPAMD_REPLY_BANNER, "OK", task->msg->buf->len);
        bufferevent_write (task->bev, outbuf, r);
        bufferevent_write (task->bev, task->msg->buf->begin, task->msg->buf->len);
 
index fc7ebfc7a5951dfd267c7a4e9568f7439f3aba09..3f68a1de20f04655c4b3e4b1c401fcf567401c50 100644 (file)
@@ -117,7 +117,7 @@ statfile_pool_open (statfile_pool_t *pool, char *filename)
        }
        
        if (st.st_size > pool->max) {
-               msg_info ("statfile_pool_open: cannot attach file to pool, too large: %zd", st.st_size);
+               msg_info ("statfile_pool_open: cannot attach file to pool, too large: %zd", (size_t)st.st_size);
                return -1;
        }
 
index 828c3d518e543f5a73773a2f1f1765bafb783a1c..f4978b5b781d1c25458a6dea7bc9255a5179fc53 100644 (file)
@@ -673,7 +673,7 @@ parse_expression (memory_pool_t *pool, char *line)
                        if (c != p) {
                                /* Copy operand */
                                str = memory_pool_alloc (pool, p - c + 1);
-                               strlcpy (str, c, (p - c + 1));
+                               g_strlcpy (str, c, (p - c + 1));
                                insert_expression (pool, &expr, EXPR_OPERAND, 0, str);
                        }
                        if (*p == ')') {
@@ -724,7 +724,7 @@ parse_expression (memory_pool_t *pool, char *line)
        if (c != p) {
                /* Copy operand */
                str = memory_pool_alloc (pool, p - c + 1);
-               strlcpy (str, c, (p - c + 1));
+               g_strlcpy (str, c, (p - c + 1));
                insert_expression (pool, &expr, EXPR_OPERAND, 0, str);
        }
        /* Pop everything from stack */
@@ -750,7 +750,7 @@ open_log (struct config_file *cfg)
                        openlog ("rspamd", LOG_NDELAY | LOG_PID, cfg->log_facility);
                        return 0;
                case RSPAMD_LOG_FILE:
-                       cfg->log_fd = open (cfg->log_file, O_CREAT | O_WRONLY | O_APPEND);
+                       cfg->log_fd = open (cfg->log_file, O_CREAT | O_WRONLY | O_APPEND, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
                        if (cfg->log_fd == -1) {
                                msg_err ("open_log: cannot open desired log file: %s, %m", cfg->log_file);
                                return -1;
@@ -787,16 +787,16 @@ syslog_log_function (const gchar *log_domain, GLogLevelFlags log_level, const gc
 
        if (log_level <= cfg->log_level) {
                if (log_level >= G_LOG_LEVEL_DEBUG) {
-                       syslog (LOG_DEBUG, message);
+                       syslog (LOG_DEBUG, "%s", message);
                }
                else if (log_level >= G_LOG_LEVEL_INFO) {
-                       syslog (LOG_INFO, message);
+                       syslog (LOG_INFO, "%s", message);
                }
                else if (log_level >= G_LOG_LEVEL_WARNING) {
-                       syslog (LOG_WARNING, message);
+                       syslog (LOG_WARNING, "%s", message);
                }
                else if (log_level >= G_LOG_LEVEL_CRITICAL) {
-                       syslog (LOG_ERR, message);
+                       syslog (LOG_ERR, "%s", message);
                }
        }
 }
index 984f4699ca10cc4741360c848ef2aa07dea4d09e..6312d8dd20b4858acb9b44f6e3ddce6d2656a537 100644 (file)
@@ -1,6 +1,3 @@
-#if HAVE_STRLCPY_H
-../src/strlcpy.c
-#endif
 ../src/mem_pool.c
 ../src/url.c
 ../src/util.c
index 467641897719a622c1f529ce8efd2701a1f8494f..6ce983282a180183c139c6c5d1e5136db72ad5cb 100644 (file)
@@ -74,7 +74,7 @@ rspamd_memcached_test_func ()
        ctx->timeout.tv_usec = 0;
        ctx->sock = -1;
        ctx->options = MEMC_OPT_DEBUG;
-       strlcpy (param->key, buf, sizeof (param->key));
+       g_strlcpy (param->key, buf, sizeof (param->key));
        param->buf = buf;
        param->bufsize = strlen (buf);
        ctx->param = param;
index 2c4e7865e690e58f6f25ea4ea6095cc8c52eb50e..47b5279fca317d8afe88fb2a8ba3143a33e3c946 100644 (file)
@@ -1,6 +1,3 @@
-#if HAVE_STRLCPY_H
-../src/strlcpy.c
-#endif
 ../src/mem_pool.c
 ../src/url.c
 ../src/util.c