summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorjsing <>2016-11-04 15:59:16 +0000
committerjsing <>2016-11-04 15:59:16 +0000
commit391f8ce21bb7929810460a73e2fde2c80540848d (patch)
treeb2b307858fd1a6d1e8ed6bf4bcd0849f380366d0 /src/lib
parenta095fd48b5773625cbe19b8a6c4d85902eafec6d (diff)
downloadopenbsd-391f8ce21bb7929810460a73e2fde2c80540848d.tar.gz
openbsd-391f8ce21bb7929810460a73e2fde2c80540848d.tar.bz2
openbsd-391f8ce21bb7929810460a73e2fde2c80540848d.zip
Address some signed vs unsigned warnings and check that an integer value
is positive before passing it to several functions as a size_t. Additionally, in tls_load_file() there is not much point using calloc(), when we're immediately reading into the buffer (having an extra byte for NUL termination seems pointless given the API). ok beck@ miod@
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libtls/tls_util.c18
1 files changed, 14 insertions, 4 deletions
diff --git a/src/lib/libtls/tls_util.c b/src/lib/libtls/tls_util.c
index 8cf3345caf..dbb2d170d5 100644
--- a/src/lib/libtls/tls_util.c
+++ b/src/lib/libtls/tls_util.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls_util.c,v 1.4 2016/10/03 04:13:58 bcook Exp $ */ 1/* $OpenBSD: tls_util.c,v 1.5 2016/11/04 15:59:16 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4 * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org> 4 * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
@@ -89,13 +89,19 @@ tls_host_port(const char *hostport, char **host, char **port)
89static int 89static int
90tls_password_cb(char *buf, int size, int rwflag, void *u) 90tls_password_cb(char *buf, int size, int rwflag, void *u)
91{ 91{
92 size_t len; 92 size_t len;
93
94 if (size < 0)
95 return (0);
96
93 if (u == NULL) { 97 if (u == NULL) {
94 memset(buf, 0, size); 98 memset(buf, 0, size);
95 return (0); 99 return (0);
96 } 100 }
101
97 if ((len = strlcpy(buf, u, size)) >= (size_t)size) 102 if ((len = strlcpy(buf, u, size)) >= (size_t)size)
98 return (0); 103 return (0);
104
99 return (len); 105 return (len);
100} 106}
101 107
@@ -110,6 +116,7 @@ tls_load_file(const char *name, size_t *len, char *password)
110 struct stat st; 116 struct stat st;
111 size_t size; 117 size_t size;
112 int fd = -1; 118 int fd = -1;
119 ssize_t n;
113 120
114 *len = 0; 121 *len = 0;
115 122
@@ -120,10 +127,13 @@ tls_load_file(const char *name, size_t *len, char *password)
120 if (password == NULL) { 127 if (password == NULL) {
121 if (fstat(fd, &st) != 0) 128 if (fstat(fd, &st) != 0)
122 goto fail; 129 goto fail;
130 if (st.st_size < 0)
131 goto fail;
123 size = (size_t)st.st_size; 132 size = (size_t)st.st_size;
124 if ((buf = calloc(1, size + 1)) == NULL) 133 if ((buf = malloc(size)) == NULL)
125 goto fail; 134 goto fail;
126 if (read(fd, buf, size) != size) 135 n = read(fd, buf, size);
136 if (n < 0 || (size_t)n != size)
127 goto fail; 137 goto fail;
128 close(fd); 138 close(fd);
129 goto done; 139 goto done;