sed: replace MALLOC/ck_malloc with gnulib's XCALLOC
ck_malloc zero'd out the allocated buffer, thus replaced with xcalloc/xzalloc (not xmalloc). * basicdefs.h (MALLOC): Remove. * utils.c (ck_malloc): Remove. * sed/compile.c, sed/execute.c, sed/regexp.c, sed/sed.c: Replace MALLOC with XCALLOC; ck_malloc with xzalloc.
This commit is contained in:
1 parent
8dda6ea11a
commit
e68a03d750
6 files changed
+24
-32
No files matched your search
+1
-2
@@ -34,12 +34,11 @@ typedef unsigned long countT;
|
||||
#include "xalloc.h"
|
||||
|
||||
/* some basic definitions to avoid undue promulgating of ugliness */
|
||||
#define MALLOC(n,t) ((t *)ck_malloc((n)*sizeof(t)))
|
||||
#define REALLOC(x,n,t) ((t *)ck_realloc((void *)(x),(n)*sizeof(t)))
|
||||
#define MEMDUP(x,n,t) ((t *)xmemdup((x),(n)*sizeof(t)))
|
||||
#define OB_MALLOC(o,n,t) ((t *)(void *)obstack_alloc(o,(n)*sizeof(t)))
|
||||
|
||||
#define obstack_chunk_alloc ck_malloc
|
||||
#define obstack_chunk_alloc xzalloc
|
||||
#define obstack_chunk_free free
|
||||
|
||||
/* MAX_PATH is not defined in some platforms, most notably GNU/Hurd.
|
||||
|
||||
+6
-5
@@ -24,6 +24,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <obstack.h>
|
||||
#include "xalloc.h"
|
||||
|
||||
|
||||
#define YMAP_LENGTH 256 /*XXX shouldn't this be (UCHAR_MAX+1)?*/
|
||||
@@ -1009,7 +1010,7 @@ compile_program(struct vector *vector)
|
||||
|
||||
if (!vector)
|
||||
{
|
||||
vector = MALLOC(1, struct vector);
|
||||
vector = XCALLOC (1, struct vector);
|
||||
vector->v = NULL;
|
||||
vector->v_allocated = 0;
|
||||
vector->v_length = 0;
|
||||
@@ -1279,7 +1280,7 @@ compile_program(struct vector *vector)
|
||||
if (mb_cur_max > 1)
|
||||
{
|
||||
size_t i, j, idx, src_char_num;
|
||||
size_t *src_lens = MALLOC(len, size_t);
|
||||
size_t *src_lens = XCALLOC (len, size_t);
|
||||
char **trans_pairs;
|
||||
size_t mbclen;
|
||||
mbstate_t cur_stat = { 0, };
|
||||
@@ -1305,7 +1306,7 @@ compile_program(struct vector *vector)
|
||||
src(i) : pointer to i-th source character.
|
||||
dest(i) : pointer to i-th destination character.
|
||||
NULL : terminator */
|
||||
trans_pairs = MALLOC(2 * src_char_num + 1, char*);
|
||||
trans_pairs = XCALLOC (2 * src_char_num + 1, char*);
|
||||
cur_cmd->x.translatemb = trans_pairs;
|
||||
for (i = 0; i < src_char_num; i++)
|
||||
{
|
||||
@@ -1313,7 +1314,7 @@ compile_program(struct vector *vector)
|
||||
bad_prog(_(Y_CMD_LEN));
|
||||
|
||||
/* Set the i-th source character. */
|
||||
trans_pairs[2 * i] = MALLOC(src_lens[i] + 1, char);
|
||||
trans_pairs[2 * i] = XCALLOC (src_lens[i] + 1, char);
|
||||
memcpy(trans_pairs[2 * i], src_buf, src_lens[i]);
|
||||
trans_pairs[2 * i][src_lens[i]] = '\0';
|
||||
src_buf += src_lens[i]; /* Forward to next character. */
|
||||
@@ -1327,7 +1328,7 @@ compile_program(struct vector *vector)
|
||||
mbclen = 1;
|
||||
|
||||
/* Set the i-th destination character. */
|
||||
trans_pairs[2 * i + 1] = MALLOC(mbclen + 1, char);
|
||||
trans_pairs[2 * i + 1] = XCALLOC (mbclen + 1, char);
|
||||
memcpy(trans_pairs[2 * i + 1], dest_buf + idx, mbclen);
|
||||
trans_pairs[2 * i + 1][mbclen] = '\0';
|
||||
idx += mbclen; /* Forward to next character. */
|
||||
|
||||
+3
-3
@@ -280,7 +280,7 @@ str_append_modified(struct line *to, const char *string, size_t length,
|
||||
static void
|
||||
line_init(struct line *buf, struct line *state, size_t initial_size)
|
||||
{
|
||||
buf->text = MALLOC(initial_size + DFA_SLOP, char);
|
||||
buf->text = XCALLOC (initial_size + DFA_SLOP, char);
|
||||
buf->active = buf->text;
|
||||
buf->alloc = initial_size;
|
||||
buf->length = 0;
|
||||
@@ -328,7 +328,7 @@ line_copy(struct line *from, struct line *to, int state)
|
||||
/* Use free()+MALLOC() instead of REALLOC() to
|
||||
avoid unnecessary copying of old text. */
|
||||
free(to->text);
|
||||
to->text = MALLOC(to->alloc + DFA_SLOP, char);
|
||||
to->text = XCALLOC (to->alloc + DFA_SLOP, char);
|
||||
}
|
||||
|
||||
to->active = to->text;
|
||||
@@ -440,7 +440,7 @@ output_line(const char *text, size_t length, int nl, struct output *outf)
|
||||
static struct append_queue *
|
||||
next_append_slot(void)
|
||||
{
|
||||
struct append_queue *n = MALLOC(1, struct append_queue);
|
||||
struct append_queue *n = XCALLOC (1, struct append_queue);
|
||||
|
||||
n->fname = NULL;
|
||||
n->text = NULL;
|
||||
|
||||
+7
-5
@@ -23,6 +23,8 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "xalloc.h"
|
||||
|
||||
#ifdef gettext_noop
|
||||
# define N_(String) gettext_noop(String)
|
||||
#else
|
||||
@@ -176,7 +178,7 @@ compile_regex(struct buffer *b, int flags, int needed_sub)
|
||||
}
|
||||
|
||||
re_len = size_buffer(b);
|
||||
new_regex = ck_malloc(sizeof (struct regex) + re_len - 1);
|
||||
new_regex = xzalloc(sizeof (struct regex) + re_len - 1);
|
||||
new_regex->flags = flags;
|
||||
memcpy (new_regex->re, get_buffer(b), re_len);
|
||||
|
||||
@@ -206,8 +208,8 @@ copy_regs (regs, pmatch, nregs)
|
||||
/* Have the register data arrays been allocated? */
|
||||
if (!regs->start)
|
||||
{ /* No. So allocate them with malloc. */
|
||||
regs->start = MALLOC (need_regs, regoff_t);
|
||||
regs->end = MALLOC (need_regs, regoff_t);
|
||||
regs->start = XCALLOC (need_regs, regoff_t);
|
||||
regs->end = XCALLOC (need_regs, regoff_t);
|
||||
regs->num_regs = need_regs;
|
||||
}
|
||||
else if (need_regs > regs->num_regs)
|
||||
@@ -323,8 +325,8 @@ match_regex(struct regex *regex, char *buf, size_t buflen,
|
||||
|
||||
if (!regarray->start)
|
||||
{
|
||||
regarray->start = MALLOC (1, regoff_t);
|
||||
regarray->end = MALLOC (1, regoff_t);
|
||||
regarray->start = XCALLOC (1, regoff_t);
|
||||
regarray->end = XCALLOC (1, regoff_t);
|
||||
regarray->num_regs = 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -301,7 +301,7 @@ main (int argc, char **argv)
|
||||
|
||||
else
|
||||
{
|
||||
in_place_extension = MALLOC (strlen(optarg) + 2, char);
|
||||
in_place_extension = XCALLOC (strlen(optarg) + 2, char);
|
||||
in_place_extension[0] = '*';
|
||||
strcpy (in_place_extension + 1, optarg);
|
||||
}
|
||||
|
||||
+6
-16
@@ -115,7 +115,7 @@ register_open_file (FILE *fp, const char *name)
|
||||
}
|
||||
if (!p)
|
||||
{
|
||||
p = MALLOC(1, struct open_file);
|
||||
p = XCALLOC (1, struct open_file);
|
||||
p->link = open_files;
|
||||
open_files = p;
|
||||
}
|
||||
@@ -302,8 +302,8 @@ follow_symlink(const char *fname)
|
||||
|
||||
if (buf_size == 0)
|
||||
{
|
||||
buf1 = ck_malloc (PATH_MAX + 1);
|
||||
buf2 = ck_malloc (PATH_MAX + 1);
|
||||
buf1 = xzalloc (PATH_MAX + 1);
|
||||
buf2 = xzalloc (PATH_MAX + 1);
|
||||
buf_size = PATH_MAX + 1;
|
||||
}
|
||||
|
||||
@@ -392,16 +392,6 @@ ck_rename (const char *from, const char *to, const char *unlink_if_fail)
|
||||
|
||||
|
||||
|
||||
|
||||
/* Panic on failing malloc */
|
||||
void *
|
||||
ck_malloc(size_t size)
|
||||
{
|
||||
void *ret = calloc(1, size ? size : 1);
|
||||
if (!ret)
|
||||
panic("couldn't allocate memory");
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Panic on failing realloc */
|
||||
void *
|
||||
@@ -415,7 +405,7 @@ ck_realloc(void *ptr, size_t size)
|
||||
return NULL;
|
||||
}
|
||||
if (!ptr)
|
||||
return ck_malloc(size);
|
||||
return xzalloc(size);
|
||||
ret = realloc(ptr, size);
|
||||
if (!ret)
|
||||
panic("couldn't re-allocate memory");
|
||||
@@ -438,8 +428,8 @@ struct buffer
|
||||
struct buffer *
|
||||
init_buffer(void)
|
||||
{
|
||||
struct buffer *b = MALLOC(1, struct buffer);
|
||||
b->b = MALLOC(MIN_ALLOCATE, char);
|
||||
struct buffer *b = XCALLOC (1, struct buffer);
|
||||
b->b = XCALLOC (MIN_ALLOCATE, char);
|
||||
b->allocated = MIN_ALLOCATE;
|
||||
b->length = 0;
|
||||
return b;
|
||||
|
||||
Reference in new issue
Block a user