Skip to content

Commit

Permalink
(file_path.c) Cut down on some strlcats
Browse files Browse the repository at this point in the history
  • Loading branch information
LibretroAdmin committed Jul 17, 2023
1 parent aeff636 commit fe1297c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 40 deletions.
64 changes: 32 additions & 32 deletions libretro-common/file/file_path.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,24 +372,20 @@ char *find_last_slash(const char *str)
* Assumes path is a directory. Appends a slash
* if not already there.
**/
void fill_pathname_slash(char *path, size_t size)
size_t fill_pathname_slash(char *path, size_t size)
{
size_t path_len;
const char *last_slash = find_last_slash(path);

if (!last_slash)
{
strlcat(path, PATH_DEFAULT_SLASH(), size);
return;
}

return strlcat(path, PATH_DEFAULT_SLASH(), size);
path_len = strlen(path);
/* Try to preserve slash type. */
if (last_slash != (path + path_len - 1))
{
path[ path_len] = last_slash[0];
path[++path_len] = '\0';
}
return path_len;
}

/**
Expand All @@ -412,12 +408,11 @@ void fill_pathname_slash(char *path, size_t size)
size_t fill_pathname_dir(char *in_dir, const char *in_basename,
const char *replace, size_t size)
{
const char *base = NULL;

fill_pathname_slash(in_dir, size);
base = path_basename(in_basename);
strlcat(in_dir, base, size);
return strlcat(in_dir, replace, size);
size_t _len = fill_pathname_slash(in_dir, size);
const char *base = path_basename(in_basename);
_len += strlcpy(in_dir + _len, base, size - _len);
_len += strlcpy(in_dir + _len, replace, size - _len);
return _len;
}

/**
Expand Down Expand Up @@ -570,8 +565,9 @@ size_t fill_dated_filename(char *out_filename,
size_t fill_str_dated_filename(char *out_filename,
const char *in_str, const char *ext, size_t size)
{
char format[NAME_MAX_LENGTH];
struct tm tm_;
char format[NAME_MAX_LENGTH];
size_t _len = 0;
time_t cur_time = time(NULL);

rtime_localtime(&cur_time, &tm_);
Expand All @@ -583,8 +579,9 @@ size_t fill_str_dated_filename(char *out_filename,
return strlcat(out_filename, format, size);
}
strftime(format, sizeof(format), "-%y%m%d-%H%M%S.", &tm_);
strlcat(out_filename, format, size);
return strlcat(out_filename, ext, size);
_len = strlcat(out_filename, format, size);
_len += strlcpy(out_filename + _len, ext, size - _len);
return _len;
}

/**
Expand Down Expand Up @@ -964,11 +961,13 @@ void fill_pathname_resolve_relative(char *out_path,
size_t fill_pathname_join(char *out_path,
const char *dir, const char *path, size_t size)
{
size_t _len = 0;
if (out_path != dir)
strlcpy(out_path, dir, size);
_len = strlcpy(out_path, dir, size);
if (*out_path)
fill_pathname_slash(out_path, size);
return strlcat(out_path, path, size);
_len = fill_pathname_slash(out_path, size);
_len += strlcpy(out_path + _len, path, size - _len);
return _len;
}

/**
Expand Down Expand Up @@ -1013,20 +1012,21 @@ size_t fill_pathname_join_special(char *out_path,
}
}

return strlcat(out_path, path, size);
len += strlcpy(out_path + len, path, size - len);
return len;
}

size_t fill_pathname_join_special_ext(char *out_path,
const char *dir, const char *path,
const char *last, const char *ext,
size_t size)
{
fill_pathname_join(out_path, dir, path, size);
size_t _len = fill_pathname_join(out_path, dir, path, size);
if (*out_path)
fill_pathname_slash(out_path, size);

strlcat(out_path, last, size);
return strlcat(out_path, ext, size);
_len = fill_pathname_slash(out_path, size);
_len += strlcpy(out_path + _len, last, size - _len);
_len += strlcpy(out_path + _len, ext, size - _len);
return _len;
}

/**
Expand All @@ -1043,19 +1043,19 @@ size_t fill_pathname_join_special_ext(char *out_path,
size_t fill_pathname_join_delim(char *out_path, const char *dir,
const char *path, const char delim, size_t size)
{
size_t copied;
/* behavior of strlcpy is undefined if dst and src overlap */
size_t _len;
/* Behavior of strlcpy is undefined if dst and src overlap */
if (out_path == dir)
copied = strlen(dir);
_len = strlen(dir);
else
copied = strlcpy(out_path, dir, size);
_len = strlcpy(out_path, dir, size);

out_path[copied] = delim;
out_path[copied+1] = '\0';
out_path[_len] = delim;
out_path[_len+1] = '\0';

if (path)
return strlcat(out_path, path, size);
return copied;
return _len;
}

size_t fill_pathname_expand_special(char *out_path,
Expand Down
14 changes: 6 additions & 8 deletions libretro-common/include/file/file_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,10 @@ size_t fill_dated_filename(char *out_filename,
* Hidden non-leaf function cost:
* - Calls time
* - Calls rtime_localtime()
* - Calls strlcpy
* - Calls strlcpy 2x
* - Calls string_is_empty()
* - Calls strftime
* - Calls strlcat at least 2x
* - Calls strlcat
*
* @return Length of the string copied into @out_path
**/
Expand Down Expand Up @@ -369,7 +369,7 @@ char *find_last_slash(const char *str);
* Hidden non-leaf function cost:
* - Calls fill_pathname_slash()
* - Calls path_basename()
* - Calls strlcat 2x
* - Calls strlcpy 2x
**/
size_t fill_pathname_dir(char *in_dir, const char *in_basename,
const char *replace, size_t size);
Expand Down Expand Up @@ -470,9 +470,8 @@ void fill_pathname_resolve_relative(char *out_path, const char *in_refpath,
* between directory and path.
*
* Hidden non-leaf function cost:
* - calls strlcpy
* - calls strlcpy at least once
* - calls fill_pathname_slash()
* - calls strlcat
*
* Deprecated. Use fill_pathname_join_special() instead
* if you can ensure @dir != @out_path
Expand All @@ -499,9 +498,8 @@ size_t fill_pathname_join(char *out_path, const char *dir,
* between directory and path.
*
* Hidden non-leaf function cost:
* - calls strlcpy
* - calls strlcpy 2x
* - calls find_last_slash()
* - calls strlcat
*
* @return Length of the string copied into @out_path
**/
Expand Down Expand Up @@ -627,7 +625,7 @@ void path_basedir_wrapper(char *path);
* - can call strlcat once if it returns false
* - calls strlen
**/
void fill_pathname_slash(char *path, size_t size);
size_t fill_pathname_slash(char *path, size_t size);

#if !defined(RARCH_CONSOLE) && defined(RARCH_INTERNAL)
void fill_pathname_application_path(char *buf, size_t size);
Expand Down

0 comments on commit fe1297c

Please sign in to comment.