summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRay Lai <ray@cvs.openbsd.org>2006-08-16 07:39:16 +0000
committerRay Lai <ray@cvs.openbsd.org>2006-08-16 07:39:16 +0000
commitdd109580266c56df7b5feb24de5100e6d457579f (patch)
treeb386c6f592769599dd4e9d5d096971816e90aa69
parent65bc33032d45046943c4d18a409e2c0e7d02b696 (diff)
Improve rcs_buf_load() by setting errno appropriately on failure and
never print errors or quit on error. Fix usages of rcs_buf_load() and rcs_set_description. Also plug an fd leak. OK xsa@
-rw-r--r--usr.bin/rcs/buf.c38
-rw-r--r--usr.bin/rcs/ci.c14
-rw-r--r--usr.bin/rcs/rcsprog.c24
-rw-r--r--usr.bin/rcs/rcsutil.c9
-rw-r--r--usr.bin/rcs/rcsutil.h4
5 files changed, 63 insertions, 26 deletions
diff --git a/usr.bin/rcs/buf.c b/usr.bin/rcs/buf.c
index 525f7000c01..bb52670ffb2 100644
--- a/usr.bin/rcs/buf.c
+++ b/usr.bin/rcs/buf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: buf.c,v 1.7 2006/08/02 03:28:50 ray Exp $ */
+/* $OpenBSD: buf.c,v 1.8 2006/08/16 07:39:15 ray Exp $ */
/*
* Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
* All rights reserved.
@@ -81,7 +81,8 @@ rcs_buf_alloc(size_t len, u_int flags)
*
* Open the file specified by <path> and load all of its contents into a
* buffer.
- * Returns the loaded buffer on success.
+ * Returns the loaded buffer on success or NULL on failure.
+ * Sets errno on error.
*/
BUF *
rcs_buf_load(const char *path, u_int flags)
@@ -93,28 +94,45 @@ rcs_buf_load(const char *path, u_int flags)
struct stat st;
BUF *buf;
- if ((fd = open(path, O_RDONLY, 0600)) == -1) {
- warn("%s", path);
- return (NULL);
- }
+ buf = NULL;
+
+ if ((fd = open(path, O_RDONLY, 0600)) == -1)
+ goto out;
if (fstat(fd, &st) == -1)
- err(1, "%s", path);
+ goto out;
- buf = rcs_buf_alloc((size_t)st.st_size, flags);
+ if (st.st_size > SIZE_MAX) {
+ errno = EFBIG;
+ goto out;
+ }
+ buf = rcs_buf_alloc(st.st_size, flags);
for (bp = buf->cb_cur; ; bp += (size_t)ret) {
len = SIZE_LEFT(buf);
ret = read(fd, bp, len);
if (ret == -1) {
+ int saved_errno;
+
+ saved_errno = errno;
rcs_buf_free(buf);
- err(1, "rcs_buf_load");
+ buf = NULL;
+ errno = saved_errno;
+ goto out;
} else if (ret == 0)
break;
buf->cb_len += (size_t)ret;
}
- (void)close(fd);
+out:
+ if (fd != -1) {
+ int saved_errno;
+
+ /* We may want to preserve errno here. */
+ saved_errno = errno;
+ (void)close(fd);
+ errno = saved_errno;
+ }
return (buf);
}
diff --git a/usr.bin/rcs/ci.c b/usr.bin/rcs/ci.c
index 2e9995225c7..3491d0ee06c 100644
--- a/usr.bin/rcs/ci.c
+++ b/usr.bin/rcs/ci.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ci.c,v 1.184 2006/08/02 05:16:18 ray Exp $ */
+/* $OpenBSD: ci.c,v 1.185 2006/08/16 07:39:15 ray Exp $ */
/*
* Copyright (c) 2005, 2006 Niall O'Higgins <niallo@openbsd.org>
* All rights reserved.
@@ -266,8 +266,9 @@ checkin_main(int argc, char **argv)
if (pb.file == NULL)
errx(1, "failed to open rcsfile `%s'", pb.fpath);
- if (pb.flags & DESCRIPTION)
- rcs_set_description(pb.file, pb.description);
+ if ((pb.flags & DESCRIPTION) &&
+ rcs_set_description(pb.file, pb.description) == -1)
+ err(1, "%s", pb.file);
if (!(pb.flags & QUIET))
(void)fprintf(stderr,
@@ -611,8 +612,11 @@ checkin_init(struct checkin_params *pb)
goto skipdesc;
/* Get description from user */
- if (pb->description == NULL)
- rcs_set_description(pb->file, NULL);
+ if (pb->description == NULL &&
+ rcs_set_description(pb->file, NULL) == -1) {
+ warn("%s", pb->file);
+ goto fail;
+ }
skipdesc:
diff --git a/usr.bin/rcs/rcsprog.c b/usr.bin/rcs/rcsprog.c
index dcb72fe07e6..0c6c45a766c 100644
--- a/usr.bin/rcs/rcsprog.c
+++ b/usr.bin/rcs/rcsprog.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rcsprog.c,v 1.131 2006/05/29 21:17:44 ray Exp $ */
+/* $OpenBSD: rcsprog.c,v 1.132 2006/08/16 07:39:15 ray Exp $ */
/*
* Copyright (c) 2005 Jean-Francois Brousseau <jfb@openbsd.org>
* All rights reserved.
@@ -312,13 +312,25 @@ rcs_main(int argc, char **argv)
if (!(rcsflags & QUIET))
(void)fprintf(stderr, "RCS file: %s\n", fpath);
- if ((file = rcs_open(fpath, fd, flags, fmode)) == NULL)
+ if ((file = rcs_open(fpath, fd, flags, fmode)) == NULL) {
+ close(fd);
continue;
+ }
- if (rcsflags & DESCRIPTION)
- rcs_set_description(file, descfile);
- else if (flags & RCS_CREATE)
- rcs_set_description(file, NULL);
+ if (rcsflags & DESCRIPTION) {
+ if (rcs_set_description(file, descfile) == -1) {
+ warn("%s", descfile);
+ rcs_close(file);
+ continue;
+ }
+ }
+ else if (flags & RCS_CREATE) {
+ if (rcs_set_description(file, NULL) == -1) {
+ warn("stdin");
+ rcs_close(file);
+ continue;
+ }
+ }
if (rcsflags & PRESERVETIME)
rcs_mtime = rcs_get_mtime(file);
diff --git a/usr.bin/rcs/rcsutil.c b/usr.bin/rcs/rcsutil.c
index 3a7b3da11f8..6c25e21acd2 100644
--- a/usr.bin/rcs/rcsutil.c
+++ b/usr.bin/rcs/rcsutil.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rcsutil.c,v 1.17 2006/07/08 09:14:03 ray Exp $ */
+/* $OpenBSD: rcsutil.c,v 1.18 2006/08/16 07:39:15 ray Exp $ */
/*
* Copyright (c) 2005, 2006 Joris Vink <joris@openbsd.org>
* Copyright (c) 2006 Xavier Santolaria <xsa@openbsd.org>
@@ -437,8 +437,9 @@ rcs_rev_select(RCSFILE *file, const char *range)
* If <in> starts with a `-', <in> is taken as the description.
* Otherwise <in> is the name of the file containing the description.
* If <in> is NULL, the description is read from stdin.
+ * Returns 0 on success, -1 on failure, setting errno.
*/
-void
+int
rcs_set_description(RCSFILE *file, const char *in)
{
BUF *bp;
@@ -449,7 +450,8 @@ rcs_set_description(RCSFILE *file, const char *in)
/* Description is in file <in>. */
if (in != NULL && *in != '-') {
- bp = rcs_buf_load(in, BUF_AUTOEXT);
+ if ((bp = rcs_buf_load(in, BUF_AUTOEXT)) == NULL)
+ return (-1);
rcs_buf_putc(bp, '\0');
content = rcs_buf_release(bp);
/* Description is in <in>. */
@@ -462,6 +464,7 @@ rcs_set_description(RCSFILE *file, const char *in)
rcs_desc_set(file, content);
xfree(content);
+ return (0);
}
/*
diff --git a/usr.bin/rcs/rcsutil.h b/usr.bin/rcs/rcsutil.h
index 84083cdcef8..d65e8a4f319 100644
--- a/usr.bin/rcs/rcsutil.h
+++ b/usr.bin/rcs/rcsutil.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: rcsutil.h,v 1.7 2006/06/03 03:05:10 niallo Exp $ */
+/* $OpenBSD: rcsutil.h,v 1.8 2006/08/16 07:39:15 ray Exp $ */
/*
* Copyright (c) 2006 Xavier Santolaria <xsa@openbsd.org>
* All rights reserved.
@@ -56,7 +56,7 @@ time_t rcs_get_mtime(RCSFILE *);
RCSNUM *rcs_getrevnum(const char *, RCSFILE *);
char *rcs_prompt(const char *);
u_int rcs_rev_select(RCSFILE *, const char *);
-void rcs_set_description(RCSFILE *, const char *);
+int rcs_set_description(RCSFILE *, const char *);
void rcs_set_rev(const char *, RCSNUM **);
void rcs_setrevstr(char **, char *);
void rcs_setrevstr2(char **, char **, char *);