diff options
author | Campbell Barton <ideasman42@gmail.com> | 2011-02-13 06:21:27 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2011-02-13 06:21:27 +0300 |
commit | 867fc4b463ef39ea16103f18f332c3d259624d29 (patch) | |
tree | 7d20c416241afb7b878b767a9955e284d3cddbe2 /source/blender/blenlib | |
parent | 9e03a0d4762b4734fe7ccb20e03b4a3c8f939620 (diff) |
enforce string limits (reported by pedantic checking tools & some developers).
mostly replace strcpy with BLI_strncpy and multiple strcat's with a BLI_snprintf().
also fix possible crash if CWD isnt available.
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_path_util.h | 6 | ||||
-rw-r--r-- | source/blender/blenlib/intern/bpath.c | 2 | ||||
-rw-r--r-- | source/blender/blenlib/intern/path_util.c | 92 | ||||
-rw-r--r-- | source/blender/blenlib/intern/storage.c | 2 |
4 files changed, 53 insertions, 49 deletions
diff --git a/source/blender/blenlib/BLI_path_util.h b/source/blender/blenlib/BLI_path_util.h index 85286d15acb..fd710c59e2f 100644 --- a/source/blender/blenlib/BLI_path_util.h +++ b/source/blender/blenlib/BLI_path_util.h @@ -103,7 +103,7 @@ void BLI_make_file_string(const char *relabase, char *string, const char *dir, void BLI_make_exist(char *dir); void BLI_make_existing_file(const char *name); void BLI_split_dirfile(const char *string, char *dir, char *file); -void BLI_join_dirfile(char *string, const char *dir, const char *file); +void BLI_join_dirfile(char *string, const int maxlen, const char *dir, const char *file); char *BLI_path_basename(char *path); int BKE_rebase_path(char *abs, int abs_size, char *rel, int rel_size, const char *base_dir, const char *src_dir, const char *dest_dir); char *BLI_last_slash(const char *string); @@ -181,7 +181,7 @@ void BLI_char_switch(char *string, char from, char to); * @param fullname The full path and full name of the executable * @param name The name of the executable (usually argv[0]) to be checked */ -void BLI_where_am_i(char *fullname, const char *name); +void BLI_where_am_i(char *fullname, const int maxlen, const char *name); char *get_install_dir(void); /** @@ -192,7 +192,7 @@ char *get_install_dir(void); * * @param fullname The full path to the temp directory */ -void BLI_where_is_temp(char *fullname, int usertemp); +void BLI_where_is_temp(char *fullname, const int maxlen, int usertemp); #ifdef WITH_ICONV diff --git a/source/blender/blenlib/intern/bpath.c b/source/blender/blenlib/intern/bpath.c index b145eabed94..b1e5561b0cd 100644 --- a/source/blender/blenlib/intern/bpath.c +++ b/source/blender/blenlib/intern/bpath.c @@ -808,7 +808,7 @@ static int findFileRecursive(char *filename_new, const char *dirname, const char if (strcmp(".", de->d_name)==0 || strcmp("..", de->d_name)==0) continue; - BLI_join_dirfile(path, dirname, de->d_name); + BLI_join_dirfile(path, sizeof(path), dirname, de->d_name); if (stat(path, &status) != 0) continue; /* cant stat, dont bother with this file, could print debug info here */ diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c index eae50b309b1..e44e3e92cdb 100644 --- a/source/blender/blenlib/intern/path_util.c +++ b/source/blender/blenlib/intern/path_util.c @@ -861,7 +861,7 @@ static int test_path(char *targetpath, const char *path_base, const char *path_s { char tmppath[FILE_MAX]; - if(path_sep) BLI_join_dirfile(tmppath, path_base, path_sep); + if(path_sep) BLI_join_dirfile(tmppath, sizeof(tmppath), path_base, path_sep); else BLI_strncpy(tmppath, path_base, sizeof(tmppath)); BLI_make_file_string("/", targetpath, tmppath, folder_name); @@ -905,9 +905,9 @@ static int get_path_local(char *targetpath, const char *folder_name, const char #endif if (subfolder_name) { - BLI_join_dirfile(relfolder, folder_name, subfolder_name); + BLI_join_dirfile(relfolder, sizeof(relfolder), folder_name, subfolder_name); } else { - BLI_strncpy(relfolder, folder_name, FILE_MAX); + BLI_strncpy(relfolder, folder_name, sizeof(relfolder)); } /* use argv[0] (bprogname) to get the path to the executable */ @@ -973,15 +973,18 @@ static int get_path_system(char *targetpath, const char *folder_name, const char BLI_split_dirfile(bprogname, bprogdir, NULL); if (subfolder_name) { - BLI_join_dirfile(relfolder, folder_name, subfolder_name); + BLI_join_dirfile(relfolder, sizeof(relfolder), folder_name, subfolder_name); } else { - BLI_strncpy(relfolder, folder_name, FILE_MAX); + BLI_strncpy(relfolder, folder_name, sizeof(relfolder)); } /* try CWD/release/folder_name */ - if(test_path(targetpath, BLI_getwdN(cwd, sizeof(cwd)), "release", relfolder)) - return 1; - + if(BLI_getwdN(cwd, sizeof(cwd))) { + if(test_path(targetpath, cwd, "release", relfolder)) { + return 1; + } + } + /* try EXECUTABLE_DIR/release/folder_name */ if(test_path(targetpath, bprogdir, "release", relfolder)) return 1; @@ -1420,12 +1423,12 @@ void BLI_split_dirfile(const char *string, char *dir, char *file) } /* simple appending of filename to dir, does not check for valid path! */ -void BLI_join_dirfile(char *string, const char *dir, const char *file) +void BLI_join_dirfile(char *string, const int maxlen, const char *dir, const char *file) { int sl_dir; if(string != dir) /* compare pointers */ - BLI_strncpy(string, dir, FILE_MAX); + BLI_strncpy(string, dir, maxlen); if (!file) return; @@ -1433,7 +1436,7 @@ void BLI_join_dirfile(char *string, const char *dir, const char *file) sl_dir= BLI_add_slash(string); if (sl_dir <FILE_MAX) { - BLI_strncpy(string + sl_dir, file, FILE_MAX-sl_dir); + BLI_strncpy(string + sl_dir, file, maxlen - sl_dir); } } @@ -1508,21 +1511,21 @@ int BKE_rebase_path(char *abs, int abs_size, char *rel, int rel_size, const char /* if image is _in_ current .blend file directory */ if (!strcmp(dir, blend_dir)) { - BLI_join_dirfile(dest_path, dest_dir, base); + BLI_join_dirfile(dest_path, sizeof(dest_path), dest_dir, base); } /* "below" */ else { /* rel = image_path_dir - blend_dir */ BLI_strncpy(rel_dir, dir + len, sizeof(rel_dir)); - BLI_join_dirfile(dest_path, dest_dir, rel_dir); - BLI_join_dirfile(dest_path, dest_path, base); + BLI_join_dirfile(dest_path, sizeof(dest_path), dest_dir, rel_dir); + BLI_join_dirfile(dest_path, sizeof(dest_path), dest_path, base); } } /* image is out of current directory */ else { - BLI_join_dirfile(dest_path, dest_dir, base); + BLI_join_dirfile(dest_path, sizeof(dest_path), dest_dir, base); } if (abs) @@ -1646,17 +1649,15 @@ static int add_win32_extension(char *name) } /* filename must be FILE_MAX length minimum */ -void BLI_where_am_i(char *fullname, const char *name) +void BLI_where_am_i(char *fullname, const int maxlen, const char *name) { char filename[FILE_MAXDIR+FILE_MAXFILE]; char *path = NULL, *temp; - + #ifdef _WIN32 const char *separator = ";"; - char slash = '\\'; #else const char *separator = ":"; - char slash = '/'; #endif @@ -1664,36 +1665,37 @@ void BLI_where_am_i(char *fullname, const char *name) /* linux uses binreloc since argv[0] is not relyable, call br_init( NULL ) first */ path = br_find_exe( NULL ); if (path) { - BLI_strncpy(fullname, path, FILE_MAXDIR+FILE_MAXFILE); + BLI_strncpy(fullname, path, maxlen); free(path); return; } #endif #ifdef _WIN32 - if(GetModuleFileName(0, fullname, FILE_MAXDIR+FILE_MAXFILE)) { - GetShortPathName(fullname, fullname, FILE_MAXDIR+FILE_MAXFILE); + if(GetModuleFileName(0, fullname, maxlen)) { + GetShortPathName(fullname, fullname, maxlen); return; } #endif /* unix and non linux */ - if (name && fullname && strlen(name)) { - strcpy(fullname, name); + if (name && name[0]) { + BLI_strncpy(fullname, name, maxlen); if (name[0] == '.') { - // relative path, prepend cwd - BLI_getwdN(fullname, FILE_MAX); - + char wdir[FILE_MAX]= ""; + BLI_getwdN(wdir, sizeof(wdir)); /* backup cwd to restore after */ + // not needed but avoids annoying /./ in name - if(name && name[0]=='.' && name[1]==slash) - BLI_join_dirfile(fullname, fullname, name+2); + if(name[1]==SEP) + BLI_join_dirfile(fullname, maxlen, wdir, name+2); else - BLI_join_dirfile(fullname, fullname, name); - - add_win32_extension(fullname); - } else if (BLI_last_slash(name)) { + BLI_join_dirfile(fullname, maxlen, wdir, name); + + add_win32_extension(fullname); /* XXX, doesnt respect length */ + } + else if (BLI_last_slash(name)) { // full path - strcpy(fullname, name); + BLI_strncpy(fullname, name, maxlen); add_win32_extension(fullname); } else { // search for binary in $PATH @@ -1708,9 +1710,9 @@ void BLI_where_am_i(char *fullname, const char *name) } else { strncpy(filename, path, sizeof(filename)); } - BLI_join_dirfile(fullname, fullname, name); + BLI_join_dirfile(fullname, maxlen, fullname, name); if (add_win32_extension(filename)) { - strcpy(fullname, filename); + BLI_strncpy(fullname, filename, maxlen); break; } } while (temp); @@ -1728,7 +1730,7 @@ void BLI_where_am_i(char *fullname, const char *name) // spaces and double-quotes. There's another solution to this // with spawnv(P_WAIT, bprogname, argv) instead of system() but // that's even uglier - GetShortPathName(fullname, fullname, FILE_MAXDIR+FILE_MAXFILE); + GetShortPathName(fullname, fullname, maxlen); #if defined(DEBUG) printf("Shortname = '%s'\n", fullname); #endif @@ -1736,12 +1738,12 @@ void BLI_where_am_i(char *fullname, const char *name) } } -void BLI_where_is_temp(char *fullname, int usertemp) +void BLI_where_is_temp(char *fullname, int maxlen, int usertemp) { fullname[0] = '\0'; if (usertemp && BLI_is_dir(U.tempdir)) { - strcpy(fullname, U.tempdir); + BLI_strncpy(fullname, U.tempdir, maxlen); } @@ -1749,7 +1751,7 @@ void BLI_where_is_temp(char *fullname, int usertemp) if (fullname[0] == '\0') { char *tmp = getenv("TEMP"); /* Windows */ if (tmp && BLI_is_dir(tmp)) { - strcpy(fullname, tmp); + BLI_strncpy((fullname, tmp, maxlen); } } #else @@ -1757,25 +1759,27 @@ void BLI_where_is_temp(char *fullname, int usertemp) if (fullname[0] == '\0') { char *tmp = getenv("TMP"); if (tmp && BLI_is_dir(tmp)) { - strcpy(fullname, tmp); + BLI_strncpy(fullname, tmp, maxlen); } } if (fullname[0] == '\0') { char *tmp = getenv("TMPDIR"); if (tmp && BLI_is_dir(tmp)) { - strcpy(fullname, tmp); + BLI_strncpy(fullname, tmp, maxlen); } } #endif if (fullname[0] == '\0') { - strcpy(fullname, "/tmp/"); + BLI_strncpy(fullname, "/tmp/", maxlen); } else { /* add a trailing slash if needed */ BLI_add_slash(fullname); #ifdef WIN32 - strcpy(U.tempdir, fullname); /* also set user pref to show %TEMP%. /tmp/ is just plain confusing for Windows users. */ + if(U.tempdir != fullname) { + BLI_strncpy(U.tempdir, fullname, maxlen); /* also set user pref to show %TEMP%. /tmp/ is just plain confusing for Windows users. */ + } #endif } } diff --git a/source/blender/blenlib/intern/storage.c b/source/blender/blenlib/intern/storage.c index eaa312b82f9..c2605a15ad8 100644 --- a/source/blender/blenlib/intern/storage.c +++ b/source/blender/blenlib/intern/storage.c @@ -103,7 +103,7 @@ static struct ListBase dirbase_={ 0,0}; static struct ListBase *dirbase = &dirbase_; - +/* can return NULL when the size is not big enough */ char *BLI_getwdN(char *dir, const int maxncpy) { const char *pwd= getenv("PWD"); |