diff --git a/sed/compile.c b/sed/compile.c index 26e617c..5f966fe 100644 --- a/sed/compile.c +++ b/sed/compile.c @@ -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 '{'"); } diff --git a/sed/execute.c b/sed/execute.c index 67399bd..ac53c4b 100644 --- a/sed/execute.c +++ b/sed/execute.c @@ -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); diff --git a/sed/mbcs.c b/sed/mbcs.c index 7dd7d1d..5922b43 100644 --- a/sed/mbcs.c +++ b/sed/mbcs.c @@ -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 */