[dovecot-cvs] dovecot/src/lib hostpid.c,1.3,1.4 mempool-alloconly.c,1.10,1.11 network.c,1.14,1.15 ostream.c,1.1,1.2 ostream.h,1.1,1.2 process-title.c,1.2,1.3 restrict-access.c,1.6,1.7 strfuncs.c,1.19,1.20 strfuncs.h,1.9,1.10 unlink-directory.c,1.2,1.3 Message-Id: <20021219010237.74222238C2@danu.procontrol.fi>
cras at procontrol.fi
cras at procontrol.fi
Thu Dec 19 03:02:37 EET 2002
- Previous message: [dovecot-cvs] dovecot/src/lib-storage/index index-fetch-section.c,1.21,1.22 index-fetch.c,1.35,1.36 index-search.c,1.50,1.51 index-sort.c,1.3,1.4
- Next message: [dovecot-cvs] dovecot/src/lib-storage/index/mbox mbox-list.c,1.4,1.5 mbox-save.c,1.26,1.27 mbox-storage.c,1.20,1.21
- Messages sorted by:
[ date ]
[ thread ]
[ subject ]
[ author ]
Update of /home/cvs/dovecot/src/lib
In directory danu:/tmp/cvs-serv11467/lib
Modified Files:
hostpid.c mempool-alloconly.c network.c ostream.c ostream.h
process-title.c restrict-access.c strfuncs.c strfuncs.h
unlink-directory.c unlink-lockfiles.c
Log Message:
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Added str_path() and str_ppath() functions. i_snprintf() now returns only -1
or 0 depending on if buffer got full. dec2str() returns the string allocated
from data stack. Instead of just casting to (long) or (int), we now use
dec2str() with printf-like functions. Added o_stream_send_str(). Added
strocpy() and replaced all strcpy()s and strncpy()s with it.
Pretty much untested, hope it doesn't break too badly :)
Index: hostpid.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/hostpid.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- hostpid.c 26 Nov 2002 13:07:53 -0000 1.3
+++ hostpid.c 19 Dec 2002 01:02:35 -0000 1.4
@@ -36,15 +36,15 @@
static char hostname[256], pid[MAX_INT_STRLEN];
if (my_hostname == NULL) {
- hostname[sizeof(hostname)-1] = '\0';
if (gethostname(hostname, sizeof(hostname)-1) == -1)
- strcpy(hostname, "unknown");
+ strocpy(hostname, "unknown", sizeof(hostname));
+ hostname[sizeof(hostname)-1] = '\0';
my_hostname = hostname;
}
if (my_pid == NULL) {
- dec2str(pid, sizeof(pid), getpid());
+ strocpy(pid, dec2str(getpid()), sizeof(pid));
my_pid = pid;
}
}
Index: mempool-alloconly.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/mempool-alloconly.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- mempool-alloconly.c 18 Dec 2002 15:15:41 -0000 1.10
+++ mempool-alloconly.c 19 Dec 2002 01:02:35 -0000 1.11
@@ -103,10 +103,9 @@
i_panic("pool_alloconly_create(): Out of memory");
apool->pool = static_alloconly_pool;
apool->refcount = 1;
+ memcpy(apool->name, name, len+1);
block_alloc(apool, size);
-
- strcpy(apool->name, name);
return (Pool) apool;
}
Index: network.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/network.c,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- network.c 18 Dec 2002 15:15:41 -0000 1.14
+++ network.c 19 Dec 2002 01:02:35 -0000 1.15
@@ -181,7 +181,9 @@
struct sockaddr_un sa;
int fd, ret;
- if (strlen(path) > sizeof(sa.sun_path)-1) {
+ memset(&sa, 0, sizeof(sa));
+ sa.sun_family = AF_UNIX;
+ if (strocpy(sa.sun_path, path, sizeof(sa.sun_path)) < 0) {
/* too long path */
errno = EINVAL;
return -1;
@@ -196,10 +198,6 @@
net_set_nonblock(fd, TRUE);
/* connect */
- memset(&sa, 0, sizeof(sa));
- sa.sun_family = AF_UNIX;
- strcpy(sa.sun_path, path);
-
ret = connect(fd, (struct sockaddr *) &sa, sizeof(sa));
if (ret < 0 && errno != EINPROGRESS) {
close_save_errno(fd);
@@ -312,7 +310,9 @@
struct sockaddr_un sa;
int fd;
- if (strlen(path) > sizeof(sa.sun_path)-1) {
+ memset(&sa, 0, sizeof(sa));
+ sa.sun_family = AF_UNIX;
+ if (strocpy(sa.sun_path, path, sizeof(sa.sun_path)) < 0) {
/* too long path */
errno = EINVAL;
return -1;
@@ -327,10 +327,6 @@
net_set_nonblock(fd, TRUE);
/* bind */
- memset(&sa, 0, sizeof(sa));
- sa.sun_family = AF_UNIX;
- strcpy(sa.sun_path, path);
-
if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) == 0) {
/* start listening */
if (listen(fd, LISTEN_BACKLOG) == 0)
Index: ostream.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/ostream.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- ostream.c 6 Dec 2002 01:09:22 -0000 1.1
+++ ostream.c 19 Dec 2002 01:02:35 -0000 1.2
@@ -105,6 +105,11 @@
return _stream->send(_stream, data, size);
}
+ssize_t o_stream_send_str(OStream *stream, const char *str)
+{
+ return o_stream_send(stream, str, strlen(str));
+}
+
off_t o_stream_send_istream(OStream *outstream, IStream *instream)
{
_OStream *_outstream = outstream->real_stream;
Index: ostream.h
===================================================================
RCS file: /home/cvs/dovecot/src/lib/ostream.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- ostream.h 6 Dec 2002 01:09:22 -0000 1.1
+++ ostream.h 19 Dec 2002 01:02:35 -0000 1.2
@@ -45,6 +45,7 @@
int o_stream_seek(OStream *stream, uoff_t offset);
/* Returns number of bytes sent or buffered, or -1 if disconnected */
ssize_t o_stream_send(OStream *stream, const void *data, size_t size);
+ssize_t o_stream_send_str(OStream *stream, const char *str);
/* Send data from input stream. Returns number of bytes sent, or -1 if error.
Note that this function may block if either instream or outstream is
blocking. */
Index: process-title.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/process-title.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- process-title.c 18 Dec 2002 04:00:01 -0000 1.2
+++ process-title.c 19 Dec 2002 01:02:35 -0000 1.3
@@ -52,10 +52,8 @@
environ = p;
for (i = 0; envp[i] != NULL; i++) {
- if ((environ[i] = malloc(strlen(envp[i]) + 1)) == NULL)
+ if ((environ[i] = strdup(envp[i])) == NULL)
i_panic("malloc() failed: %m");
-
- strcpy(environ[i], envp[i]);
}
environ[i] = NULL;
@@ -63,14 +61,12 @@
Calculate the max. size for process name with by checking the
address for last environment and it's length. */
process_title = argv[0];
- process_title_len = (size_t) (envp[i-1] - argv[0]) +
- strlen(envp[i-1]);
+ process_title_len = (size_t) (envp[i-1] - argv[0]) + strlen(envp[i-1]);
}
static void linux_proctitle_set(const char *title)
{
- strncpy(process_title, title, process_title_len);
- process_title[process_title_len] = '\0';
+ strocpy(process_title, title, process_title_len);
}
#endif
Index: restrict-access.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/restrict-access.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- restrict-access.c 18 Dec 2002 04:00:01 -0000 1.6
+++ restrict-access.c 19 Dec 2002 01:02:35 -0000 1.7
@@ -38,8 +38,8 @@
if (chroot_dir != NULL && *chroot_dir != '\0')
env_put(t_strconcat("RESTRICT_CHROOT=", chroot_dir, NULL));
- env_put(t_strdup_printf("RESTRICT_SETUID=%ld", (long) uid));
- env_put(t_strdup_printf("RESTRICT_SETGID=%ld", (long) gid));
+ env_put(t_strdup_printf("RESTRICT_SETUID=%s", dec2str(uid)));
+ env_put(t_strdup_printf("RESTRICT_SETGID=%s", dec2str(gid)));
}
void restrict_access_by_env(int disallow_root)
@@ -69,7 +69,7 @@
gid = env == NULL ? 0 : (gid_t) atol(env);
if (gid != 0 && (gid != getgid() || gid != getegid())) {
if (setgid(gid) != 0)
- i_fatal("setgid(%ld) failed: %m", (long) gid);
+ i_fatal("setgid(%s) failed: %m", dec2str(gid));
env = getenv("RESTRICT_USER");
if (env == NULL) {
@@ -77,8 +77,8 @@
(void)setgroups(1, &gid);
} else {
if (initgroups(env, gid) != 0) {
- i_fatal("initgroups(%s, %ld) failed: %m",
- env, (long) gid);
+ i_fatal("initgroups(%s, %s) failed: %m",
+ env, dec2str(gid));
}
}
}
@@ -88,7 +88,7 @@
uid = env == NULL ? 0 : (uid_t) atol(env);
if (uid != 0) {
if (setuid(uid) != 0)
- i_fatal("setuid(%ld) failed: %m", (long) uid);
+ i_fatal("setuid(%s) failed: %m", dec2str(uid));
}
/* verify that we actually dropped the privileges */
Index: strfuncs.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/strfuncs.c,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -d -r1.19 -r1.20
--- strfuncs.c 18 Dec 2002 15:15:41 -0000 1.19
+++ strfuncs.c 19 Dec 2002 01:02:35 -0000 1.20
@@ -387,34 +387,35 @@
return fmt;
}
-int i_snprintf(char *str, size_t max_chars, const char *format, ...)
+int i_snprintf(char *dest, size_t max_chars, const char *format, ...)
{
#ifdef HAVE_VSNPRINTF
va_list args;
int ret;
- i_assert(str != NULL);
+ i_assert(dest != NULL);
i_assert(max_chars < INT_MAX);
i_assert(format != NULL);
t_push();
va_start(args, format);
- ret = vsnprintf(str, max_chars, printf_string_fix_format(format), args);
+ ret = vsnprintf(dest, max_chars,
+ printf_string_fix_format(format), args);
va_end(args);
t_pop();
if (ret < 0 || (size_t)ret >= max_chars) {
- str[max_chars-1] = '\0';
- ret = strlen(str);
+ dest[max_chars-1] = '\0';
+ return -1;
}
- return ret;
+ return 0;
#else
char *buf;
va_list args;
- int len;
+ int len, ret;
- i_assert(str != NULL);
+ i_assert(dest != NULL);
i_assert(max_chars < INT_MAX);
i_assert(format != NULL);
@@ -426,14 +427,23 @@
va_end(args);
len = vsprintf(buf, format, args);
- if (len >= (int)max_chars)
+ if (len < 0) {
+ /* some error occured */
+ len = 0;
+ ret = -1;
+ } else if ((size_t)len >= max_chars) {
+ /* too large */
len = max_chars-1;
+ ret = -1;
+ } else {
+ ret = 0;
+ }
- memcpy(str, buf, len);
- str[len] = '\0';
+ memcpy(dest, buf, len);
+ dest[len] = '\0';
t_pop();
- return len;
+ return ret;
#endif
}
@@ -738,6 +748,65 @@
return TRUE;
}
+int strocpy(char *dest, const char *src, size_t dstsize)
+{
+ if (dstsize == 0)
+ return -1;
+
+ while (*src != '\0' && dstsize > 1) {
+ *dest++ = *src++;
+ dstsize--;
+ }
+
+ *dest++ = '\0';
+ return *src == '\0' ? 0 : -1;
+}
+
+int str_path(char *dest, size_t dstsize, const char *dir, const char *file)
+{
+ size_t dirlen, filelen;
+
+ dirlen = strlen(dir);
+ filelen = strlen(file);
+
+ if (dirlen+1+filelen >= dstsize) {
+ if (dstsize > 0)
+ *dest = '\0';
+ errno = ENAMETOOLONG;
+ return -1;
+ }
+
+ memcpy(dest, dir, dirlen);
+ dest[dirlen] = '/';
+ memcpy(dest + dirlen + 1, file, filelen);
+ dest[dirlen + 1 + filelen] = '\0';
+ return 0;
+}
+
+int str_ppath(char *dest, size_t dstsize, const char *dir,
+ const char *file_prefix, const char *file)
+{
+ size_t dirlen, prefixlen, filelen;
+
+ dirlen = strlen(dir);
+ prefixlen = strlen(file_prefix);
+ filelen = strlen(file);
+
+ if (dirlen+1+prefixlen+filelen >= dstsize) {
+ if (dstsize > 0)
+ *dest = '\0';
+ errno = ENAMETOOLONG;
+ return -1;
+ }
+
+ memcpy(dest, dir, dirlen);
+ dest[dirlen] = '/';
+ memcpy(dest + dirlen + 1, file_prefix, prefixlen);
+ memcpy(dest + dirlen + prefixlen + 1, file, filelen);
+ dest[dirlen + 1 + prefixlen + filelen] = '\0';
+ return 0;
+}
+
char *str_ucase(char *str)
{
char *p;
@@ -756,26 +825,7 @@
return str;
}
-char *i_strtoken(char **str, char delim)
-{
- char *ret;
-
- if (*str == NULL || **str == '\0')
- return NULL;
-
- ret = *str;
- while (**str != '\0') {
- if (**str == delim) {
- **str = '\0';
- (*str)++;
- break;
- }
- (*str)++;
- }
- return ret;
-}
-
-void string_remove_escapes(char *str)
+void str_remove_escapes(char *str)
{
char *dest;
@@ -851,65 +901,19 @@
return (char *const *) array;
}
-const char *t_strjoin_replace(char *const args[], char separator,
- int replacearg, const char *replacedata)
-{
- const char *arg;
- char *data;
- size_t alloc_len, arg_len, full_len;
- int i;
-
- if (args[0] == NULL)
- return NULL;
-
- alloc_len = 512; full_len = 0;
- data = t_buffer_get(alloc_len);
- for (i = 0; args[i] != NULL; i++) {
- arg = i == replacearg ? replacedata : args[i];
- arg_len = strlen(arg);
-
- if (full_len + arg_len+1 >= alloc_len) {
- alloc_len = nearest_power(full_len + arg_len+1);
- data = t_buffer_reget(data, alloc_len);
- }
-
- memcpy(data+full_len, arg, arg_len);
- full_len += arg_len;
-
- data[full_len++] = separator;
- }
- data[full_len-1] = '\0';
-
- t_buffer_alloc(full_len);
- return data;
-}
-
-static size_t dec2str_recurse(char *buffer, size_t pos, size_t size,
- uintmax_t number)
-{
- if (number == 0)
- return 0;
-
- pos = dec2str_recurse(buffer, pos, size-1, number / 10);
- if (pos < size)
- buffer[pos] = '0' + (number % 10);
- return pos + 1;
-}
-
-void dec2str(char *buffer, size_t size, uintmax_t number)
+const char *dec2str(uintmax_t number)
{
- size_t pos;
-
- if (size == 0)
- return;
-
- pos = dec2str_recurse(buffer, 0, size, number);
+ char *buffer;
+ int pos;
- if (pos == 0 && size > 1) {
- /* we wrote nothing, because number is 0 */
- buffer[0] = '0';
- pos++;
- }
+ pos = MAX_INT_STRLEN;
+ buffer = t_malloc(pos);
- buffer[pos < size ? pos : size-1] = '\0';
+ buffer[--pos] = '\0';
+ do {
+ buffer[--pos] = (number % 10) + '0';
+ number /= 10;
+ } while (number != 0 && pos >= 0);
+ i_assert(pos >= 0);
+ return buffer + pos;
}
Index: strfuncs.h
===================================================================
RCS file: /home/cvs/dovecot/src/lib/strfuncs.h,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- strfuncs.h 26 Nov 2002 13:07:53 -0000 1.9
+++ strfuncs.h 19 Dec 2002 01:02:35 -0000 1.10
@@ -4,9 +4,13 @@
#define is_empty_str(str) \
((str) == NULL || (str)[0] == '\0')
+#define MAX_INT_STRLEN ((sizeof(uintmax_t) * CHAR_BIT + 2) / 3 + 1)
+
size_t printf_string_upper_bound(const char *format, va_list args);
const char *printf_string_fix_format(const char *fmt);
-int i_snprintf(char *str, size_t max_chars, const char *format, ...)
+
+/* Returns -1 if dest wasn't large enough, 0 if not. */
+int i_snprintf(char *dest, size_t max_chars, const char *format, ...)
__attr_format__(3, 4);
char *p_strdup(Pool pool, const char *str);
@@ -37,10 +41,19 @@
Stop when `end_char' is found from string. */
int is_numeric(const char *str, char end_char);
+/* like strlcpy(), but return -1 if buffer was overflown, 0 if not. */
+int strocpy(char *dest, const char *src, size_t dstsize);
+
+/* Print given directory and file to dest buffer, separated with '/'.
+ If destination buffer is too small, it's set to empty string and errno is
+ set to ENAMETOOLONG. Retuns -1 if buffer is too small, or 0 if not. */
+int str_path(char *dest, size_t dstsize, const char *dir, const char *file);
+int str_ppath(char *dest, size_t dstsize, const char *dir,
+ const char *file_prefix, const char *file);
+
char *str_ucase(char *str);
char *str_lcase(char *str);
-char *i_strtoken(char **str, char delim);
-void string_remove_escapes(char *str);
+void str_remove_escapes(char *str);
/* returns number of items in array */
int strarray_length(char *const array[]);
@@ -48,15 +61,9 @@
int strarray_find(char *const array[], const char *item);
/* seprators is an array of separator characters, not a separator string. */
-char * const *t_strsplit(const char *data, const char *separators);
-
-#define t_strjoin(args, separator) \
- t_strjoin_replace(args, separator, -1, NULL)
-const char *t_strjoin_replace(char *const args[], char separator,
- int replacearg, const char *replacedata);
+char *const *t_strsplit(const char *data, const char *separators);
-#define MAX_INT_STRLEN ((sizeof(uintmax_t) * CHAR_BIT + 2) / 3 + 1)
-void dec2str(char *buffer, size_t size, uintmax_t number);
+const char *dec2str(uintmax_t number);
/* INTERNAL */
const char *temp_strconcat(const char *str1, va_list args, size_t *ret_len);
Index: unlink-directory.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/unlink-directory.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- unlink-directory.c 18 Dec 2002 15:15:41 -0000 1.2
+++ unlink-directory.c 19 Dec 2002 01:02:35 -0000 1.3
@@ -35,7 +35,7 @@
DIR *dirp;
struct dirent *d;
struct stat st;
- char path[1024];
+ char path[PATH_MAX];
dirp = opendir(dir);
if (dirp == NULL)
@@ -49,7 +49,8 @@
continue;
}
- i_snprintf(path, sizeof(path), "%s/%s", dir, d->d_name);
+ if (str_path(path, sizeof(path), dir, d->d_name) < 0)
+ return FALSE;
if (unlink(path) == -1 && errno != ENOENT) {
int old_errno = errno;
Index: unlink-lockfiles.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/unlink-lockfiles.c,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- unlink-lockfiles.c 9 Aug 2002 09:15:49 -0000 1.1.1.1
+++ unlink-lockfiles.c 19 Dec 2002 01:02:35 -0000 1.2
@@ -38,7 +38,7 @@
DIR *dirp;
struct dirent *d;
struct stat st;
- char path[1024];
+ char path[PATH_MAX];
unsigned int pidlen, otherlen;
/* check for any invalid access files */
@@ -61,15 +61,14 @@
if (kill(atoi(fname+pidlen), 0) == 0)
continue; /* valid */
- i_snprintf(path, sizeof(path), "%s/%s", dir, fname);
- (void)unlink(path);
+ if (str_path(path, sizeof(path), dir, fname) == 0)
+ (void)unlink(path);
} else if (otherprefix != 0 &&
strncmp(fname, otherprefix, otherlen) == 0) {
- i_snprintf(path, sizeof(path), "%s/%s", dir, fname);
- if (stat(path, &st) == 0 &&
- st.st_mtime < other_min_time) {
+ if (str_path(path, sizeof(path), dir, fname) == 0 &&
+ stat(path, &st) == 0 &&
+ st.st_mtime < other_min_time)
(void)unlink(path);
- }
}
}
- Previous message: [dovecot-cvs] dovecot/src/lib-storage/index index-fetch-section.c,1.21,1.22 index-fetch.c,1.35,1.36 index-search.c,1.50,1.51 index-sort.c,1.3,1.4
- Next message: [dovecot-cvs] dovecot/src/lib-storage/index/mbox mbox-list.c,1.4,1.5 mbox-save.c,1.26,1.27 mbox-storage.c,1.20,1.21
- Messages sorted by:
[ date ]
[ thread ]
[ subject ]
[ author ]
More information about the dovecot-cvs
mailing list