Move bufsize_roundup() -> sudo_pow2_roundup() in libsudo_util.

This commit is contained in:
Todd C. Miller
2019-11-02 12:03:44 -06:00
parent 676d3fc67b
commit f1d0c99e03
8 changed files with 65 additions and 26 deletions

View File

@@ -196,6 +196,7 @@ lib/util/regress/sudo_parseln/test6.in
lib/util/regress/sudo_parseln/test6.out.ok
lib/util/regress/tailq/hltq_test.c
lib/util/regress/vsyslog/vsyslog_test.c
lib/util/roundup.c
lib/util/secure_path.c
lib/util/setgroups.c
lib/util/sha2.c

View File

@@ -244,6 +244,10 @@ __dso_public ssize_t sudo_parseln_v2(char **buf, size_t *bufsize, unsigned int *
/* progname.c */
__dso_public void initprogname(const char *);
/* roundup.c */
__dso_public unsigned int sudo_pow2_roundup_v1(unsigned int len);
#define sudo_pow2_roundup(_a) sudo_pow2_roundup_v1((_a))
/* secure_path.c */
#define SUDO_PATH_SECURE 0
#define SUDO_PATH_MISSING -1

View File

@@ -118,7 +118,7 @@ SHELL = @SHELL@
LTOBJS = @DIGEST@ event.lo fatal.lo key_val.lo gethostname.lo \
gettime.lo getgrouplist.lo gidlist.lo lbuf.lo locking.lo \
logfac.lo logpri.lo mkdir_parents.lo parseln.lo progname.lo \
secure_path.lo setgroups.lo strsplit.lo strtobool.lo \
roundup.lo secure_path.lo setgroups.lo strsplit.lo strtobool.lo \
strtoid.lo strtomode.lo strtonum.lo sudo_conf.lo \
sudo_debug.lo sudo_dso.lo term.lo ttyname_dev.lo \
ttysize.lo @COMMON_OBJS@ @LTLIBOBJS@
@@ -966,6 +966,18 @@ reallocarray.i: $(srcdir)/reallocarray.c $(incdir)/sudo_compat.h \
$(CC) -E -o $@ $(CPPFLAGS) $<
reallocarray.plog: reallocarray.i
rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/reallocarray.c --i-file $< --output-file $@
roundup.lo: $(srcdir)/roundup.c $(incdir)/compat/stdbool.h \
$(incdir)/sudo_compat.h $(incdir)/sudo_debug.h \
$(incdir)/sudo_queue.h $(incdir)/sudo_util.h \
$(top_builddir)/config.h
$(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/roundup.c
roundup.i: $(srcdir)/roundup.c $(incdir)/compat/stdbool.h \
$(incdir)/sudo_compat.h $(incdir)/sudo_debug.h \
$(incdir)/sudo_queue.h $(incdir)/sudo_util.h \
$(top_builddir)/config.h
$(CC) -E -o $@ $(CPPFLAGS) $<
roundup.plog: roundup.i
rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/roundup.c --i-file $< --output-file $@
secure_path.lo: $(srcdir)/secure_path.c $(incdir)/compat/stdbool.h \
$(incdir)/sudo_compat.h $(incdir)/sudo_debug.h \
$(incdir)/sudo_queue.h $(incdir)/sudo_util.h \

43
lib/util/roundup.c Normal file
View File

@@ -0,0 +1,43 @@
/*
* Copyright (c) 2019 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <sys/types.h>
#include "sudo_compat.h"
#include "sudo_debug.h"
#include "sudo_util.h"
/*
* Round 32-bit unsigned length to the next highest power of two.
* Always returns at least 64.
* Algorithm from bit twiddling hacks.
*/
unsigned int
sudo_pow2_roundup_v1(unsigned int len)
{
if (len < 64)
return 64;
len--;
len |= len >> 1;
len |= len >> 2;
len |= len >> 4;
len |= len >> 8;
len |= len >> 16;
len++;
return len;
}

View File

@@ -43,26 +43,6 @@
#include "sudo_iolog.h"
#include "logsrv_util.h"
/*
* Round 32-bit unsigned length to the next highest power of two.
* Always returns at least 64.
* Algorithm from bit twiddling hacks.
*/
unsigned int
bufsize_roundup(unsigned int len)
{
if (len < 64)
return 64;
len--;
len |= len >> 1;
len |= len >> 2;
len |= len >> 4;
len |= len >> 8;
len |= len >> 16;
len++;
return len;
}
/*
* Expand buf as needed or just reset it.
*/
@@ -74,7 +54,7 @@ expand_buf(struct connection_buffer *buf, unsigned int needed)
if (buf->size < needed) {
/* Expand buffer. */
needed = bufsize_roundup(needed);
needed = sudo_pow2_roundup(needed);
if ((newdata = malloc(needed)) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
"%s: unable to malloc %u", __func__, needed);

View File

@@ -35,7 +35,6 @@ struct iolog_file;
bool expand_buf(struct connection_buffer *buf, unsigned int needed);
bool iolog_open_all(int dfd, const char *iolog_dir, struct iolog_file *iolog_files, const char *mode);
bool iolog_seekto(int iolog_dir_fd, const char *iolog_path, struct iolog_file *iolog_files, struct timespec *elapsed_time, const struct timespec *target);
unsigned int bufsize_roundup(unsigned int len);
#endif /* SUDO_LOGSRV_UTIL_H */

View File

@@ -126,7 +126,7 @@ fmt_server_message(struct connection_buffer *buf, ServerMessage *msg)
/* Resize buffer as needed. */
if (len > buf->size) {
free(buf->data);
buf->size = bufsize_roundup(len);
buf->size = sudo_pow2_roundup(len);
if ((buf->data = malloc(buf->size)) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to malloc %u", buf->size);

View File

@@ -186,7 +186,7 @@ read_io_buf(struct client_closure *closure)
/* Expand buf as needed. */
if (timing->u.nbytes > closure->bufsize) {
free(closure->buf);
closure->bufsize = bufsize_roundup(timing->u.nbytes);
closure->bufsize = sudo_pow2_roundup(timing->u.nbytes);
if ((closure->buf = malloc(closure->bufsize)) == NULL) {
sudo_warn(NULL);
timing->u.nbytes = 0;
@@ -228,7 +228,7 @@ fmt_client_message(struct connection_buffer *buf, ClientMessage *msg)
/* Resize buffer as needed. */
if (len > buf->size) {
free(buf->data);
buf->size = bufsize_roundup(len);
buf->size = sudo_pow2_roundup(len);
if ((buf->data = malloc(buf->size)) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unable to malloc %u", buf->size);