Merge #2473: Fix clang analysis warnings. (10)

Reviewed-by: oni-link <knil.ino@gmail.com>
Reviewed-by: Scott Prager <splinterofchaos@gmail.com>
This commit is contained in:
Eliseo Martínez 2015-04-23 17:54:00 +02:00
commit 5f34f1b80f
2 changed files with 49 additions and 48 deletions

View File

@ -10,6 +10,7 @@
* ex_getln.c: Functions for entering and editing an Ex command line. * ex_getln.c: Functions for entering and editing an Ex command line.
*/ */
#include <assert.h>
#include <errno.h> #include <errno.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
@ -4169,58 +4170,59 @@ static char_u *get_history_arg(expand_T *xp, int idx)
return NULL; return NULL;
} }
/* /// Initialize command line history.
* init_history() - Initialize the command line history. /// Also used to re-allocate history tables when size changes.
* Also used to re-allocate the history when the size changes.
*/
void init_history(void) void init_history(void)
{ {
/* assert(p_hi >= 0 && p_hi <= INT_MAX);
* If size of history table changed, reallocate it int newlen = (int)p_hi;
*/ int oldlen = hislen;
ssize_t newlen = p_hi;
if (newlen != hislen) {
histentry_T *temp;
ssize_t i;
ssize_t j;
// adjust the tables // If history tables size changed, reallocate them.
for (int type = 0; type < HIST_COUNT; ++type) { // Tables are circular arrays (current position marked by hisidx[type]).
if (newlen) { // On copying them to the new arrays, we take the chance to reorder them.
temp = xmalloc(newlen * sizeof(*temp)); if (newlen != oldlen) {
} else for (int type = 0; type < HIST_COUNT; type++) {
temp = NULL; histentry_T *temp = newlen ? xmalloc(newlen * sizeof(*temp)) : NULL;
if (newlen == 0 || temp != NULL) {
if (hisidx[type] < 0) { /* there are no entries yet */ int j = hisidx[type];
for (i = 0; i < newlen; ++i) if (j >= 0) {
clear_hist_entry(&temp[i]); // old array gets partitioned this way:
} else if (newlen > hislen) { /* array becomes bigger */ // [0 , i1 ) --> newest entries to be deleted
for (i = 0; i <= hisidx[type]; ++i) // [i1 , i1 + l1) --> newest entries to be copied
temp[i] = history[type][i]; // [i1 + l1 , i2 ) --> oldest entries to be deleted
j = i; // [i2 , i2 + l2) --> oldest entries to be copied
for (; i <= newlen - (hislen - hisidx[type]); ++i) int l1 = MIN(j + 1, newlen); // how many newest to copy
clear_hist_entry(&temp[i]); int l2 = MIN(newlen, oldlen) - l1; // how many oldest to copy
for (; j < hislen; ++i, ++j) int i1 = j + 1 - l1; // copy newest from here
temp[i] = history[type][j]; int i2 = MAX(l1, oldlen - newlen + l1); // copy oldest from here
} else { /* array becomes smaller or 0 */
j = hisidx[type]; // copy as much entries as they fit to new table, reordering them
for (i = newlen - 1;; --i) { if (newlen) {
if (i >= 0) /* copy newest entries */ // copy oldest entries
temp[i] = history[type][j]; memcpy(&temp[0], &history[type][i2], (size_t)l2 * sizeof(*temp));
else { /* remove older entries */ // copy newest entries
xfree(history[type][j].hisstr); memcpy(&temp[l2], &history[type][i1], (size_t)l1 * sizeof(*temp));
history[type][j].hisstr = NULL; }
}
if (--j < 0) // delete entries that don't fit in newlen, if any
j = hislen - 1; for (int i = 0; i < i1; i++) {
if (j == hisidx[type]) xfree(history[type][i].hisstr);
break; history[type][i].hisstr = NULL;
} }
hisidx[type] = newlen - 1; for (int i = i1 + l1; i < i2; i++) {
xfree(history[type][i].hisstr);
history[type][i].hisstr = NULL;
} }
xfree(history[type]);
history[type] = temp;
} }
// clear remaining space, if any
int l3 = j < 0 ? 0 : MIN(newlen, oldlen); // number of copied entries
memset(temp + l3, 0, (size_t)(newlen - l3) * sizeof(*temp));
hisidx[type] = l3 - 1;
xfree(history[type]);
history[type] = temp;
} }
hislen = newlen; hislen = newlen;
} }

View File

@ -2477,7 +2477,6 @@ static void op_yank_reg(oparg_T *oap, bool message, yankreg_T *reg, bool append)
curr->y_array[j++] = reg->y_array[y_idx++]; curr->y_array[j++] = reg->y_array[y_idx++];
curr->y_size = j; curr->y_size = j;
xfree(reg->y_array); xfree(reg->y_array);
reg = curr;
} }
if (curwin->w_p_rnu) { if (curwin->w_p_rnu) {
redraw_later(SOME_VALID); // cursor moved to start redraw_later(SOME_VALID); // cursor moved to start