Browse Source

Avoid needless dynamic memory allocation

Do not use a dynamically allocated buffer to store the structure
used to pass random bytes to the kernel. The maximum size is
known in advance, so we can use a buffer allocated on the stack.

We can thus get rid of the xmalloc wrapper, which was only used
for that allocation.
develop
Damien Goutte-Gattat 4 years ago
parent
commit
a72f2363e1
4 changed files with 6 additions and 181 deletions
  1. +1
    -1
      lib/Makefile.am
  2. +0
    -114
      lib/xmem.c
  3. +0
    -60
      lib/xmem.h
  4. +5
    -6
      src/scdrand.c

+ 1
- 1
lib/Makefile.am View File

@ -1,5 +1,5 @@
noinst_LIBRARIES = libscdrand.a
libscdrand_a_SOURCES = err.compat.h compat.h xmem.c xmem.h
libscdrand_a_SOURCES = err.compat.h compat.h
libscdrand_a_LIBADD = $(LIBOBJS)

+ 0
- 114
lib/xmem.c View File

@ -1,114 +0,0 @@
/*
* xmem - Incenp.org Notch Library: die-on-error memory functions
* Copyright (C) 2011 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <xmem.h>
#include <stdio.h>
#include <string.h>
#include <err.h>
void (*xmem_error)(const char *, unsigned int) = NULL;
#define mem_error(f, l) \
do { \
if ( xmem_error ) \
(*xmem_error)((f), (l)); \
err(EXIT_FAILURE, "%s, %d", (f), (l)); \
} while ( 0 )
void *
do_malloc(size_t s, const char *file, unsigned int line)
{
void *p;
if ( ! (p = malloc(s)) && s )
mem_error(file, line);
return p;
}
void *
do_calloc(size_t n, size_t s, const char *file, unsigned int line)
{
void *p;
if ( ! (p = calloc(n, s)) && n && s )
mem_error(file, line);
return p;
}
void *
do_realloc(void *p, size_t s, const char *file, unsigned int line)
{
void *np;
if ( ! (np = realloc(p, s)) && s )
mem_error(file, line);
return np;
}
char *
do_strdup(const char *s, const char *file, unsigned int line)
{
char *dup;
if ( ! (dup = strdup(s)) && s )
mem_error(file, line);
return dup;
}
int
do_asprintf(char **s,
const char *fmt,
const char *file,
unsigned int line,
...)
{
int ret;
va_list ap;
va_start(ap, line);
ret = vasprintf(s, fmt, ap);
va_end(ap);
if ( ret == -1 )
mem_error(file, line);
return ret;
}
int
do_vasprintf(char **s,
const char *fmt,
const char *file,
unsigned int line,
va_list ap)
{
int ret;
if ( (ret = vasprintf(s, fmt, ap)) == -1 )
mem_error(file, line);
return ret;
}

+ 0
- 60
lib/xmem.h View File

@ -1,60 +0,0 @@
/*
* xmem - Incenp.org Notch Library: die-on-error memory functions
* Copyright (C) 2011 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ICP20110203_XMEM_H
#define ICP20110203_XMEM_H
#include <stdlib.h>
#include <stdarg.h>
#ifdef __cpluscplus
extern "C" {
#endif
extern void (*xmem_error)(const char *, unsigned int);
void *
do_malloc(size_t, const char *, unsigned int);
void *
do_calloc(size_t, size_t, const char *, unsigned int);
void *
do_realloc(void *, size_t, const char *, unsigned int);
char *
do_strdup(const char *, const char *, unsigned int);
int
do_asprintf(char **, const char *, const char *, unsigned int, ...);
int
do_vasprintf(char **, const char *, const char *, unsigned int, va_list);
#define xmalloc(s) do_malloc(s, __FILE__, __LINE__)
#define xcalloc(n,s) do_calloc(n, s, __FILE__, __LINE__)
#define xrealloc(p,s) do_realloc(p, s, __FILE__, __LINE__)
#define xstrdup(s) do_strdup(s, __FILE__, __LINE__)
#define xasprintf(s,f,...) do_asprintf(s, f, __FILE__, __LINE__, __VA_ARGS__)
#define xvasprintf(s,f,v) do_vasprintf(s, f, __FILE__, __LINE__, v)
#ifdef __cplusplus
}
#endif
#endif /* !ICP20110203_XMEM_H */

+ 5
- 6
src/scdrand.c View File

@ -37,7 +37,6 @@
#include <gcrypt.h>
#include "xmem.h"
#include "gpg-util.h"
#define MAX_RANDOM_BYTES 256
@ -206,18 +205,19 @@ start_privileged_process(unsigned factor)
if ( (pid = fork()) == 0 ) {
int random_fd, n;
char buffer[MAX_RANDOM_BYTES];
char buffer[sizeof(struct rand_pool_info) + MAX_RANDOM_BYTES], *ptr;
struct rand_pool_info *rpi;
rpi = (struct rand_pool_info *)buffer;
ptr = (char *) &(rpi->buf[0]);
close(fd[1]);
if ( (random_fd = open("/dev/random", O_RDONLY)) == -1 )
err(EXIT_FAILURE, "Cannot open /dev/random");
rpi = xmalloc(sizeof(struct rand_pool_info) + MAX_RANDOM_BYTES);
while ( 1 ) {
n = read(fd[0], buffer, sizeof(buffer));
n = read(fd[0], ptr, MAX_RANDOM_BYTES);
if ( n == -1 )
err(EXIT_FAILURE, "Cannot read from pipe");
@ -227,7 +227,6 @@ start_privileged_process(unsigned factor)
rpi->entropy_count = n * factor;
rpi->buf_size = n;
memcpy(&(rpi->buf[0]), buffer, n);
if ( ioctl(random_fd, RNDADDENTROPY, rpi) == -1 )
warn("Cannot add entropy");


Loading…
Cancel
Save