[dovecot-cvs] dovecot/src/lib buffer.c,1.1,1.2 buffer.h,1.2,1.3 compat.c,1.6,1.7 compat.h,1.14,1.15 data-stack.c,1.10,1.11 hex-binary.c,1.3,1.4 ioloop-poll.c,1.4,1.5 istream-file.c,1.2,1.3 istream.c,1.1,1.2 md5.c,1.5,1.6 mempool-alloconly.c,1.9,1.10 Message-Id: <20021218151544.050E5238CA@danu.procontrol.fi>

cras at procontrol.fi cras at procontrol.fi
Wed Dec 18 17:15:44 EET 2002


Update of /home/cvs/dovecot/src/lib
In directory danu:/tmp/cvs-serv3676/src/lib

Modified Files:
	buffer.c buffer.h compat.c compat.h data-stack.c hex-binary.c 
	ioloop-poll.c istream-file.c istream.c md5.c 
	mempool-alloconly.c mempool-datastack.c mempool-system.c 
	mmap-anon.c network.c network.h ostream-file.c randgen.c 
	strfuncs.c temp-string.c unlink-directory.c 
Log Message:
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
cleanups and a couple of minor bugfixes.



Index: buffer.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/buffer.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- buffer.c	8 Dec 2002 05:23:07 -0000	1.1
+++ buffer.c	18 Dec 2002 15:15:41 -0000	1.2
@@ -21,6 +21,8 @@
     SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
+/* @UNSAFE: whole file */
+
 #include "lib.h"
 #include "buffer.h"
 
@@ -210,6 +212,58 @@
 	if (data_size == 1)
 		buf->w_buffer[pos] = chr;
 	return data_size;
+}
+
+size_t buffer_insert(Buffer *buf, size_t pos,
+		     const void *data, size_t data_size)
+{
+	size_t move_size, size;
+
+	move_size = buf->used - buf->start_pos;
+	i_assert(pos <= move_size);
+	move_size -= pos;
+
+	if (data_size < (size_t)-1 - move_size)
+		size = data_size + move_size;
+	else
+		size = (size_t)-1;
+
+	if (!buffer_check_write(buf, &pos, &size, TRUE))
+		return 0;
+
+	i_assert(size >= move_size);
+	size -= move_size;
+
+	memmove(buf->w_buffer + pos + size, buf->w_buffer + pos, move_size);
+	memcpy(buf->w_buffer + pos, data, size);
+	return size;
+}
+
+size_t buffer_delete(Buffer *buf, size_t pos, size_t size)
+{
+	size_t end_size;
+
+	if (buf->readonly)
+		return 0;
+
+	end_size = buf->used - buf->start_pos;
+	i_assert(pos <= end_size);
+	end_size -= pos;
+
+	if (size < end_size) {
+		/* delete from between */
+		memmove(buf->w_buffer + buf->start_pos + pos,
+			buf->w_buffer + buf->start_pos + pos + size,
+			end_size - size);
+		end_size = size;
+	} else {
+		/* delete the rest of the buffer */
+		size = end_size;
+		end_size = 0;
+	}
+
+	buffer_set_used_size(buf, pos + end_size);
+	return size;
 }
 
 size_t buffer_copy(Buffer *dest, size_t dest_pos,

Index: buffer.h
===================================================================
RCS file: /home/cvs/dovecot/src/lib/buffer.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- buffer.h	17 Dec 2002 02:21:46 -0000	1.2
+++ buffer.h	18 Dec 2002 15:15:41 -0000	1.3
@@ -29,6 +29,12 @@
 /* Append character to buffer, returns 1 if written, 0 if not. */
 size_t buffer_append_c(Buffer *buf, char chr);
 
+/* Insert data to buffer, returns number of bytes inserted. */
+size_t buffer_insert(Buffer *buf, size_t pos,
+		     const void *data, size_t data_size);
+/* Delete data from buffer, returns number of bytes deleted. */
+size_t buffer_delete(Buffer *buf, size_t pos, size_t size);
+
 /* Copy data from buffer to another. The buffers may be same in which case
    it's internal copying, possibly with overlapping positions (ie. memmove()
    like functionality). copy_size may be set to (size_t)-1 to copy the rest of

Index: compat.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/compat.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- compat.c	26 Nov 2002 20:22:09 -0000	1.6
+++ compat.c	18 Dec 2002 15:15:41 -0000	1.7
@@ -34,34 +34,6 @@
 #  define INADDR_NONE INADDR_BROADCAST
 #endif
 
-#ifndef HAVE_MEMMOVE
-void *my_memmove(void *dest, const void *src, size_t size)
-{
-	char *destp = dest;
-        const char *srcp = src;
-
-	if (destp < srcp) {
-		/* dest = 1234, src=234 */
-		destp = dest;
-                srcp = src;
-		while (size > 0) {
-			*destp++ = *srcp++;
-                        size--;
-		}
-	} else if (destp > srcp) {
-		/* dest = 234, src=123 */
-		destp += size-1;
-                srcp += size-1;
-		while (size > 0) {
-			*destp-- = *srcp--;
-                        size--;
-		}
-	}
-
-        return dest;
-}
-#endif
-
 #if !defined (HAVE_STRCASECMP) && !defined (HAVE_STRICMP)
 int my_strcasecmp(const char *s1, const char *s2)
 {

Index: compat.h
===================================================================
RCS file: /home/cvs/dovecot/src/lib/compat.h,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- compat.h	26 Nov 2002 20:22:09 -0000	1.14
+++ compat.h	18 Dec 2002 15:15:41 -0000	1.15
@@ -38,12 +38,6 @@
 #  error I do not know how to compare dev_t
 #endif
 
-/* memmove() */
-#ifndef HAVE_MEMMOVE
-#  define memmove my_memmove
-void *my_memmove(void *dest, const void *src, size_t n);
-#endif
-
 /* strcasecmp(), strncasecmp() */
 #ifndef HAVE_STRCASECMP
 #  ifdef HAVE_STRICMP

Index: data-stack.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/data-stack.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- data-stack.c	18 Dec 2002 01:31:53 -0000	1.10
+++ data-stack.c	18 Dec 2002 15:15:41 -0000	1.11
@@ -23,6 +23,8 @@
     SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
+/* @UNSAFE: whole file */
+
 #include "lib.h"
 #include "data-stack.h"
 

Index: hex-binary.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/hex-binary.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- hex-binary.c	8 Dec 2002 05:23:07 -0000	1.3
+++ hex-binary.c	18 Dec 2002 15:15:41 -0000	1.4
@@ -33,6 +33,8 @@
 	size_t i;
 	int value;
 
+	/* @UNSAFE */
+
 	buf = p = t_malloc(size * 2 + 1);
 	for (i = 0; i < size; i++) {
 		value = data[i] >> 4;

Index: ioloop-poll.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/ioloop-poll.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- ioloop-poll.c	26 Nov 2002 10:28:40 -0000	1.4
+++ ioloop-poll.c	18 Dec 2002 15:15:41 -0000	1.5
@@ -23,6 +23,8 @@
     SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
+/* @UNSAFE: whole file */
+
 #include "lib.h"
 #include "ioloop-internal.h"
 

Index: istream-file.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/istream-file.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- istream-file.c	12 Dec 2002 18:57:47 -0000	1.2
+++ istream-file.c	18 Dec 2002 15:15:41 -0000	1.3
@@ -23,6 +23,8 @@
     SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
+/* @UNSAFE: whole file */
+
 #include "lib.h"
 #include "alarm-hup.h"
 #include "istream-internal.h"

Index: istream.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/istream.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- istream.c	6 Dec 2002 01:09:22 -0000	1.1
+++ istream.c	18 Dec 2002 15:15:41 -0000	1.2
@@ -167,6 +167,7 @@
 		return NULL;
 	}
 
+	/* @UNSAFE */
 	ret_buf = NULL;
 	for (i = _stream->cr_lookup_pos; i < _stream->pos; i++) {
 		if (_stream->buffer[i] == 10) {

Index: md5.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/md5.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- md5.c	4 Nov 2002 07:11:32 -0000	1.5
+++ md5.c	18 Dec 2002 15:15:41 -0000	1.6
@@ -186,6 +186,7 @@
 
 void md5_update(MD5Context *ctx, const void *data, size_t size)
 {
+	/* @UNSAFE */
 	MD5_u32plus saved_lo;
 	unsigned long used, free;
 
@@ -220,6 +221,7 @@
 
 void md5_final(MD5Context *ctx, unsigned char result[16])
 {
+	/* @UNSAFE */
 	unsigned long used, free;
 
 	used = ctx->lo & 0x3f;

Index: mempool-alloconly.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/mempool-alloconly.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- mempool-alloconly.c	26 Nov 2002 20:04:08 -0000	1.9
+++ mempool-alloconly.c	18 Dec 2002 15:15:41 -0000	1.10
@@ -24,6 +24,8 @@
     SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
+/* @UNSAFE: whole file */
+
 #include "lib.h"
 #include "mempool.h"
 

Index: mempool-datastack.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/mempool-datastack.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- mempool-datastack.c	10 Oct 2002 02:01:34 -0000	1.1
+++ mempool-datastack.c	18 Dec 2002 15:15:41 -0000	1.2
@@ -95,6 +95,7 @@
 static void *pool_data_stack_realloc_min(Pool pool __attr_unused__,
 					 void *mem, size_t size)
 {
+	/* @UNSAFE */
 	PoolAlloc *alloc, *new_alloc;
         size_t old_size;
 	unsigned char *rmem;

Index: mempool-system.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/mempool-system.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- mempool-system.c	29 Sep 2002 17:47:41 -0000	1.4
+++ mempool-system.c	18 Dec 2002 15:15:41 -0000	1.5
@@ -23,6 +23,8 @@
     SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
+/* @UNSAFE: whole file */
+
 #include "lib.h"
 #include "mempool.h"
 

Index: mmap-anon.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/mmap-anon.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- mmap-anon.c	13 Nov 2002 05:00:49 -0000	1.6
+++ mmap-anon.c	18 Dec 2002 15:15:41 -0000	1.7
@@ -21,6 +21,8 @@
     SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
+/* @UNSAFE: whole file */
+
 #include "lib.h"
 #include "mmap-util.h"
 

Index: network.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/network.c,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- network.c	12 Dec 2002 18:57:47 -0000	1.13
+++ network.c	18 Dec 2002 15:15:41 -0000	1.14
@@ -120,7 +120,7 @@
 static inline void close_save_errno(int fd)
 {
 	int old_errno = errno;
-	close(fd);
+	(void)close(fd);
 	errno = old_errno;
 }
 
@@ -420,6 +420,7 @@
    to be free'd. Returns 0 = ok, others = error code for net_gethosterror() */
 int net_gethostbyname(const char *addr, IPADDR **ips, int *ips_count)
 {
+	/* @UNSAFE */
 #ifdef HAVE_IPV6
 	union sockaddr_union *so;
 	struct addrinfo hints, *ai, *origai;
@@ -508,25 +509,28 @@
 	return 0;
 }
 
-int net_ip2host(const IPADDR *ip, char *host)
+const char *net_ip2host(const IPADDR *ip)
 {
 #ifdef HAVE_IPV6
-	if (!inet_ntop(ip->family, &ip->ip, host, MAX_IP_LEN))
-		return -1;
+	char host[MAX_IP_LEN+1];
+
+	host[MAX_IP_LEN] = '\0';
+	if (inet_ntop(ip->family, &ip->ip, host, MAX_IP_LEN) == NULL)
+		return NULL;
+
+	return t_strdup(host);
 #else
 	unsigned long ip4;
 
-	if (ip->family != AF_INET) {
-		strcpy(host, "0.0.0.0");
-		return -1;
-	}
+	if (ip->family != AF_INET)
+		return NULL;
 
 	ip4 = ntohl(ip->ip.s_addr);
-	i_snprintf(host, MAX_IP_LEN, "%lu.%lu.%lu.%lu",
-		   (ip4 & 0xff000000UL) >> 24,
-		   (ip4 & 0x00ff0000) >> 16,
-		   (ip4 & 0x0000ff00) >> 8,
-		   (ip4 & 0x000000ff));
+	return t_strdup_printf("%lu.%lu.%lu.%lu",
+			       (ip4 & 0xff000000UL) >> 24,
+			       (ip4 & 0x00ff0000) >> 16,
+			       (ip4 & 0x0000ff00) >> 8,
+			       (ip4 & 0x000000ff));
 #endif
 	return 0;
 }

Index: network.h
===================================================================
RCS file: /home/cvs/dovecot/src/lib/network.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- network.h	12 Dec 2002 18:57:47 -0000	1.7
+++ network.h	18 Dec 2002 15:15:41 -0000	1.8
@@ -89,8 +89,8 @@
 /* Get socket address/port */
 int net_getsockname(int fd, IPADDR *addr, unsigned int *port);
 
-/* IPADDR -> char* translation. `host' must be at least MAX_IP_LEN bytes */
-int net_ip2host(const IPADDR *ip, char *host);
+/* Returns IPADDR as string, or NULL if ip is invalid. */
+const char *net_ip2host(const IPADDR *ip);
 /* char* -> IPADDR translation. */
 int net_host2ip(const char *host, IPADDR *ip);
 

Index: ostream-file.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/ostream-file.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- ostream-file.c	6 Dec 2002 01:09:22 -0000	1.1
+++ ostream-file.c	18 Dec 2002 15:15:41 -0000	1.2
@@ -23,6 +23,8 @@
     SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
+/* @UNSAFE: whole file */
+
 #include "lib.h"
 #include "alarm-hup.h"
 #include "ioloop.h"

Index: randgen.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/randgen.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- randgen.c	12 Dec 2002 18:57:47 -0000	1.5
+++ randgen.c	18 Dec 2002 15:15:41 -0000	1.6
@@ -56,8 +56,8 @@
 	urandom_fd = open("/dev/urandom", O_RDONLY);
 	if (urandom_fd == -1) {
 		if (errno == ENOENT) {
-			i_fatal("/dev/urandom doesn't exist, currently we "
-				"require it");
+			i_fatal("/dev/urandom doesn't exist, "
+				"currently we require it");
 		} else {
 			i_fatal("Can't open /dev/urandom: %m");
 		}

Index: strfuncs.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/strfuncs.c,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -d -r1.18 -r1.19
--- strfuncs.c	26 Nov 2002 13:07:53 -0000	1.18
+++ strfuncs.c	18 Dec 2002 15:15:41 -0000	1.19
@@ -24,6 +24,8 @@
     Boston, MA 02111-1307, USA.
 */
 
+/* @UNSAFE: whole file */
+
 #include "lib.h"
 #include "strfuncs.h"
 

Index: temp-string.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/temp-string.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- temp-string.c	28 Oct 2002 09:46:02 -0000	1.6
+++ temp-string.c	18 Dec 2002 15:15:41 -0000	1.7
@@ -23,6 +23,8 @@
     SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
+/* @UNSAFE: whole file */
+
 #include "lib.h"
 #include "temp-string.h"
 

Index: unlink-directory.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/unlink-directory.c,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- unlink-directory.c	9 Aug 2002 09:15:50 -0000	1.1.1.1
+++ unlink-directory.c	18 Dec 2002 15:15:41 -0000	1.2
@@ -51,15 +51,17 @@
 
 		i_snprintf(path, sizeof(path), "%s/%s", dir, d->d_name);
 
-		if (unlink(path) == -1) {
+		if (unlink(path) == -1 && errno != ENOENT) {
+			int old_errno = errno;
+
 			if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
 				if (!unlink_directory(path))
 					return FALSE;
 			} else {
 				/* so it wasn't a directory, unlink() again
 				   to get correct errno */
-				if (unlink(path) == -1)
-					return FALSE;
+				errno = old_errno;
+				return FALSE;
 			}
 		}
 	}




More information about the dovecot-cvs mailing list