From c87bc1d55ffc4c746943e863175a3b6aa9462127 Mon Sep 17 00:00:00 2001 From: Hussam al-Homsi Date: Sat, 29 Aug 2020 22:57:45 -0400 Subject: [PATCH] tweaks: stop casting the return of malloc() and friends Those casts are redundant, and sometimes ugly. And as the types of variables are extremely unlikely to change any more at this point, the protection they offer against miscompilations is moot. Signed-off-by: Hussam al-Homsi --- src/browser.c | 12 ++++----- src/chars.c | 2 +- src/color.c | 2 +- src/cut.c | 10 ++++---- src/definitions.h | 4 --- src/files.c | 64 +++++++++++++++++++++++------------------------ src/help.c | 2 +- src/history.c | 6 ++--- src/nano.c | 8 +++--- src/prompt.c | 10 ++++---- src/rcfile.c | 8 +++--- src/search.c | 6 ++--- src/text.c | 60 ++++++++++++++++++++++---------------------- src/utils.c | 6 ++--- src/winio.c | 20 +++++++-------- 15 files changed, 108 insertions(+), 112 deletions(-) diff --git a/src/browser.c b/src/browser.c index 92f838e7..4c8e8c3c 100644 --- a/src/browser.c +++ b/src/browser.c @@ -245,7 +245,7 @@ char *browse(char *path) /* If the given path is relative, join it with the current path. */ if (*path != '/') { - path = charealloc(path, strlen(present_path) + + path = nrealloc(path, strlen(present_path) + strlen(answer) + 1); sprintf(path, "%s%s", present_path, answer); } @@ -359,7 +359,7 @@ char *browse_in(const char *inpath) path = free_and_assign(path, strip_last_component(path)); if (stat(path, &fileinfo) == -1 || !S_ISDIR(fileinfo.st_mode)) { - char *currentdir = charalloc(PATH_MAX + 1); + char *currentdir = nmalloc(PATH_MAX + 1); path = free_and_assign(path, getcwd(currentdir, PATH_MAX + 1)); @@ -422,7 +422,7 @@ void read_the_list(const char *path, DIR *dir) filelist_len = i; - filelist = (char **)nmalloc(filelist_len * sizeof(char *)); + filelist = nmalloc(filelist_len * sizeof(char *)); i = 0; @@ -431,7 +431,7 @@ void read_the_list(const char *path, DIR *dir) if (strcmp(nextdir->d_name, ".") == 0) continue; - filelist[i] = charalloc(path_len + strlen(nextdir->d_name) + 1); + filelist[i] = nmalloc(path_len + strlen(nextdir->d_name) + 1); sprintf(filelist[i], "%s%s", path, nextdir->d_name); i++; @@ -520,7 +520,7 @@ void browser_refresh(void) off_t result = state.st_size; char modifier; - info = charalloc(infomaxlen + 1); + info = nmalloc(infomaxlen + 1); /* Massage the file size into a human-readable form. */ if (state.st_size < (1 << 10)) @@ -617,7 +617,7 @@ int filesearch_init(bool forwards) if (*last_search != '\0') { char *disp = display_string(last_search, 0, COLS / 3, FALSE, FALSE); - thedefault = charalloc(strlen(disp) + 7); + thedefault = nmalloc(strlen(disp) + 7); /* We use (COLS / 3) here because we need to see more on the line. */ sprintf(thedefault, " [%s%s]", disp, (breadth(last_search) > COLS / 3) ? "..." : ""); diff --git a/src/chars.c b/src/chars.c index 619b7fd9..ed888645 100644 --- a/src/chars.c +++ b/src/chars.c @@ -202,7 +202,7 @@ int mbwidth(const char *c) * character and its length. Otherwise, return a length of zero. */ char *make_mbchar(long code, int *length) { - char *mb_char = charalloc(MAXCHARLEN); + char *mb_char = nmalloc(MAXCHARLEN); *length = wctomb(mb_char, (wchar_t)code); diff --git a/src/color.c b/src/color.c index 6e225b20..58803fab 100644 --- a/src/color.c +++ b/src/color.c @@ -243,7 +243,7 @@ void find_and_prime_applicable_syntax(void) /* Allocate and initialize (for the given line) the cache for multiline info. */ void set_up_multicache(linestruct *line) { - line->multidata = (short *)nmalloc(openfile->syntax->nmultis * sizeof(short)); + line->multidata = nmalloc(openfile->syntax->nmultis * sizeof(short)); for (short index = 0; index < openfile->syntax->nmultis; index++) line->multidata[index] = -1; diff --git a/src/cut.c b/src/cut.c index 3f0a5828..63130c2a 100644 --- a/src/cut.c +++ b/src/cut.c @@ -88,7 +88,7 @@ void do_deletion(undo_type action) openfile->current->has_anchor |= joining->has_anchor; #endif /* Add the content of the next line to that of the current one. */ - openfile->current->data = charealloc(openfile->current->data, + openfile->current->data = nrealloc(openfile->current->data, strlen(openfile->current->data) + strlen(joining->data) + 1); strcat(openfile->current->data, joining->data); @@ -289,7 +289,7 @@ void extract_segment(linestruct *top, size_t top_x, linestruct *bot, size_t bot_ if (bot->next) bot->next->prev = top; - top->data = charealloc(top->data, top_x + strlen(bot->data + bot_x) + 1); + top->data = nrealloc(top->data, top_x + strlen(bot->data + bot_x) + 1); strcpy(top->data + top_x, bot->data + bot_x); last = bot; @@ -308,7 +308,7 @@ void extract_segment(linestruct *top, size_t top_x, linestruct *bot, size_t bot_ cutbuffer = taken; cutbottom = last; } else { - cutbottom->data = charealloc(cutbottom->data, + cutbottom->data = nrealloc(cutbottom->data, strlen(cutbottom->data) + strlen(taken->data) + 1); strcat(cutbottom->data, taken->data); @@ -375,7 +375,7 @@ void ingraft_buffer(linestruct *topline) if (extralen > 0) { /* Insert the text of topline at the current cursor position. */ - line->data = charealloc(line->data, length + extralen + 1); + line->data = nrealloc(line->data, length + extralen + 1); memmove(line->data + xpos + extralen, line->data + xpos, length - xpos + 1); strncpy(line->data + xpos, topline->data, extralen); } @@ -401,7 +401,7 @@ void ingraft_buffer(linestruct *topline) /* Add the text after the cursor position at the end of botline. */ length = strlen(botline->data); extralen = strlen(tailtext); - botline->data = charealloc(botline->data, length + extralen + 1); + botline->data = nrealloc(botline->data, length + extralen + 1); strcpy(botline->data + length, tailtext); /* Put the cursor at the end of the grafted text. */ diff --git a/src/definitions.h b/src/definitions.h index 91f10d00..b887663c 100644 --- a/src/definitions.h +++ b/src/definitions.h @@ -56,10 +56,6 @@ #define ISSET(flag) ((FLAGS(flag) & FLAGMASK(flag)) != 0) #define TOGGLE(flag) FLAGS(flag) ^= FLAGMASK(flag) -/* Macros for allocation of character strings. */ -#define charalloc(howmuch) (char *)nmalloc(howmuch) -#define charealloc(ptr, howmuch) (char *)nrealloc(ptr, howmuch) - /* In UTF-8 a character is at most six bytes long. */ #ifdef ENABLE_UTF8 #define MAXCHARLEN 6 diff --git a/src/files.c b/src/files.c index 092ef85a..2d094756 100644 --- a/src/files.c +++ b/src/files.c @@ -159,7 +159,7 @@ bool write_lockfile(const char *lockfilename, const char *filename, bool modifie return FALSE; } - lockdata = charalloc(LOCKSIZE); + lockdata = nmalloc(LOCKSIZE); memset(lockdata, 0, LOCKSIZE); /* This is the lock data we will store (other bytes remain 0x00): @@ -212,7 +212,7 @@ char *do_lockfile(const char *filename, bool ask_the_user) char *secondcopy = copy_of(filename); size_t locknamesize = strlen(filename) + strlen(locking_prefix) + strlen(locking_suffix) + 3; - char *lockfilename = charalloc(locknamesize); + char *lockfilename = nmalloc(locknamesize); struct stat fileinfo; snprintf(lockfilename, locknamesize, "%s/%s%s%s", dirname(namecopy), @@ -235,7 +235,7 @@ char *do_lockfile(const char *filename, bool ask_the_user) return NULL; } - lockbuf = charalloc(LOCKSIZE); + lockbuf = nmalloc(LOCKSIZE); readamt = read(lockfd, lockbuf, LOCKSIZE); @@ -258,7 +258,7 @@ char *do_lockfile(const char *filename, bool ask_the_user) lockuser[16] = '\0'; free(lockbuf); - pidstring = charalloc(11); + pidstring = nmalloc(11); sprintf (pidstring, "%u", (unsigned int)lockpid); /* Display newlines in filenames as ^J. */ @@ -273,7 +273,7 @@ char *do_lockfile(const char *filename, bool ask_the_user) else if (room < breadth(filename)) { char *fragment = display_string(filename, breadth(filename) - room + 3, room, FALSE, FALSE); - postedname = charalloc(strlen(fragment) + 4); + postedname = nmalloc(strlen(fragment) + 4); strcpy(postedname, "..."); strcat(postedname, fragment); free(fragment); @@ -282,7 +282,7 @@ char *do_lockfile(const char *filename, bool ask_the_user) /* Allow extra space for username (14), program name (8), PID (8), * and terminating \0 (1), minus the %s (2) for the file name. */ - promptstr = charalloc(strlen(question) + 29 + strlen(postedname)); + promptstr = nmalloc(strlen(question) + 29 + strlen(postedname)); sprintf(promptstr, question, postedname, lockuser, lockprog, pidstring); free(postedname); free(pidstring); @@ -314,7 +314,7 @@ char *do_lockfile(const char *filename, bool ask_the_user) void stat_with_alloc(const char *filename, struct stat **pstat) { if (*pstat == NULL) - *pstat = (struct stat *)nmalloc(sizeof(struct stat)); + *pstat = nmalloc(sizeof(struct stat)); if (stat(filename, *pstat) != 0) { free(*pstat); @@ -615,7 +615,7 @@ void read_file(FILE *f, int fd, const char *filename, bool undoable) /* The length of the current line of the file. */ size_t bufsize = LUMPSIZE; /* The size of the line buffer; increased as needed. */ - char *buf = charalloc(bufsize); + char *buf = nmalloc(bufsize); /* The buffer in which we assemble each line of the file. */ linestruct *topline; /* The top of the new buffer where we store the read file. */ @@ -689,7 +689,7 @@ void read_file(FILE *f, int fd, const char *filename, bool undoable) * decreasing it -- it gets freed when reading is finished. */ if (len == bufsize) { bufsize += LUMPSIZE; - buf = charealloc(buf, bufsize); + buf = nrealloc(buf, bufsize); } continue; @@ -889,7 +889,7 @@ char *get_next_filename(const char *name, const char *suffix) /* Reserve space for: the name plus the suffix plus a dot plus * possibly five digits plus a null byte. */ - buf = charalloc(wholenamelen + 7); + buf = nmalloc(wholenamelen + 7); sprintf(buf, "%s%s", name, suffix); while (TRUE) { @@ -1307,7 +1307,7 @@ char *get_full_path(const char *origpath) if (origpath == NULL) return NULL; - allocation = charalloc(PATH_MAX + 1); + allocation = nmalloc(PATH_MAX + 1); here = getcwd(allocation, PATH_MAX + 1); /* If getting the current directory failed, go up one level and try again, @@ -1321,7 +1321,7 @@ char *get_full_path(const char *origpath) /* If we found a directory, make sure its path ends in a slash. */ if (here != NULL) { if (strcmp(here, "/") != 0) { - here = charealloc(here, strlen(here) + 2); + here = nrealloc(here, strlen(here) + 2); strcat(here, "/"); } } else { @@ -1340,7 +1340,7 @@ char *get_full_path(const char *origpath) size_t length = strlen(target); if (target[length - 1] != '/') { - target = charealloc(target, length + 2); + target = nrealloc(target, length + 2); strcat(target, "/"); } } @@ -1367,14 +1367,14 @@ char *get_full_path(const char *origpath) } else { free(target); - allocation = charalloc(PATH_MAX + 1); + allocation = nmalloc(PATH_MAX + 1); target = getcwd(allocation, PATH_MAX + 1); /* If we got a result, make sure it ends in a slash. * Otherwise, ensure that we return NULL. */ if (target != NULL) { if (strcmp(target, "/") != 0) { - target = charealloc(target, strlen(target) + 2); + target = nrealloc(target, strlen(target) + 2); strcat(target, "/"); } } else { @@ -1393,7 +1393,7 @@ char *get_full_path(const char *origpath) /* If we were given more than a bare path, concatenate the target path * with the filename portion to get the full, absolute file path. */ if (!path_only && target != NULL) { - target = charealloc(target, strlen(target) + strlen(just_filename) + 1); + target = nrealloc(target, strlen(target) + strlen(just_filename) + 1); strcat(target, just_filename); } @@ -1439,7 +1439,7 @@ char *safe_tempfile(FILE **stream) if (tempdir == NULL) tempdir = copy_of("/tmp/"); - tempfile_name = charealloc(tempdir, strlen(tempdir) + 12); + tempfile_name = nrealloc(tempdir, strlen(tempdir) + 12); strcat(tempfile_name, "nano.XXXXXX"); fd = mkstemp(tempfile_name); @@ -1465,7 +1465,7 @@ void init_operating_dir(void) die(_("Invalid operating directory: %s\n"), operating_dir); free(operating_dir); - operating_dir = charealloc(target, strlen(target) + 1); + operating_dir = nrealloc(target, strlen(target) + 1); } /* Check whether the given path is outside of the operating directory. @@ -1514,7 +1514,7 @@ void init_backup_dir(void) die(_("Invalid backup directory: %s\n"), backup_dir); free(backup_dir); - backup_dir = charealloc(target, strlen(target) + 1); + backup_dir = nrealloc(target, strlen(target) + 1); } #endif @@ -1571,7 +1571,7 @@ bool make_backup_of(char *realname) * by appending a tilde to the original file name. Otherwise, * we create a numbered backup in the specified directory. */ if (backup_dir == NULL) { - backupname = charalloc(strlen(realname) + 2); + backupname = nmalloc(strlen(realname) + 2); sprintf(backupname, "%s~", realname); } else { char *thename = get_full_path(realname); @@ -1586,7 +1586,7 @@ bool make_backup_of(char *realname) } else thename = copy_of(tail(realname)); - backupname = charalloc(strlen(backup_dir) + strlen(thename) + 1); + backupname = nmalloc(strlen(backup_dir) + strlen(thename) + 1); sprintf(backupname, "%s%s", backup_dir, thename); free(thename); @@ -1678,7 +1678,7 @@ bool make_backup_of(char *realname) warn_and_briefly_pause(_("Trying again in your home directory")); currmenu = MMOST; - backupname = charalloc(strlen(homedir) + strlen(tail(realname)) + 9); + backupname = nmalloc(strlen(homedir) + strlen(tail(realname)) + 9); sprintf(backupname, "%s/%s~XXXXXX", homedir, tail(realname)); descriptor = mkstemp(backupname); @@ -2212,7 +2212,7 @@ int do_writeout(bool exiting, bool withprompt) char *question = _("File \"%s\" exists; OVERWRITE? "); char *name = display_string(answer, 0, COLS - breadth(question) + 1, FALSE, FALSE); - char *message = charalloc(strlen(question) + + char *message = nmalloc(strlen(question) + strlen(name) + 1); sprintf(message, question, name); @@ -2330,7 +2330,7 @@ char *real_dir_from_tilde(const char *path) #endif } - retval = charalloc(strlen(tilded) + strlen(path + i) + 1); + retval = nmalloc(strlen(tilded) + strlen(path + i) + 1); sprintf(retval, "%s%s", tilded, path + i); free(tilded); @@ -2393,8 +2393,8 @@ char **username_completion(const char *morsel, size_t length, size_t *num_matche if (outside_of_confinement(userdata->pw_dir, TRUE)) continue; #endif - matches = (char **)nrealloc(matches, (*num_matches + 1) * sizeof(char *)); - matches[*num_matches] = charalloc(strlen(userdata->pw_name) + 2); + matches = nrealloc(matches, (*num_matches + 1) * sizeof(char *)); + matches[*num_matches] = nmalloc(strlen(userdata->pw_name) + 2); sprintf(matches[*num_matches], "~%s", userdata->pw_name); ++(*num_matches); } @@ -2442,7 +2442,7 @@ char **filename_completion(const char *morsel, size_t length, size_t *num_matche dirname = real_dir_from_tilde(dirname); /* A non-absolute path is relative to the current browser directory. */ if (dirname[0] != '/') { - dirname = charealloc(dirname, strlen(present_path) + strlen(wasdirname) + 1); + dirname = nrealloc(dirname, strlen(present_path) + strlen(wasdirname) + 1); sprintf(dirname, "%s%s", present_path, wasdirname); } free(wasdirname); @@ -2468,7 +2468,7 @@ char **filename_completion(const char *morsel, size_t length, size_t *num_matche if (strncmp(entry->d_name, filename, filenamelen) == 0 && strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { - fullname = charealloc(fullname, strlen(dirname) + + fullname = nrealloc(fullname, strlen(dirname) + strlen(entry->d_name) + 1); sprintf(fullname, "%s%s", dirname, entry->d_name); @@ -2480,7 +2480,7 @@ char **filename_completion(const char *morsel, size_t length, size_t *num_matche if (currmenu == MGOTODIR && !is_dir(fullname)) continue; - matches = (char **)nrealloc(matches, (*num_matches + 1) * sizeof(char *)); + matches = nrealloc(matches, (*num_matches + 1) * sizeof(char *)); matches[*num_matches] = copy_of(entry->d_name); ++(*num_matches); } @@ -2551,7 +2551,7 @@ char *input_tab(char *morsel, size_t *place, void (*refresh_func)(void), bool *l common_len += len1; } - shared = charalloc(length_of_path + common_len + 1); + shared = nmalloc(length_of_path + common_len + 1); strncpy(shared, morsel, length_of_path); strncpy(shared + length_of_path, matches[0], common_len); @@ -2560,7 +2560,7 @@ char *input_tab(char *morsel, size_t *place, void (*refresh_func)(void), bool *l shared[common_len] = '\0'; /* Cover also the case of the user specifying a relative path. */ - glued = charalloc(strlen(present_path) + common_len + 1); + glued = nmalloc(strlen(present_path) + common_len + 1); sprintf(glued, "%s%s", present_path, shared); if (num_matches == 1 && (is_dir(shared) || is_dir(glued))) @@ -2568,7 +2568,7 @@ char *input_tab(char *morsel, size_t *place, void (*refresh_func)(void), bool *l /* If the matches have something in common, copy that part. */ if (common_len != *place) { - morsel = charealloc(morsel, common_len + 1); + morsel = nrealloc(morsel, common_len + 1); strncpy(morsel, shared, common_len); morsel[common_len] = '\0'; *place = common_len; diff --git a/src/help.c b/src/help.c index e57c82bd..9b5a8e0d 100644 --- a/src/help.c +++ b/src/help.c @@ -240,7 +240,7 @@ void help_init(void) #endif /* Allocate memory for the help text. */ - help_text = charalloc(allocsize + 1); + help_text = nmalloc(allocsize + 1); /* Now add the text we want. */ strcpy(help_text, htx[0]); diff --git a/src/history.c b/src/history.c index b765c814..2017522b 100644 --- a/src/history.c +++ b/src/history.c @@ -417,7 +417,7 @@ void load_poshistory(void) *(lineptr++) = '\0'; /* Create a new position record. */ - newrecord = (poshiststruct *)nmalloc(sizeof(poshiststruct)); + newrecord = nmalloc(sizeof(poshiststruct)); newrecord->filename = copy_of(line); newrecord->lineno = atoi(lineptr); newrecord->xno = atoi(xptr); @@ -472,7 +472,7 @@ void save_poshistory(void) /* Assume 20 decimal positions each for line and column number, * plus two spaces, plus the line feed, plus the null byte. */ - path_and_place = charalloc(strlen(posptr->filename) + 44); + path_and_place = nmalloc(strlen(posptr->filename) + 44); sprintf(path_and_place, "%s %zd %zd\n", posptr->filename, posptr->lineno, posptr->xno); length = strlen(path_and_place); @@ -556,7 +556,7 @@ void update_poshistory(void) /* If we didn't find it, make a new node; otherwise, if we're * not at the end, move the matching one to the end. */ if (theone == NULL) { - theone = (poshiststruct *)nmalloc(sizeof(poshiststruct)); + theone = nmalloc(sizeof(poshiststruct)); theone->filename = copy_of(fullpath); if (position_history == NULL) position_history = theone; diff --git a/src/nano.c b/src/nano.c index 4da79678..6ba81804 100644 --- a/src/nano.c +++ b/src/nano.c @@ -1403,7 +1403,7 @@ void suck_up_input_and_paste_it(void) index = 0; } else if ((0x20 <= input && input <= 0xFF && input != DEL_CODE) || input == '\t') { - line->data = charealloc(line->data, index + 2); + line->data = nrealloc(line->data, index + 2); line->data[index++] = (char)input; line->data[index] = '\0'; } else if (input != BRACKETED_PASTE_MARKER) @@ -1447,7 +1447,7 @@ void inject(char *burst, size_t count) #endif /* Make room for the new bytes and copy them into the line. */ - thisline->data = charealloc(thisline->data, datalen + count + 1); + thisline->data = nrealloc(thisline->data, datalen + count + 1); memmove(thisline->data + openfile->current_x + count, thisline->data + openfile->current_x, datalen - openfile->current_x + 1); @@ -1559,7 +1559,7 @@ void process_a_keystroke(void) } #endif /* Store the byte, and leave room for a terminating zero. */ - puddle = charealloc(puddle, depth + 2); + puddle = nrealloc(puddle, depth + 2); puddle[depth++] = (char)input; } } @@ -2233,7 +2233,7 @@ int main(int argc, char **argv) quoterc = regcomp("ereg, quotestr, NANO_REG_EXTENDED); if (quoterc != 0) { size_t size = regerror(quoterc, "ereg, NULL, 0); - char *message = charalloc(size); + char *message = nmalloc(size); regerror(quoterc, "ereg, message, size); die(_("Bad quoting regex \"%s\": %s\n"), quotestr, message); diff --git a/src/prompt.c b/src/prompt.c index d8d422b2..dc47a0c0 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -141,7 +141,7 @@ void do_statusbar_cut_text(void) void paste_into_answer(void) { size_t pastelen = strlen(cutbuffer->data); - char *fusion = charalloc(strlen(answer) + pastelen + 1); + char *fusion = nmalloc(strlen(answer) + pastelen + 1); /* Concatenate: the current answer before the cursor, the first line * of the cutbuffer, plus the rest of the current answer. */ @@ -184,7 +184,7 @@ void inject_into_answer(char *burst, size_t count) if (burst[index] == '\0') burst[index] = '\n'; - answer = charealloc(answer, strlen(answer) + count + 1); + answer = nrealloc(answer, strlen(answer) + count + 1); memmove(answer + typing_x + count, answer + typing_x, strlen(answer) - typing_x + 1); strncpy(answer + typing_x, burst , count); @@ -253,7 +253,7 @@ int do_statusbar_input(bool *finished) beep(); else if (!ISSET(RESTRICTED) || currmenu != MWRITEFILE || openfile->filename[0] == '\0') { - puddle = charealloc(puddle, depth + 2); + puddle = nrealloc(puddle, depth + 2); puddle[depth++] = (char)input; } } @@ -397,7 +397,7 @@ void add_or_remove_pipe_symbol_from_answer(void) if (typing_x > 0) typing_x--; } else { - answer = charealloc(answer, strlen(answer) + 2); + answer = nrealloc(answer, strlen(answer) + 2); memmove(answer + 1, answer, strlen(answer) + 1); answer[0] = '|'; typing_x++; @@ -566,7 +566,7 @@ int do_prompt(int menu, const char *provided, linestruct **history_list, #ifndef NANO_TINY redo_theprompt: #endif - prompt = charalloc((COLS * MAXCHARLEN) + 1); + prompt = nmalloc((COLS * MAXCHARLEN) + 1); va_start(ap, msg); vsnprintf(prompt, COLS * MAXCHARLEN, msg, ap); va_end(ap); diff --git a/src/rcfile.c b/src/rcfile.c index c259cc7d..edc2853e 100644 --- a/src/rcfile.c +++ b/src/rcfile.c @@ -612,7 +612,7 @@ bool compile(const char *expression, int rex_flags, regex_t **packed) if (outcome != 0) { size_t length = regerror(outcome, compiled, NULL, 0); - char *message = charalloc(length); + char *message = nmalloc(length); regerror(outcome, compiled, message, length); jot_error(N_("Bad regex \"%s\": %s"), expression, message); @@ -662,7 +662,7 @@ void begin_new_syntax(char *ptr) } /* Initialize a new syntax struct. */ - live_syntax = (syntaxtype *)nmalloc(sizeof(syntaxtype)); + live_syntax = nmalloc(sizeof(syntaxtype)); live_syntax->name = copy_of(nameptr); live_syntax->filename = copy_of(nanorc); live_syntax->lineno = lineno; @@ -1166,7 +1166,7 @@ void parse_rule(char *ptr, int rex_flags) } /* Allocate a rule, fill in the data, and link it into the list. */ - newcolor = (colortype *)nmalloc(sizeof(colortype)); + newcolor = nmalloc(sizeof(colortype)); newcolor->start = start_rgx; newcolor->end = end_rgx; @@ -1248,7 +1248,7 @@ void grab_and_store(const char *kind, char *ptr, regexlisttype **storage) continue; /* Copy the regex into a struct, and hook this in at the end. */ - newthing = (regexlisttype *)nmalloc(sizeof(regexlisttype)); + newthing = nmalloc(sizeof(regexlisttype)); newthing->full_regex = copy_of(regexstring); newthing->next = NULL; diff --git a/src/search.c b/src/search.c index 8a8d8eee..cc427cc2 100644 --- a/src/search.c +++ b/src/search.c @@ -38,7 +38,7 @@ bool regexp_init(const char *regexp) /* If regex compilation failed, show the error message. */ if (value != 0) { size_t len = regerror(value, &search_regexp, NULL, 0); - char *str = charalloc(len); + char *str = nmalloc(len); regerror(value, &search_regexp, str, len); statusline(ALERT, _("Bad regex \"%s\": %s"), regexp, str); @@ -78,7 +78,7 @@ void search_init(bool replacing, bool retain_answer) if (*last_search != '\0') { char *disp = display_string(last_search, 0, COLS / 3, FALSE, FALSE); - thedefault = charalloc(strlen(disp) + 7); + thedefault = nmalloc(strlen(disp) + 7); /* We use (COLS / 3) here because we need to see more on the line. */ sprintf(thedefault, " [%s%s]", disp, (breadth(last_search) > COLS / 3) ? "..." : ""); @@ -489,7 +489,7 @@ char *replace_line(const char *needle) new_size += strlen(answer) - match_len; } - copy = charalloc(new_size); + copy = nmalloc(new_size); /* Copy the head of the original line. */ strncpy(copy, openfile->current->data, openfile->current_x); diff --git a/src/text.c b/src/text.c index 4b1f8030..f10b30d5 100644 --- a/src/text.c +++ b/src/text.c @@ -70,7 +70,7 @@ void do_tab(void) #endif #ifndef NANO_TINY if (ISSET(TABS_TO_SPACES)) { - char *spaces = charalloc(tabsize + 1); + char *spaces = nmalloc(tabsize + 1); size_t length = tabsize - (xplustabs() % tabsize); memset(spaces, ' ', length); @@ -96,7 +96,7 @@ void indent_a_line(linestruct *line, char *indentation) return; /* Add the fabricated indentation to the beginning of the line. */ - line->data = charealloc(line->data, length + indent_len + 1); + line->data = nrealloc(line->data, length + indent_len + 1); memmove(line->data + indent_len, line->data, length + 1); memcpy(line->data, indentation, indent_len); @@ -133,7 +133,7 @@ void do_indent(void) if (top == bot->next) return; - indentation = charalloc(tabsize + 1); + indentation = nmalloc(tabsize + 1); /* Set the indentation to either a bunch of spaces or a single tab. */ #ifdef ENABLE_COLOR @@ -331,7 +331,7 @@ bool comment_line(undo_type action, linestruct *line, const char *comment_seq) if (action == COMMENT) { /* Make room for the comment sequence(s), move the text right and * copy them in. */ - line->data = charealloc(line->data, line_len + pre_len + post_len + 1); + line->data = nrealloc(line->data, line_len + pre_len + post_len + 1); memmove(line->data + pre_len, line->data, line_len + 1); memmove(line->data, comment_seq, pre_len); if (post_len > 0) @@ -543,7 +543,7 @@ void do_undo(void) * case, adjust the positions to return to and to scoop data from. */ original_x = (u->head_x == 0) ? u->tail_x : u->head_x; regain_from_x = (u->head_x == 0) ? 0 : u->tail_x; - line->data = charealloc(line->data, strlen(line->data) + + line->data = nrealloc(line->data, strlen(line->data) + strlen(&u->strdata[regain_from_x]) + 1); strcat(line->data, &u->strdata[regain_from_x]); line->has_anchor |= line->next->has_anchor; @@ -554,7 +554,7 @@ void do_undo(void) case BACK: case DEL: undidmsg = _("deletion"); - data = charalloc(strlen(line->data) + strlen(u->strdata) + 1); + data = nmalloc(strlen(line->data) + strlen(u->strdata) + 1); strncpy(data, line->data, u->head_x); strcpy(&data[u->head_x], u->strdata); strcpy(&data[u->head_x + strlen(u->strdata)], &line->data[u->head_x]); @@ -711,7 +711,7 @@ void do_redo(void) redidmsg = _("addition"); if ((u->xflags & INCLUDED_LAST_LINE) && !ISSET(NO_NEWLINES)) new_magicline(); - data = charalloc(strlen(line->data) + strlen(u->strdata) + 1); + data = nmalloc(strlen(line->data) + strlen(u->strdata) + 1); strncpy(data, line->data, u->head_x); strcpy(&data[u->head_x], u->strdata); strcpy(&data[u->head_x + strlen(u->strdata)], &line->data[u->head_x]); @@ -746,7 +746,7 @@ void do_redo(void) goto_line_posx(u->tail_lineno, u->tail_x); break; } - line->data = charealloc(line->data, strlen(line->data) + strlen(u->strdata) + 1); + line->data = nrealloc(line->data, strlen(line->data) + strlen(u->strdata) + 1); strcat(line->data, u->strdata); unlink_node(line->next); renumber_from(line); @@ -876,7 +876,7 @@ void do_enter(void) allblanks = TRUE; } #endif /* NANO_TINY */ - newnode->data = charalloc(strlen(openfile->current->data + + newnode->data = nmalloc(strlen(openfile->current->data + openfile->current_x) + extra + 1); strcpy(&newnode->data[extra], openfile->current->data + openfile->current_x); @@ -1112,7 +1112,7 @@ void update_multiline_undo(ssize_t lineno, char *indentation) u->grouping->bottom_line++; number_of_lines = u->grouping->bottom_line - u->grouping->top_line + 1; - u->grouping->indentations = (char **)nrealloc(u->grouping->indentations, + u->grouping->indentations = nrealloc(u->grouping->indentations, number_of_lines * sizeof(char *)); u->grouping->indentations[number_of_lines - 1] = copy_of(indentation); } else { @@ -1123,7 +1123,7 @@ void update_multiline_undo(ssize_t lineno, char *indentation) born->top_line = lineno; born->bottom_line = lineno; - u->grouping->indentations = (char **)nmalloc(sizeof(char *)); + u->grouping->indentations = nmalloc(sizeof(char *)); u->grouping->indentations[0] = copy_of(indentation); } @@ -1148,7 +1148,7 @@ void update_undo(undo_type action) switch (u->type) { case ADD: newlen = openfile->current_x - u->head_x; - u->strdata = charealloc(u->strdata, newlen + 1); + u->strdata = nrealloc(u->strdata, newlen + 1); strncpy(u->strdata, openfile->current->data + u->head_x, newlen); u->strdata[newlen] = '\0'; u->tail_x = openfile->current_x; @@ -1164,13 +1164,13 @@ void update_undo(undo_type action) datalen = strlen(u->strdata); if (openfile->current_x == u->head_x) { /* They deleted more: add removed character after earlier stuff. */ - u->strdata = charealloc(u->strdata, datalen + charlen + 1); + u->strdata = nrealloc(u->strdata, datalen + charlen + 1); strncpy(u->strdata + datalen, textposition, charlen); u->strdata[datalen + charlen] = '\0'; u->tail_x = openfile->current_x; } else if (openfile->current_x == u->head_x - charlen) { /* They backspaced further: add removed character before earlier. */ - u->strdata = charealloc(u->strdata, datalen + charlen + 1); + u->strdata = nrealloc(u->strdata, datalen + charlen + 1); memmove(u->strdata + charlen, u->strdata, datalen + 1); strncpy(u->strdata, textposition, charlen); u->head_x = openfile->current_x; @@ -1302,7 +1302,7 @@ bool do_wrap(void) #ifndef NANO_TINY add_undo(ADD, NULL); #endif - line->data = charealloc(line->data, line_len + 2); + line->data = nrealloc(line->data, line_len + 2); line->data[line_len] = ' '; line->data[line_len + 1] = '\0'; rest_length++; @@ -1352,7 +1352,7 @@ bool do_wrap(void) if (quot_len > 0) { line = line->next; line_len = strlen(line->data); - line->data = charealloc(line->data, lead_len + line_len + 1); + line->data = nrealloc(line->data, lead_len + line_len + 1); memmove(line->data + lead_len, line->data, line_len + 1); strncpy(line->data, line->prev->data, lead_len); @@ -1586,12 +1586,12 @@ void concat_paragraph(linestruct *line, size_t count) /* We're just about to tack the next line onto this one. If * this line isn't empty, make sure it ends in a space. */ if (line_len > 0 && line->data[line_len - 1] != ' ') { - line->data = charealloc(line->data, line_len + 2); + line->data = nrealloc(line->data, line_len + 2); line->data[line_len++] = ' '; line->data[line_len] = '\0'; } - line->data = charealloc(line->data, + line->data = nrealloc(line->data, line_len + next_line_len - next_lead_len + 1); strcat(line->data, next_line->data + next_lead_len); #ifndef NANO_TINY @@ -1688,7 +1688,7 @@ void rewrap_paragraph(linestruct **line, char *lead_string, size_t lead_len) /* Insert a new line after the current one, and copy the leading part * plus the text after the breaking point into it. */ splice_node(*line, make_new_node(*line)); - (*line)->next->data = charalloc(lead_len + line_len - break_pos + 1); + (*line)->next->data = nmalloc(lead_len + line_len - break_pos + 1); strncpy((*line)->next->data, lead_string, lead_len); strcpy((*line)->next->data + lead_len, (*line)->data + break_pos); @@ -1838,7 +1838,7 @@ void do_justify(bool full_justify) other_white_len = indent_length(sampleline->data + other_quot_len); secondary_len = quot_len + other_white_len; - secondary_lead = charalloc(secondary_len + 1); + secondary_lead = nmalloc(secondary_len + 1); strncpy(secondary_lead, startline->data, quot_len); strncpy(secondary_lead + quot_len, sampleline->data + other_quot_len, @@ -1919,7 +1919,7 @@ void do_justify(bool full_justify) /* Then copy back in the leading part that it should have. */ if (primary_len > 0) { - line->data = charealloc(line->data, primary_len + text_len + 1); + line->data = nrealloc(line->data, primary_len + text_len + 1); memmove(line->data + primary_len, line->data, text_len + 1); strncpy(line->data, primary_lead, primary_len); } @@ -2036,7 +2036,7 @@ void construct_argument_list(char ***arguments, char *command, char *filename) int count = 2; while (element != NULL) { - *arguments = (char **)nrealloc(*arguments, ++count * sizeof(char *)); + *arguments = nrealloc(*arguments, ++count * sizeof(char *)); (*arguments)[count - 3] = element; element = strtok(NULL, " "); } @@ -2428,13 +2428,13 @@ void do_int_speller(const char *tempfile_name) totalread = 0; buffersize = pipesize + 1; - misspellings = charalloc(buffersize); + misspellings = nmalloc(buffersize); pointer = misspellings; while ((bytesread = read(uniq_fd[0], pointer, pipesize)) > 0) { totalread += bytesread; buffersize += pipesize; - misspellings = charealloc(misspellings, buffersize); + misspellings = nrealloc(misspellings, buffersize); pointer = misspellings + totalread; } @@ -2643,13 +2643,13 @@ void do_linter(void) /* Read in the returned syntax errors. */ totalread = 0; buffersize = pipesize + 1; - lintings = charalloc(buffersize); + lintings = nmalloc(buffersize); pointer = lintings; while ((bytesread = read(lint_fd[0], pointer, pipesize)) > 0) { totalread += bytesread; buffersize += pipesize; - lintings = charealloc(lintings, buffersize); + lintings = nrealloc(lintings, buffersize); pointer = lintings + totalread; } @@ -2767,7 +2767,7 @@ void do_linter(void) if (openfile->statinfo == NULL || openfile->statinfo->st_ino != lintfileinfo.st_ino) { - char *msg = charalloc(1024 + strlen(curlint->filename)); + char *msg = nmalloc(1024 + strlen(curlint->filename)); int choice; sprintf(msg, _("This message is for unopened file %s," @@ -3031,7 +3031,7 @@ char *copy_completion(char *text) length = step_right(text, length); /* Now copy this candidate to a new string. */ - word = charalloc(length + 1); + word = nmalloc(length + 1); while (index < length) word[index++] = *(text++); word[index] = '\0'; @@ -3095,7 +3095,7 @@ void complete_a_word(void) return; } - shard = charalloc(openfile->current_x - start_of_shard + 1); + shard = nmalloc(openfile->current_x - start_of_shard + 1); /* Copy the fragment that has to be searched for. */ while (start_of_shard < openfile->current_x) @@ -3150,7 +3150,7 @@ void complete_a_word(void) } /* Add the found word to the list of completions. */ - some_word = (completion_word *)nmalloc(sizeof(completion_word)); + some_word = nmalloc(sizeof(completion_word)); some_word->word = completion; some_word->next = list_of_completions; list_of_completions = some_word; diff --git a/src/utils.c b/src/utils.c index 2b8dc2c4..90238c2a 100644 --- a/src/utils.c +++ b/src/utils.c @@ -68,7 +68,7 @@ const char *tail(const char *path) char *concatenate(const char *path, const char *name) { size_t pathlen = strlen(path); - char *joined = charalloc(pathlen + strlen(name) + 1); + char *joined = nmalloc(pathlen + strlen(name) + 1); strcpy(joined, path); strcpy(joined + pathlen, name); @@ -318,7 +318,7 @@ char *mallocstrcpy(char *dest, const char *src) { size_t count = strlen(src) + 1; - dest = charealloc(dest, count); + dest = nrealloc(dest, count); strncpy(dest, src, count); return dest; @@ -328,7 +328,7 @@ char *mallocstrcpy(char *dest, const char *src) * of the given string, and NUL-terminate the copy. */ char *measured_copy(const char *string, size_t count) { - char *thecopy = charalloc(count + 1); + char *thecopy = nmalloc(count + 1); memcpy(thecopy, string, count); thecopy[count] = '\0'; diff --git a/src/winio.c b/src/winio.c index a174e37b..33270e19 100644 --- a/src/winio.c +++ b/src/winio.c @@ -73,7 +73,7 @@ static size_t macro_length = 0; void add_to_macrobuffer(int code) { macro_length++; - macro_buffer = (int*)nrealloc(macro_buffer, macro_length * sizeof(int)); + macro_buffer = nrealloc(macro_buffer, macro_length * sizeof(int)); macro_buffer[macro_length - 1] = code; } @@ -114,7 +114,7 @@ void run_macro(void) return; } - key_buffer = (int *)nrealloc(key_buffer, macro_length * sizeof(int)); + key_buffer = nrealloc(key_buffer, macro_length * sizeof(int)); key_buffer_len = macro_length; for (size_t i = 0; i < macro_length; i++) @@ -204,7 +204,7 @@ void read_keys_from(WINDOW *win) curs_set(0); /* Initiate the keystroke buffer, and save the keycode in it. */ - key_buffer = (int *)nrealloc(key_buffer, sizeof(int)); + key_buffer = nrealloc(key_buffer, sizeof(int)); key_buffer[0] = input; key_buffer_len = 1; @@ -235,7 +235,7 @@ void read_keys_from(WINDOW *win) /* Extend the keystroke buffer, and save the keycode at its end. */ key_buffer_len++; - key_buffer = (int *)nrealloc(key_buffer, key_buffer_len * sizeof(int)); + key_buffer = nrealloc(key_buffer, key_buffer_len * sizeof(int)); key_buffer[key_buffer_len - 1] = input; } @@ -264,7 +264,7 @@ void put_back(int keycode) return; /* Extend the keystroke buffer to make room for the extra keycode. */ - key_buffer = (int *)nrealloc(key_buffer, ++key_buffer_len * sizeof(int)); + key_buffer = nrealloc(key_buffer, ++key_buffer_len * sizeof(int)); /* If the keystroke buffer wasn't empty before, move all the * existing content one step further away. */ @@ -1377,7 +1377,7 @@ int *parse_verbatim_kbinput(WINDOW *win, size_t *count) #endif /* Reserve ample space for the possible result. */ - yield = (int *)nmalloc(6 * sizeof(int)); + yield = nmalloc(6 * sizeof(int)); #ifdef ENABLE_UTF8 /* If the first code is a valid Unicode starter digit (0 or 1), @@ -1442,7 +1442,7 @@ int *parse_verbatim_kbinput(WINDOW *win, size_t *count) * an escape sequence, and return the resulting number of bytes in count. */ char *get_verbatim_kbinput(WINDOW *win, size_t *count) { - char *bytes = charalloc(MAXCHARLEN + 2); + char *bytes = nmalloc(MAXCHARLEN + 2); int *input; /* Turn off flow control characters if necessary so that we can type @@ -1722,7 +1722,7 @@ char *display_string(const char *buf, size_t column, size_t span, buf += start_index; /* Allocate enough space for converting the relevant part of the line. */ - converted = charalloc(strlen(buf) * (MAXCHARLEN + tabsize) + 1); + converted = nmalloc(strlen(buf) * (MAXCHARLEN + tabsize) + 1); #ifndef NANO_TINY if (span > HIGHEST_POSITIVE) { @@ -1954,7 +1954,7 @@ void titlebar(const char *path) #ifdef ENABLE_MULTIBUFFER /* If there are/were multiple buffers, show which out of how many. */ if (more_than_one) { - ranking = charalloc(24); + ranking = nmalloc(24); sprintf(ranking, "[%i/%i]", buffer_number(openfile), buffer_number(startfile->prev)); upperleft = ranking; @@ -2092,7 +2092,7 @@ void statusline(message_type importance, const char *msg, ...) blank_statusbar(); /* Construct the message out of all the arguments. */ - compound = charalloc(MAXCHARLEN * (COLS + 1)); + compound = nmalloc(MAXCHARLEN * (COLS + 1)); va_start(ap, msg); vsnprintf(compound, MAXCHARLEN * (COLS + 1), msg, ap); va_end(ap);