sed: prefer assignment to memcpy/memset

* sed/compile.c (next_cmd_entry, match_slash, setup_label)
(compile_program, check_final_program):
* sed/execute.c (str_append, str_append_modified, line_init)
(line_reset, line_copy, line_append, line_exchange, open_next_file):
* sed/mbcs.c (is_mb_char):
Prefer assignment to memcpy/memset when that is easy.
* sed/execute.c (SIZEOF_LINE): Remove.
This commit is contained in:
Paul Eggert committed 2026-08-17 01:17:23 -07:00
1 parent ff5921491a
commit b05def1241
3 files changed
+26 -29

No files matched your search

+7 -6
View File
@@ -362,8 +362,9 @@ next_cmd_entry (struct vector *v)
v->v = xpalloc (v->v, &v->v_allocated, 1, -1, sizeof *v->v);
cmd = v->v + v->v_length;
memset (cmd, 0, sizeof *cmd);
cmd->cmd = '\0'; /* something invalid, to catch bugs early */
/* This sets cmd->cmd to something invalid, to catch bugs early. */
*cmd = (struct sed_cmd) {NULL};
return cmd;
}
@@ -456,7 +457,7 @@ match_slash (int slash, bool regex, bool s_command)
if (IS_MB_CHAR (slash, &cur_stat))
bad_prog ("delimiter character is not a single-byte character");
memset (&cur_stat, 0, sizeof cur_stat);
cur_stat = (mbstate_t) {0};
b = init_buffer ();
while ((ch = inchar ()) != EOF && ch != '\n')
@@ -631,7 +632,7 @@ setup_label (struct sed_label *list, idx_t idx, char *name,
ret->v_index = idx;
ret->name = name;
if (err_info)
memcpy (&ret->err_info, err_info, sizeof (ret->err_info));
ret->err_info = *err_info;
ret->next = list;
return ret;
}
@@ -1227,7 +1228,7 @@ compile_program (struct vector *vector)
}
src_char_num = j;
memset (&cur_stat, 0, sizeof cur_stat);
cur_stat = (mbstate_t) {0};
idx = 0;
/* trans_pairs = {src(0), dest(0), src(1), dest(1), ..., NULL}
@@ -1527,7 +1528,7 @@ check_final_program (struct vector *program)
if (blocks)
{
/* update info for error reporting: */
memcpy (&cur_input, &blocks->err_info, sizeof (cur_input));
cur_input = blocks->err_info;
bad_prog ("unmatched '{'");
}
+18 -22
View File
@@ -56,8 +56,6 @@ struct line {
mbstate_t mbstate;
};
#define SIZEOF_LINE offsetof (struct line, mbstate)
/* A queue of text to write out at the end of a cycle
(filled by the "a", "r" and "R" commands.) */
struct append_queue {
@@ -174,7 +172,7 @@ str_append (struct line *to, const char *string, idx_t length)
single-byte character. */
if (n == (size_t) -1 || n == (size_t) -2)
{
memset (&to->mbstate, 0, sizeof (to->mbstate));
to->mbstate = (mbstate_t) {0};
n = 1;
}
@@ -201,7 +199,7 @@ str_append_modified (struct line *to, const char *string, idx_t length,
if (to->alloc - to->length < length * mb_cur_max)
resize_line (to, length * mb_cur_max);
memcpy (&from_stat, &to->mbstate, sizeof (mbstate_t));
from_stat = to->mbstate;
while (length)
{
wchar_t wc;
@@ -218,7 +216,7 @@ str_append_modified (struct line *to, const char *string, idx_t length,
}
str_append (to, string, 1);
memset (&to->mbstate, 0, sizeof (from_stat));
to->mbstate = (mbstate_t) {0};
n = 1;
string += n, length -= n;
continue;
@@ -283,11 +281,10 @@ line_init (struct line *buf, struct line *state, idx_t initial_size)
buf->alloc = initial_size;
buf->length = 0;
buf->chomped = true;
if (state)
memcpy (&buf->mbstate, &state->mbstate, sizeof (buf->mbstate));
buf->mbstate = state->mbstate;
else
memset (&buf->mbstate, 0, sizeof (buf->mbstate));
buf->mbstate = (mbstate_t) {0};
}
/* Reset a "struct line" buffer to length zero. Copy multibyte state from
@@ -304,9 +301,9 @@ line_reset (struct line *buf, struct line *state)
{
buf->length = 0;
if (state)
memcpy (&buf->mbstate, &state->mbstate, sizeof (buf->mbstate));
buf->mbstate = state->mbstate;
else
memset (&buf->mbstate, 0, sizeof (buf->mbstate));
buf->mbstate = (mbstate_t) {0};
}
}
@@ -335,7 +332,7 @@ line_copy (struct line *from, struct line *to, int state)
memcpy (to->active, from->active, from->length);
if (state)
memcpy (&to->mbstate, &from->mbstate, sizeof (from->mbstate));
to->mbstate = from->mbstate;
}
/* Append the contents of the line 'from' to the line 'to'.
@@ -348,7 +345,7 @@ line_append (struct line *from, struct line *to, int state)
to->chomped = from->chomped;
if (state)
memcpy (&to->mbstate, &from->mbstate, sizeof (from->mbstate));
to->mbstate = from->mbstate;
}
/* Exchange two "struct line" buffers.
@@ -356,19 +353,19 @@ line_append (struct line *from, struct line *to, int state)
static void
line_exchange (struct line *a, struct line *b, int state)
{
struct line t;
if (state)
{
memcpy (&t, a, sizeof (struct line));
memcpy ( a, b, sizeof (struct line));
memcpy ( b, &t, sizeof (struct line));
struct line t = *a;
*a = *b;
*b = t;
}
else
{
memcpy (&t, a, SIZEOF_LINE);
memcpy ( a, b, SIZEOF_LINE);
memcpy ( b, &t, SIZEOF_LINE);
char *t1 = a->text ; a->text = b->text ; b->text = t1;
char *t2 = a->active ; a->active = b->active ; b->active = t2;
idx_t t3 = a->length ; a->length = b->length ; b->length = t3;
idx_t t4 = a->alloc ; a->alloc = b->alloc ; b->alloc = t4;
bool t5 = a->chomped; a->chomped = b->chomped; b->chomped = t5;
}
}
@@ -579,9 +576,8 @@ open_next_file (const char *name, struct input *input)
{
int input_fd;
char *tmpdir, *p;
char *old_fscreatecon;
char *old_fscreatecon = NULL;
int reset_fscreatecon = 0;
memset (&old_fscreatecon, 0, sizeof (old_fscreatecon));
/* get the base name */
tmpdir = xstrdup (input->in_file_name);
+1 -1
View File
@@ -47,7 +47,7 @@ is_mb_char (int ch, mbstate_t *cur_stat)
return 1;
case -1: /* Invalid sequence, byte treated like a single-byte character */
memset (cur_stat, 0, sizeof (mbstate_t));
*cur_stat = (mbstate_t) {0};
return 0;
case 1: /* A valid byte, check if part of on-going multibyte sequence */