Cleanup: replace *_max_length with _maxncpy for null inclusive size
The term "length" is too easily confused with the `strlen(..)` length without the null byte. Most string functions that take a static buffer size use the suffix `maxncpy` to avoid off by one errors.
This commit is contained in:
@@ -385,7 +385,7 @@ blender::Vector<blender::bke::path_templates::Error> BKE_path_validate_template(
|
||||
* - Format specifications that don't apply to the type of variable they're
|
||||
* paired with.
|
||||
*
|
||||
* \param path_max_length: The maximum length that template expansion is allowed
|
||||
* \param path_maxncpy: The maximum length that template expansion is allowed
|
||||
* to make the template-expanded path (in bytes), including the null terminator.
|
||||
* In general, this should be the size of the underlying allocation of `path`.
|
||||
*
|
||||
@@ -394,7 +394,7 @@ blender::Vector<blender::bke::path_templates::Error> BKE_path_validate_template(
|
||||
*/
|
||||
blender::Vector<blender::bke::path_templates::Error> BKE_path_apply_template(
|
||||
char *path,
|
||||
int path_max_length,
|
||||
int path_maxncpy,
|
||||
const blender::bke::path_templates::VariableMap &template_variables);
|
||||
/**
|
||||
* Produces a human-readable error message for the given template error.
|
||||
|
||||
@@ -819,7 +819,7 @@ bool BKE_path_contains_template_syntax(blender::StringRef path)
|
||||
* case writing is skipped, and this function just acts to validate the
|
||||
* templating in the path.
|
||||
*
|
||||
* \param out_path_max_length: The maximum length that template expansion is
|
||||
* \param out_path_maxncpy: The maximum length that template expansion is
|
||||
* allowed to make the template-expanded path (in bytes), including the null
|
||||
* terminator. In general, this should be the size of the underlying allocation
|
||||
* of `out_path`.
|
||||
@@ -832,12 +832,12 @@ bool BKE_path_contains_template_syntax(blender::StringRef path)
|
||||
* it should be treated as bogus data in that case.
|
||||
*/
|
||||
static blender::Vector<Error> eval_template(char *out_path,
|
||||
const int out_path_max_length,
|
||||
const int out_path_maxncpy,
|
||||
blender::StringRef in_path,
|
||||
const VariableMap &template_variables)
|
||||
{
|
||||
if (out_path) {
|
||||
in_path.copy_utf8_truncated(out_path, out_path_max_length);
|
||||
in_path.copy_utf8_truncated(out_path, out_path_maxncpy);
|
||||
}
|
||||
|
||||
const blender::Vector<Token> tokens = parse_template(in_path);
|
||||
@@ -933,12 +933,12 @@ static blender::Vector<Error> eval_template(char *out_path,
|
||||
/* Perform the actual substitution with the expanded value. */
|
||||
if (out_path) {
|
||||
/* We're off the end of the available space. */
|
||||
if (token.byte_range.start() + length_diff >= out_path_max_length) {
|
||||
if (token.byte_range.start() + length_diff >= out_path_maxncpy) {
|
||||
break;
|
||||
}
|
||||
|
||||
BLI_string_replace_range(out_path,
|
||||
out_path_max_length,
|
||||
out_path_maxncpy,
|
||||
token.byte_range.start() + length_diff,
|
||||
token.byte_range.one_after_last() + length_diff,
|
||||
replacement_string);
|
||||
@@ -958,19 +958,19 @@ blender::Vector<Error> BKE_path_validate_template(
|
||||
}
|
||||
|
||||
blender::Vector<Error> BKE_path_apply_template(char *path,
|
||||
int path_max_length,
|
||||
int path_maxncpy,
|
||||
const VariableMap &template_variables)
|
||||
{
|
||||
BLI_assert(path != nullptr);
|
||||
|
||||
blender::Vector<char> path_buffer(path_max_length);
|
||||
blender::Vector<char> path_buffer(path_maxncpy);
|
||||
|
||||
const blender::Vector<Error> errors = eval_template(
|
||||
path_buffer.data(), path_buffer.size(), path, template_variables);
|
||||
|
||||
if (errors.is_empty()) {
|
||||
/* No errors, so copy the modified path back to the original. */
|
||||
BLI_strncpy(path, path_buffer.data(), path_max_length);
|
||||
BLI_strncpy(path, path_buffer.data(), path_maxncpy);
|
||||
}
|
||||
return errors;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user