From 42b81716ece887b0011b1e40b0101ad37598997e Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 3 Dec 2008 19:48:15 +0300 Subject: [PATCH] * Remove strlcpy code and replace it with glib alternative * Polish code and remove bugs found --- Makefile.in | 2 +- compat/strlcpy.c | 70 ------------------------------------ compat/strlcpy.h | 8 ----- configure | 11 +----- perl/rspamd.xs | 11 +++--- src/controller.c | 2 +- src/main.c | 2 +- src/memcached.c | 19 +++++++--- src/plugins/surbl.c | 21 ++++++++--- src/protocol.c | 4 +-- src/statfile.c | 2 +- src/util.c | 14 ++++---- test/.depends | 3 -- test/rspamd_memcached_test.c | 2 +- utils/.depends | 3 -- 15 files changed, 52 insertions(+), 122 deletions(-) delete mode 100644 compat/strlcpy.c delete mode 100644 compat/strlcpy.h diff --git a/Makefile.in b/Makefile.in index 0bb5380d0..fc7078d1a 100644 --- a/Makefile.in +++ b/Makefile.in @@ -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 index 1c0805548..000000000 --- a/compat/strlcpy.c +++ /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 - * 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 - -#include -#include - -/* - * 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 index 74d772d31..000000000 --- a/compat/strlcpy.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef STRLCPY_H -#define STRLCPY_H - -#include - -size_t strlcpy(char *, const char*, size_t); - -#endif diff --git a/configure b/configure index 6c8eb4bf2..08d83c713 100755 --- a/configure +++ b/configure @@ -485,7 +485,7 @@ check_perl() echo "#include " > autotest.c echo "#include " >> autotest.c echo "#include " >> 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 diff --git a/perl/rspamd.xs b/perl/rspamd.xs index 1c36b06e0..c96401033 100644 --- a/perl/rspamd.xs +++ b/perl/rspamd.xs @@ -9,6 +9,7 @@ #include #include #include +#include #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; diff --git a/src/controller.c b/src/controller.c index 393bc2347..b85db1b2d 100644 --- a/src/controller.c +++ b/src/controller.c @@ -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 { diff --git a/src/main.c b/src/main.c index 445b0e42b..64ac455a7 100644 --- a/src/main.c +++ b/src/main.c @@ -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; diff --git a/src/memcached.c b/src/memcached.c index 05ae16617..22dd9fbb3 100644 --- a/src/memcached.c +++ b/src/memcached.c @@ -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); diff --git a/src/plugins/surbl.c b/src/plugins/surbl.c index c90a8419e..07e2376bf 100644 --- a/src/plugins/surbl.c +++ b/src/plugins/surbl.c @@ -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 (¶m->ev, param->sock, EV_READ | EV_PERSIST | EV_TIMEOUT, redirector_callback, (void *)param); event_add (¶m->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 (¶m->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 { diff --git a/src/protocol.c b/src/protocol.c index b259f6cd9..027e3a91a 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -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); diff --git a/src/statfile.c b/src/statfile.c index fc7ebfc7a..3f68a1de2 100644 --- a/src/statfile.c +++ b/src/statfile.c @@ -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; } diff --git a/src/util.c b/src/util.c index 828c3d518..f4978b5b7 100644 --- a/src/util.c +++ b/src/util.c @@ -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); } } } diff --git a/test/.depends b/test/.depends index 984f4699c..6312d8dd2 100644 --- a/test/.depends +++ b/test/.depends @@ -1,6 +1,3 @@ -#if HAVE_STRLCPY_H -../src/strlcpy.c -#endif ../src/mem_pool.c ../src/url.c ../src/util.c diff --git a/test/rspamd_memcached_test.c b/test/rspamd_memcached_test.c index 467641897..6ce983282 100644 --- a/test/rspamd_memcached_test.c +++ b/test/rspamd_memcached_test.c @@ -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; diff --git a/utils/.depends b/utils/.depends index 2c4e7865e..47b5279fc 100644 --- a/utils/.depends +++ b/utils/.depends @@ -1,6 +1,3 @@ -#if HAVE_STRLCPY_H -../src/strlcpy.c -#endif ../src/mem_pool.c ../src/url.c ../src/util.c -- 2.39.5