From 40e69a6e953e6a7180682bf2e820753da51572d3 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 16 Oct 2015 04:57:52 +1100 Subject: Fix crash pressing +/- in file-selector Filenames over 128 chars would crash. Move BLI_newname into file_ops, this was only used in one place and isn't all that re-usable. Also remove special behavior for 4 digits. --- source/blender/blenlib/intern/path_util.c | 25 --------------------- source/blender/editors/space_file/file_ops.c | 33 +++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 26 deletions(-) (limited to 'source/blender') diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c index 902de796572..951c88e31af 100644 --- a/source/blender/blenlib/intern/path_util.c +++ b/source/blender/blenlib/intern/path_util.c @@ -190,31 +190,6 @@ int BLI_split_name_num(char *left, int *nr, const char *name, const char delim) return name_len; } -/** - * Looks for a string of digits within name (using BLI_stringdec) and adjusts it by add. - */ -void BLI_newname(char *name, int add) -{ - char head[UNIQUE_NAME_MAX], tail[UNIQUE_NAME_MAX]; - int pic; - unsigned short digits; - - pic = BLI_stringdec(name, head, tail, &digits); - - /* are we going from 100 -> 99 or from 10 -> 9 */ - if (add < 0 && digits < 4 && digits > 0) { - int i, exp; - exp = 1; - for (i = digits; i > 1; i--) exp *= 10; - if (pic >= exp && (pic + add) < exp) digits--; - } - - pic += add; - - if (digits == 4 && pic < 0) pic = 0; - BLI_stringenc(name, head, tail, digits, pic); -} - /** * Ensures name is unique (according to criteria specified by caller in unique_check callback), * incrementing its numeric suffix as necessary. Returns true if name had to be adjusted. diff --git a/source/blender/editors/space_file/file_ops.c b/source/blender/editors/space_file/file_ops.c index 08b6564ec97..328b09dce49 100644 --- a/source/blender/editors/space_file/file_ops.c +++ b/source/blender/editors/space_file/file_ops.c @@ -1973,6 +1973,37 @@ void FILE_OT_bookmark_toggle(struct wmOperatorType *ot) } +/** + * Looks for a string of digits within name (using BLI_stringdec) and adjusts it by add. + */ +static void filenum_newname(char *name, size_t name_size, int add) +{ + char head[FILE_MAXFILE], tail[FILE_MAXFILE]; + char name_temp[FILE_MAXFILE]; + int pic; + unsigned short digits; + + pic = BLI_stringdec(name, head, tail, &digits); + + /* are we going from 100 -> 99 or from 10 -> 9 */ + if (add < 0 && digits > 0) { + int i, exp; + exp = 1; + for (i = digits; i > 1; i--) { + exp *= 10; + } + if (pic >= exp && (pic + add) < exp) { + digits--; + } + } + + pic += add; + if (pic < 0) + pic = 0; + BLI_stringenc(name_temp, head, tail, digits, pic); + BLI_strncpy(name, name_temp, name_size); +} + static int file_filenum_exec(bContext *C, wmOperator *op) { SpaceFile *sfile = CTX_wm_space_file(C); @@ -1980,7 +2011,7 @@ static int file_filenum_exec(bContext *C, wmOperator *op) int inc = RNA_int_get(op->ptr, "increment"); if (sfile->params && (inc != 0)) { - BLI_newname(sfile->params->file, inc); + filenum_newname(sfile->params->file, sizeof(sfile->params->file), inc); ED_area_tag_redraw(sa); file_draw_check(C); // WM_event_add_notifier(C, NC_WINDOW, NULL); -- cgit v1.2.3