From: Richard Russon Date: Fri, 19 Jul 2019 21:48:51 +0000 (+0100) Subject: drop Buffer.destroy X-Git-Tag: 2019-10-25~120^2~5 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5c758a8d65babfe755d7c76ab5952bcf44e09f2d;p=neomutt drop Buffer.destroy This member controlled whether, when destroyed, a Buffer should delete its data. The member was only used in a couple of places, so it seems better to duplicate the data in those cases and *always* destroy it afterwards. --- diff --git a/hcache/serialize.c b/hcache/serialize.c index f3f525b03..a786beec2 100644 --- a/hcache/serialize.c +++ b/hcache/serialize.c @@ -318,7 +318,6 @@ unsigned char *serial_dump_buffer(struct Buffer *b, unsigned char *d, int *off, d = serial_dump_char_size(b->data, d, off, b->dsize + 1, convert); d = serial_dump_int(b->dptr - b->data, d, off); d = serial_dump_int(b->dsize, d, off); - d = serial_dump_int(b->destroy, d, off); return d; } @@ -347,8 +346,6 @@ void serial_restore_buffer(struct Buffer **b, const unsigned char *d, int *off, (*b)->dptr = (*b)->data + offset; serial_restore_int(&used, d, off); (*b)->dsize = used; - serial_restore_int(&used, d, off); - (*b)->destroy = used; } /** diff --git a/icommands.c b/icommands.c index 2e1c04166..43a78ef9d 100644 --- a/icommands.c +++ b/icommands.c @@ -78,26 +78,22 @@ enum CommandResult mutt_parse_icommand(/* const */ char *line, struct Buffer *er enum CommandResult rc = MUTT_CMD_ERROR; - struct Buffer expn, token; - - mutt_buffer_init(&expn); - mutt_buffer_init(&token); - - expn.data = expn.dptr = line; - expn.dsize = mutt_str_strlen(line); + struct Buffer *token = mutt_buffer_pool_get(); + struct Buffer *expn = mutt_buffer_from(line); + expn->dptr = expn->data; mutt_buffer_reset(err); - SKIPWS(expn.dptr); - while (*expn.dptr) + SKIPWS(expn->dptr); + while (*expn->dptr != '\0') { - mutt_extract_token(&token, &expn, MUTT_TOKEN_NO_FLAGS); + mutt_extract_token(token, expn, MUTT_TOKEN_NO_FLAGS); for (size_t i = 0; ICommandList[i].name; i++) { - if (mutt_str_strcmp(token.data, ICommandList[i].name) != 0) + if (mutt_str_strcmp(token->data, ICommandList[i].name) != 0) continue; - rc = ICommandList[i].func(&token, &expn, ICommandList[i].data, err); + rc = ICommandList[i].func(token, expn, ICommandList[i].data, err); if (rc != 0) goto finish; @@ -106,8 +102,8 @@ enum CommandResult mutt_parse_icommand(/* const */ char *line, struct Buffer *er } finish: - if (expn.destroy) - FREE(&expn.data); + mutt_buffer_pool_release(&token); + mutt_buffer_free(&expn); return rc; } diff --git a/init.c b/init.c index a5653a409..ff060e5f1 100644 --- a/init.c +++ b/init.c @@ -2773,11 +2773,8 @@ int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags ptr = mutt_mem_malloc(tok->dsize); memcpy(ptr, expn.data, expnlen); strcpy(ptr + expnlen, tok->dptr); - if (tok->destroy) - FREE(&tok->data); - tok->data = ptr; - tok->dptr = ptr; - tok->destroy = 1; /* mark that the caller should destroy this data */ + tok->data = mutt_str_strdup(ptr); + tok->dptr = tok->data; ptr = NULL; FREE(&expn.data); } @@ -3211,36 +3208,33 @@ int mutt_init(bool skip_sys_rc, struct ListHead *commands) enum CommandResult mutt_parse_rc_line(/* const */ char *line, struct Buffer *token, struct Buffer *err) { - int i; - enum CommandResult rc = MUTT_CMD_SUCCESS; - struct Buffer expn; - if (!line || !*line) return 0; - mutt_buffer_init(&expn); - expn.data = line; - expn.dptr = line; - expn.dsize = mutt_str_strlen(line); + int i; + enum CommandResult rc = MUTT_CMD_SUCCESS; + + struct Buffer *expn = mutt_buffer_from(line); + expn->dptr = expn->data; *err->data = 0; - SKIPWS(expn.dptr); - while (*expn.dptr) + SKIPWS(expn->dptr); + while (*expn->dptr != '\0') { - if (*expn.dptr == '#') + if (*expn->dptr == '#') break; /* rest of line is a comment */ - if (*expn.dptr == ';') + if (*expn->dptr == ';') { - expn.dptr++; + expn->dptr++; continue; } - mutt_extract_token(token, &expn, MUTT_TOKEN_NO_FLAGS); + mutt_extract_token(token, expn, MUTT_TOKEN_NO_FLAGS); for (i = 0; Commands[i].name; i++) { if (mutt_str_strcmp(token->data, Commands[i].name) == 0) { - rc = Commands[i].func(token, &expn, Commands[i].data, err); + rc = Commands[i].func(token, expn, Commands[i].data, err); if (rc != MUTT_CMD_SUCCESS) { /* -1 Error, +1 Finish */ goto finish; /* Propagate return code */ @@ -3256,8 +3250,7 @@ enum CommandResult mutt_parse_rc_line(/* const */ char *line, } } finish: - if (expn.destroy) - FREE(&expn.data); + mutt_buffer_free(&expn); return rc; } diff --git a/mutt/buffer.h b/mutt/buffer.h index 826af3596..e74ac334f 100644 --- a/mutt/buffer.h +++ b/mutt/buffer.h @@ -35,7 +35,6 @@ struct Buffer char *data; ///< Pointer to data char *dptr; ///< Current read/write position size_t dsize; ///< Length of data - int destroy; ///< Destroy 'data' when done? }; /* Convert a buffer to a const char * "string" */ diff --git a/test/pattern/extract.c b/test/pattern/extract.c index b49116df4..23e1c9935 100644 --- a/test/pattern/extract.c +++ b/test/pattern/extract.c @@ -191,11 +191,8 @@ int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags ptr = mutt_mem_malloc(tok->dsize); memcpy(ptr, expn.data, expnlen); strcpy(ptr + expnlen, tok->dptr); - if (tok->destroy) - FREE(&tok->data); - tok->data = ptr; - tok->dptr = ptr; - tok->destroy = 1; /* mark that the caller should destroy this data */ + tok->data = mutt_str_strdup(ptr); + tok->dptr = tok->data; ptr = NULL; FREE(&expn.data); }