Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drop antiquated memory protection magic #136

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/libcommon/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ noinst_LTLIBRARIES = libcommon.la
libcommon_la_SOURCES = \
argv.c \
argv.h \
debug.c \
debug.h \
error.c \
error.h \
hprintf.c \
Expand Down
3 changes: 0 additions & 3 deletions src/libcommon/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

#include "list.h"
#include "error.h"
#include "debug.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commit message typo "actually"

#include "xmalloc.h"

static char *err_prog = NULL; /* basename of calling program */
static bool err_ttyvalid = true; /* use stderr until told otherwise */
Expand Down Expand Up @@ -80,7 +78,6 @@ void err_exit(bool errno_valid, const char *fmt, ...)
va_start(ap, fmt);
_verr(errno_valid, fmt, ap);
va_end(ap);
dbg(DBG_MEMORY, "err_exit: memory not reclaimed: %d\n", xmemory());
exit(1);
}

Expand Down
77 changes: 5 additions & 72 deletions src/libcommon/xmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,102 +19,35 @@
#include "xmalloc.h"
#include "error.h"

#ifndef NDEBUG
static int memory_alloc = 0;
#endif

/* Review: look into dmalloc */
#define MALLOC_MAGIC 0xf00fbaab
#define MALLOC_PAD_SIZE 16
#define MALLOC_PAD_FILL 0x55

#ifndef NDEBUG
int xmemory(void)
{
return memory_alloc;
}

static int _checkfill(char *buf, unsigned char fill, int size)
{
while (size-- > 0)
if (buf[size] != fill)
return 0;
return 1;
}
#endif

char *xmalloc(int size)
{
char *new;
int *p;


assert(size > 0);
p = (int *) malloc(2*sizeof(int) + size + MALLOC_PAD_SIZE);
if (p == NULL)
if (!(new = calloc(1, size)))
err_exit(false, "out of memory");
p[0] = MALLOC_MAGIC; /* magic cookie */
p[1] = size; /* store size in buffer */
#ifndef NDEBUG
memory_alloc += size;
#endif
new = (char *) &p[2];
memset(new, 0, size);
memset(new + size, MALLOC_PAD_FILL, MALLOC_PAD_SIZE);
return new;
}

char *xrealloc(char *item , int newsize)
{
char *new;
int *p = (int *)item - 2;
int oldsize;


assert(item != NULL);
assert(newsize > 0);
assert(p[0] == MALLOC_MAGIC);
oldsize = p[1];
assert(_checkfill(item + oldsize, MALLOC_PAD_FILL, MALLOC_PAD_SIZE));
p = (int *)realloc(p, 2*sizeof(int) + newsize + MALLOC_PAD_SIZE);
if (p == NULL)
if (!(new = realloc(item, newsize)))
err_exit(false, "out of memory");
assert(p[0] == MALLOC_MAGIC);
p[1] = newsize;
#ifndef NDEBUG
memory_alloc += (newsize - oldsize);
#endif
new = (char *) &p[2];
if (newsize > oldsize)
memset(new + oldsize, 0, newsize - oldsize);
memset(new + newsize, MALLOC_PAD_FILL, MALLOC_PAD_SIZE);
return new;
}

void xfree(void *ptr)
{
if (ptr != NULL) {
int *p = (int *) ptr - 2;
int size;

assert(p[0] == MALLOC_MAGIC); /* magic cookie still there? */
size = p[1];
assert(_checkfill((char*)ptr + size, MALLOC_PAD_FILL, MALLOC_PAD_SIZE));
memset(p, 0, 2*sizeof(int) + size + MALLOC_PAD_SIZE);
#ifndef NDEBUG
memory_alloc -= size;
#endif
free(p);
}
free(ptr);
}

char *xstrdup(const char *str)
{
char *cpy;

cpy = xmalloc(strlen(str) + 1);

strcpy(cpy, str);
if (!(cpy = strdup(str)))
err_exit(false, "out of memory");
return cpy;
}

Expand Down
12 changes: 0 additions & 12 deletions src/libcommon/xpoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@

#define XPOLLFD_ALLOC_CHUNK 16

#define XPOLLFD_MAGIC 0x56452334
struct xpollfd {
int magic;
#if HAVE_POLL
unsigned int nfds;
unsigned int ufds_size;
Expand Down Expand Up @@ -138,7 +136,6 @@ xpoll(xpollfd_t pfd, struct timeval *tv)
static void
_grow_pollfd(xpollfd_t pfd, int n)
{
assert(pfd->magic == XPOLLFD_MAGIC);
while (pfd->ufds_size < n) {
pfd->ufds_size += XPOLLFD_ALLOC_CHUNK;
pfd->ufds = (struct pollfd *)xrealloc((char *)pfd->ufds, sizeof(struct pollfd) * pfd->ufds_size);
Expand All @@ -151,7 +148,6 @@ xpollfd_create(void)
{
xpollfd_t pfd = (xpollfd_t)xmalloc(sizeof(struct xpollfd));

pfd->magic = XPOLLFD_MAGIC;
#if HAVE_POLL
pfd->ufds_size += XPOLLFD_ALLOC_CHUNK;
pfd->ufds = (struct pollfd *)xmalloc(sizeof(struct pollfd)*pfd->ufds_size);
Expand All @@ -168,8 +164,6 @@ xpollfd_create(void)
void
xpollfd_destroy(xpollfd_t pfd)
{
assert(pfd->magic == XPOLLFD_MAGIC);
pfd->magic = 0;
#if HAVE_POLL
if (pfd->ufds != NULL)
xfree(pfd->ufds);
Expand All @@ -180,7 +174,6 @@ xpollfd_destroy(xpollfd_t pfd)
void
xpollfd_zero(xpollfd_t pfd)
{
assert(pfd->magic == XPOLLFD_MAGIC);
#if HAVE_POLL
pfd->nfds = 0;
/*memset(pfd->ufds, 0, sizeof(struct pollfd) * pfd->ufds_size);*/
Expand All @@ -197,7 +190,6 @@ xpollfd_set(xpollfd_t pfd, int fd, short events)
#if HAVE_POLL
int i;

assert(pfd->magic == XPOLLFD_MAGIC);
for (i = 0; i < pfd->nfds; i++) {
if (pfd->ufds[i].fd == fd) {
pfd->ufds[i].events |= xflag2flag(events);
Expand All @@ -210,7 +202,6 @@ xpollfd_set(xpollfd_t pfd, int fd, short events)
pfd->ufds[i].events = xflag2flag(events);
}
#else
assert(pfd->magic == XPOLLFD_MAGIC);
assert(fd < FD_SETSIZE);
if (events & XPOLLIN)
FD_SET(fd, &pfd->rset);
Expand All @@ -227,7 +218,6 @@ xpollfd_str(xpollfd_t pfd, char *str, int len)
#if HAVE_POLL
int maxfd = -1;
#endif
assert(pfd->magic == XPOLLFD_MAGIC);
#if HAVE_POLL
memset(str, '.', len);
for (i = 0; i < pfd->nfds; i++) {
Expand Down Expand Up @@ -271,15 +261,13 @@ xpollfd_revents(xpollfd_t pfd, int fd)
#if HAVE_POLL
int i;

assert(pfd->magic == XPOLLFD_MAGIC);
for (i = 0; i < pfd->nfds; i++) {
if (pfd->ufds[i].fd == fd) {
flags = flag2xflag(pfd->ufds[i].revents);
break;
}
}
#else
assert(pfd->magic == XPOLLFD_MAGIC);
if (FD_ISSET(fd, &pfd->rset))
flags |= XPOLLIN;
if (FD_ISSET(fd, &pfd->wset))
Expand Down
16 changes: 0 additions & 16 deletions src/libcommon/xregex.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@
#include "xregex.h"
#include "xmalloc.h"

#define XREGEX_MAGIC 0x3456aaaa
struct xregex_struct {
int xr_magic;
int xr_cflags;
regex_t *xr_regex;
};
#define XREGEX_MATCH_MAGIC 0x3456aaba
struct xregex_match_struct {
int xm_magic;
int xm_nmatch;
regmatch_t *xm_pmatch;
char *xm_str;
Expand All @@ -43,7 +39,6 @@ xregex_create(void)
{
xregex_t xrp = (xregex_t)xmalloc(sizeof(struct xregex_struct));

xrp->xr_magic = XREGEX_MAGIC;
xrp->xr_regex = NULL;

return xrp;
Expand All @@ -52,12 +47,10 @@ xregex_create(void)
void
xregex_destroy(xregex_t xrp)
{
assert(xrp->xr_magic == XREGEX_MAGIC);
if (xrp->xr_regex) {
regfree(xrp->xr_regex);
xfree(xrp->xr_regex);
}
xrp->xr_magic = 0;
xfree(xrp);
}

Expand Down Expand Up @@ -86,7 +79,6 @@ xregex_compile(xregex_t xrp, const char *regex, bool withsub)
int n;

assert(regex != NULL);
assert(xrp->xr_magic == XREGEX_MAGIC);
assert(xrp->xr_regex == NULL);

/* No particular limit is imposed on the length of REs(!). Programs
Expand Down Expand Up @@ -120,10 +112,8 @@ xregex_exec(xregex_t xrp, const char *s, xregex_match_t xm)
int eflags = REG_NOTEOL;
int res;

assert(xrp->xr_magic == XREGEX_MAGIC);
assert(xrp->xr_regex != NULL);
if (xm != NULL) {
assert(xm->xm_magic == XREGEX_MATCH_MAGIC);
assert(xm->xm_used == false);
}

Expand All @@ -147,7 +137,6 @@ xregex_match_create(int nmatch)
xregex_match_t xm;

xm = (xregex_match_t)xmalloc(sizeof(struct xregex_match_struct));
xm->xm_magic = XREGEX_MATCH_MAGIC;
xm->xm_nmatch = nmatch + 1;
xm->xm_pmatch = (regmatch_t *)xmalloc(sizeof(regmatch_t) * (nmatch + 1));
xm->xm_str = NULL;
Expand All @@ -159,12 +148,10 @@ xregex_match_create(int nmatch)
void
xregex_match_destroy(xregex_match_t xm)
{
assert(xm->xm_magic == XREGEX_MATCH_MAGIC);

xfree(xm->xm_pmatch);
if (xm->xm_str)
xfree(xm->xm_str);
xm->xm_magic = 0;
xfree(xm);
}

Expand All @@ -184,7 +171,6 @@ xregex_match_strdup(xregex_match_t xm)
{
char *s = NULL;

assert(xm->xm_magic == XREGEX_MATCH_MAGIC);
assert(xm->xm_used);

if (xm->xm_result == 0) {
Expand All @@ -199,7 +185,6 @@ xregex_match_strdup(xregex_match_t xm)
int
xregex_match_strlen(xregex_match_t xm)
{
assert(xm->xm_magic == XREGEX_MATCH_MAGIC);
assert(xm->xm_used);

return xm->xm_pmatch[0].rm_eo;
Expand All @@ -210,7 +195,6 @@ xregex_match_sub_strdup(xregex_match_t xm, int i)
{
char *s = NULL;

assert(xm->xm_magic == XREGEX_MATCH_MAGIC);
assert(xm->xm_used);

if (xm->xm_result == 0 && i >= 0 && i < xm->xm_nmatch
Expand Down
2 changes: 2 additions & 0 deletions src/powerman/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ powermand_SOURCES = \
client_proto.h \
daemon.c \
daemon.h \
debug.c \
debug.h \
device.c \
device.h \
device_pipe.c \
Expand Down
9 changes: 0 additions & 9 deletions src/powerman/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ typedef struct {
ArgList arglist; /* argument for query commands */
} Command;

#define CLI_MAGIC 0xdadadada
typedef struct {
int magic;
int fd; /* file descriptor for the socket */
int ofd; /* separate output file descriptor (if used) */
char *ip; /* IP address of the client's host */
Expand Down Expand Up @@ -679,7 +677,6 @@ static void _act_finish(int client_id, ActError acterr, const char *fmt, ...)
/* if client has gone away do nothing */
if (!(c = _find_client(client_id)))
return;
assert(c->magic == CLI_MAGIC);
assert(c->cmd != NULL);

/* handle errors immediately */
Expand Down Expand Up @@ -734,8 +731,6 @@ static void _act_finish(int client_id, ActError acterr, const char *fmt, ...)
*/
static void _destroy_client(Client *c)
{
assert(c->magic == CLI_MAGIC);

if (c->fd != NO_FD) {
dbg(DBG_CLIENT, "_destroy_client: closing fd %d", c->fd);
if (close(c->fd) < 0)
Expand Down Expand Up @@ -886,7 +881,6 @@ static void _create_client_socket(int fd)

/* create client data structure */
c = (Client *) xmalloc(sizeof(Client));
c->magic = CLI_MAGIC;
c->to = NULL;
c->from = NULL;
c->cmd = NULL;
Expand Down Expand Up @@ -964,7 +958,6 @@ static void _create_client_stdio(void)

/* create client data structure */
c = (Client *) xmalloc(sizeof(Client));
c->magic = CLI_MAGIC;
c->cmd = NULL;
c->client_id = _next_cli_id();
c->telemetry = false;
Expand Down Expand Up @@ -997,7 +990,6 @@ static void _handle_read(Client * c)
int n;
int dropped;

assert(c->magic == CLI_MAGIC);
n = cbuf_write_from_fd(c->from, c->fd, -1, &dropped);
if (n < 0) {
c->client_quit = true;
Expand All @@ -1021,7 +1013,6 @@ static void _handle_write(Client * c)
int n;
int ofd = c->ofd != NO_FD ? c->ofd : c->fd;

assert(c->magic == CLI_MAGIC);
if (c->client_quit)
nonblock_clr(ofd);
n = cbuf_read_to_fd(c->to, ofd, -1);
Expand Down
File renamed without changes.
File renamed without changes.
Loading
Loading