1
0
mirror of git://git.gnupg.org/gnupg.git synced 2025-02-21 19:48:05 +01:00

common: Replace all assert in iobuf by log_assert.

--
This commit is contained in:
Werner Koch 2023-01-18 12:17:02 +01:00
parent 417e8588f3
commit ca822a2339
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B

View File

@ -27,6 +27,7 @@
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>. * along with this program; if not, see <https://www.gnu.org/licenses/>.
* SPDX-License-Identifier: (LGPL-3.0-or-later OR GPL-2.0-or-later)
*/ */
#include <config.h> #include <config.h>
@ -35,7 +36,6 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <ctype.h> #include <ctype.h>
#include <assert.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
@ -203,7 +203,7 @@ fd_cache_invalidate (const char *fname)
close_cache_t cc; close_cache_t cc;
int rc = 0; int rc = 0;
assert (fname); log_assert (fname);
if (DBG_IOBUF) if (DBG_IOBUF)
log_debug ("fd_cache_invalidate (%s)\n", fname); log_debug ("fd_cache_invalidate (%s)\n", fname);
@ -366,7 +366,7 @@ fd_cache_close (const char *fname, gnupg_fd_t fp)
{ {
close_cache_t cc; close_cache_t cc;
assert (fp); log_assert (fp);
if (!fname || !*fname) if (!fname || !*fname)
{ {
#ifdef HAVE_W32_SYSTEM #ifdef HAVE_W32_SYSTEM
@ -407,7 +407,7 @@ fd_cache_open (const char *fname, const char *mode)
{ {
close_cache_t cc; close_cache_t cc;
assert (fname); log_assert (fname);
for (cc = close_cache; cc; cc = cc->next) for (cc = close_cache; cc; cc = cc->next)
{ {
if (cc->fp != GNUPG_INVALID_FD && !fd_cache_strcmp (cc->fname, fname)) if (cc->fp != GNUPG_INVALID_FD && !fd_cache_strcmp (cc->fname, fname))
@ -453,7 +453,7 @@ file_filter (void *opaque, int control, iobuf_t chain, byte * buf,
if (control == IOBUFCTRL_UNDERFLOW) if (control == IOBUFCTRL_UNDERFLOW)
{ {
assert (size); /* We need a buffer. */ log_assert (size); /* We need a buffer. */
if (a->eof_seen) if (a->eof_seen)
{ {
rc = -1; rc = -1;
@ -609,7 +609,7 @@ file_es_filter (void *opaque, int control, iobuf_t chain, byte * buf,
if (control == IOBUFCTRL_UNDERFLOW) if (control == IOBUFCTRL_UNDERFLOW)
{ {
assert (size); /* We need a buffer. */ log_assert (size); /* We need a buffer. */
if (a->eof_seen) if (a->eof_seen)
{ {
rc = -1; rc = -1;
@ -700,7 +700,7 @@ sock_filter (void *opaque, int control, iobuf_t chain, byte * buf,
if (control == IOBUFCTRL_UNDERFLOW) if (control == IOBUFCTRL_UNDERFLOW)
{ {
assert (size); /* need a buffer */ log_assert (size); /* need a buffer */
if (a->eof_seen) if (a->eof_seen)
{ {
rc = -1; rc = -1;
@ -795,7 +795,7 @@ block_filter (void *opaque, int control, iobuf_t chain, byte * buffer,
size_t n = 0; size_t n = 0;
p = buf; p = buf;
assert (size); /* need a buffer */ log_assert (size); /* need a buffer */
if (a->eof) /* don't read any further */ if (a->eof) /* don't read any further */
rc = -1; rc = -1;
while (!rc && size) while (!rc && size)
@ -917,7 +917,7 @@ block_filter (void *opaque, int control, iobuf_t chain, byte * buffer,
{ /* the complicated openpgp scheme */ { /* the complicated openpgp scheme */
size_t blen, n, nbytes = size + a->buflen; size_t blen, n, nbytes = size + a->buflen;
assert (a->buflen <= OP_MIN_PARTIAL_CHUNK); log_assert (a->buflen <= OP_MIN_PARTIAL_CHUNK);
if (nbytes < OP_MIN_PARTIAL_CHUNK) if (nbytes < OP_MIN_PARTIAL_CHUNK)
{ {
/* not enough to write a partial block out; so we store it */ /* not enough to write a partial block out; so we store it */
@ -941,12 +941,12 @@ block_filter (void *opaque, int control, iobuf_t chain, byte * buffer,
blen /= 2; blen /= 2;
c--; c--;
/* write the partial length header */ /* write the partial length header */
assert (c <= 0x1f); /*;-) */ log_assert (c <= 0x1f); /*;-) */
c |= 0xe0; c |= 0xe0;
iobuf_put (chain, c); iobuf_put (chain, c);
if ((n = a->buflen)) if ((n = a->buflen))
{ /* write stuff from the buffer */ { /* write stuff from the buffer */
assert (n == OP_MIN_PARTIAL_CHUNK); log_assert (n == OP_MIN_PARTIAL_CHUNK);
if (iobuf_write (chain, a->buffer, n)) if (iobuf_write (chain, a->buffer, n))
rc = gpg_error_from_syserror (); rc = gpg_error_from_syserror ();
a->buflen = 0; a->buflen = 0;
@ -963,8 +963,8 @@ block_filter (void *opaque, int control, iobuf_t chain, byte * buffer,
/* store the rest in the buffer */ /* store the rest in the buffer */
if (!rc && nbytes) if (!rc && nbytes)
{ {
assert (!a->buflen); log_assert (!a->buflen);
assert (nbytes < OP_MIN_PARTIAL_CHUNK); log_assert (nbytes < OP_MIN_PARTIAL_CHUNK);
if (!a->buffer) if (!a->buffer)
a->buffer = xmalloc (OP_MIN_PARTIAL_CHUNK); a->buffer = xmalloc (OP_MIN_PARTIAL_CHUNK);
memcpy (a->buffer, p, nbytes); memcpy (a->buffer, p, nbytes);
@ -1102,8 +1102,8 @@ iobuf_alloc (int use, size_t bufsize)
iobuf_t a; iobuf_t a;
static int number = 0; static int number = 0;
assert (use == IOBUF_INPUT || use == IOBUF_INPUT_TEMP log_assert (use == IOBUF_INPUT || use == IOBUF_INPUT_TEMP
|| use == IOBUF_OUTPUT || use == IOBUF_OUTPUT_TEMP); || use == IOBUF_OUTPUT || use == IOBUF_OUTPUT_TEMP);
if (bufsize == 0) if (bufsize == 0)
{ {
log_bug ("iobuf_alloc() passed a bufsize of 0!\n"); log_bug ("iobuf_alloc() passed a bufsize of 0!\n");
@ -1218,7 +1218,7 @@ iobuf_temp_with_content (const char *buffer, size_t length)
int i; int i;
a = iobuf_alloc (IOBUF_INPUT_TEMP, length); a = iobuf_alloc (IOBUF_INPUT_TEMP, length);
assert (length == a->d.size); log_assert (length == a->d.size);
/* memcpy (a->d.buf, buffer, length); */ /* memcpy (a->d.buf, buffer, length); */
for (i=0; i < length; i++) for (i=0; i < length; i++)
a->d.buf[i] = buffer[i]; a->d.buf[i] = buffer[i];
@ -1249,7 +1249,7 @@ do_open (const char *fname, int special_filenames,
int fd; int fd;
byte desc[MAX_IOBUF_DESC]; byte desc[MAX_IOBUF_DESC];
assert (use == IOBUF_INPUT || use == IOBUF_OUTPUT); log_assert (use == IOBUF_INPUT || use == IOBUF_OUTPUT);
if (special_filenames if (special_filenames
/* NULL or '-'. */ /* NULL or '-'. */
@ -1666,13 +1666,13 @@ iobuf_pop_filter (iobuf_t a, int (*f) (void *opaque, int control,
if (a->use == IOBUF_INPUT_TEMP || a->use == IOBUF_OUTPUT_TEMP) if (a->use == IOBUF_INPUT_TEMP || a->use == IOBUF_OUTPUT_TEMP)
{ {
/* This should be the last filter in the pipeline. */ /* This should be the last filter in the pipeline. */
assert (! a->chain); log_assert (! a->chain);
return 0; return 0;
} }
if (!a->filter) if (!a->filter)
{ /* this is simple */ { /* this is simple */
b = a->chain; b = a->chain;
assert (b); log_assert (b);
xfree (a->d.buf); xfree (a->d.buf);
xfree (a->real_fname); xfree (a->real_fname);
memcpy (a, b, sizeof *a); memcpy (a, b, sizeof *a);
@ -1767,12 +1767,12 @@ underflow_target (iobuf_t a, int clear_pending_eof, size_t target)
buffer. */ buffer. */
return -1; return -1;
assert (a->use == IOBUF_INPUT); log_assert (a->use == IOBUF_INPUT);
/* If there is still some buffered data, then move it to the start /* If there is still some buffered data, then move it to the start
of the buffer and try to fill the end of the buffer. (This is of the buffer and try to fill the end of the buffer. (This is
useful if we are called from iobuf_peek().) */ useful if we are called from iobuf_peek().) */
assert (a->d.start <= a->d.len); log_assert (a->d.start <= a->d.len);
a->d.len -= a->d.start; a->d.len -= a->d.start;
memmove (a->d.buf, &a->d.buf[a->d.start], a->d.len); memmove (a->d.buf, &a->d.buf[a->d.start], a->d.len);
a->d.start = 0; a->d.start = 0;
@ -1900,7 +1900,7 @@ underflow_target (iobuf_t a, int clear_pending_eof, size_t target)
} }
} }
assert (a->d.start <= a->d.len); log_assert (a->d.start <= a->d.len);
if (a->d.start < a->d.len) if (a->d.start < a->d.len)
return a->d.buf[a->d.start++]; return a->d.buf[a->d.start++];
@ -1957,7 +1957,7 @@ iobuf_readbyte (iobuf_t a)
return -1; return -1;
} }
assert (a->d.start <= a->d.len); log_assert (a->d.start <= a->d.len);
if (a->nlimit && a->nbytes >= a->nlimit) if (a->nlimit && a->nbytes >= a->nlimit)
return -1; /* forced EOF */ return -1; /* forced EOF */
@ -1969,7 +1969,7 @@ iobuf_readbyte (iobuf_t a)
else if ((c = underflow (a, 1)) == -1) else if ((c = underflow (a, 1)) == -1)
return -1; /* EOF */ return -1; /* EOF */
assert (a->d.start <= a->d.len); log_assert (a->d.start <= a->d.len);
/* Note: if underflow doesn't return EOF, then it returns the first /* Note: if underflow doesn't return EOF, then it returns the first
byte that was read and advances a->d.start appropriately. */ byte that was read and advances a->d.start appropriately. */
@ -2057,8 +2057,8 @@ iobuf_peek (iobuf_t a, byte * buf, unsigned buflen)
{ {
int n = 0; int n = 0;
assert (buflen > 0); log_assert (buflen > 0);
assert (a->use == IOBUF_INPUT || a->use == IOBUF_INPUT_TEMP); log_assert (a->use == IOBUF_INPUT || a->use == IOBUF_INPUT_TEMP);
if (buflen > a->d.size) if (buflen > a->d.size)
/* We can't peek more than we can buffer. */ /* We can't peek more than we can buffer. */
@ -2074,7 +2074,7 @@ iobuf_peek (iobuf_t a, byte * buf, unsigned buflen)
/* Underflow consumes the first character (it's the return /* Underflow consumes the first character (it's the return
value). unget() it by resetting the "file position". */ value). unget() it by resetting the "file position". */
assert (a->d.start == 1); log_assert (a->d.start == 1);
a->d.start = 0; a->d.start = 0;
} }
@ -2109,7 +2109,7 @@ iobuf_writebyte (iobuf_t a, unsigned int c)
if ((rc=filter_flush (a))) if ((rc=filter_flush (a)))
return rc; return rc;
assert (a->d.len < a->d.size); log_assert (a->d.len < a->d.size);
a->d.buf[a->d.len++] = c; a->d.buf[a->d.len++] = c;
return 0; return 0;
} }
@ -2168,8 +2168,8 @@ iobuf_writestr (iobuf_t a, const char *buf)
int int
iobuf_write_temp (iobuf_t dest, iobuf_t source) iobuf_write_temp (iobuf_t dest, iobuf_t source)
{ {
assert (source->use == IOBUF_OUTPUT || source->use == IOBUF_OUTPUT_TEMP); log_assert (source->use == IOBUF_OUTPUT || source->use == IOBUF_OUTPUT_TEMP);
assert (dest->use == IOBUF_OUTPUT || dest->use == IOBUF_OUTPUT_TEMP); log_assert (dest->use == IOBUF_OUTPUT || dest->use == IOBUF_OUTPUT_TEMP);
iobuf_flush_temp (source); iobuf_flush_temp (source);
return iobuf_write (dest, source->d.buf, source->d.len); return iobuf_write (dest, source->d.buf, source->d.len);
@ -2552,7 +2552,7 @@ iobuf_read_line (iobuf_t a, byte ** addr_of_buffer,
NUL character in the buffer. This requires at least 2 bytes. We NUL character in the buffer. This requires at least 2 bytes. We
don't complicate the code by handling the stupid corner case, but don't complicate the code by handling the stupid corner case, but
simply assert that it can't happen. */ simply assert that it can't happen. */
assert (!buffer || length >= 2 || maxlen >= 2); log_assert (!buffer || length >= 2 || maxlen >= 2);
if (!buffer || length <= 1) if (!buffer || length <= 1)
/* must allocate a new buffer */ /* must allocate a new buffer */
@ -2585,7 +2585,7 @@ iobuf_read_line (iobuf_t a, byte ** addr_of_buffer,
/* p is pointing at the last byte in the buffer. We /* p is pointing at the last byte in the buffer. We
always terminate the line with "\n\0" so overwrite always terminate the line with "\n\0" so overwrite
the previous byte with a \n. */ the previous byte with a \n. */
assert (p > buffer); log_assert (p > buffer);
p[-1] = '\n'; p[-1] = '\n';
/* Indicate truncation. */ /* Indicate truncation. */