From b109b63288ed8ceaf78a4fd56bf023c3a46364cb Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Thu, 10 Dec 2020 22:49:12 -0600 Subject: [PATCH] Bugfix: fl_string_dynamic_rip() and fl_string_dynamic_rip_nulless() are wrong. The dynamic rip functions are not actually performing a rip. Instead, they are performing just a simple append. Fix this behavior to have these functions actually perform a rip. Add additional parameter checks. Cleanup private_fl_string_append_nulless(), improving the structure of that function. --- level_1/fl_string/c/private-string.c | 54 ++++++++++++++++-------------------- level_1/fl_string/c/string.c | 30 +++++++++++++++++--- level_1/fl_string/c/string.h | 4 +++ 3 files changed, 54 insertions(+), 34 deletions(-) diff --git a/level_1/fl_string/c/private-string.c b/level_1/fl_string/c/private-string.c index deb0313..3db9a72 100644 --- a/level_1/fl_string/c/private-string.c +++ b/level_1/fl_string/c/private-string.c @@ -28,50 +28,44 @@ extern "C" { f_status_t status = F_none; + f_string_length_t i = 0; f_string_length_t first = 0; f_string_length_t size = 0; - for (f_string_length_t i = 0; i <= length; i++) { + for (; i < length; i++) { - if (i == length) { - if (i > first) { - size = i - first; + if (source[i]) continue; - if (destination->used + size > destination->size) { - status = private_fl_string_dynamic_increase_by(size, destination); - if (F_status_is_error(status)) return status; - } + if (i && i > first) { + size = i - first; - memcpy(destination->string + destination->used, source + first, size); - destination->used = destination->used + size; + if (destination->used + size > destination->size) { + status = private_fl_string_dynamic_increase_by(size, destination); + if (F_status_is_error(status)) return status; } - break; + memcpy(destination->string + destination->used, source + first, size); + destination->used = destination->used + size; } - if (!source[i]) { - if (i > 0) { - if (i > first) { - size = i - first; + while (i + 1 < length && !source[i + 1]) { + i++; + } // while - if (destination->used + size > destination->size) { - status = private_fl_string_dynamic_increase_by(size, destination); - if (F_status_is_error(status)) return status; - } + first = i + 1; + } // for - memcpy(destination->string + destination->used, source + first, size); - destination->used = destination->used + size; - } - } + if (i > first) { + size = i - first; - while (i + 1 < length && !source[i + 1]) { - i++; - } // while - - first = i + 1; - continue; + if (destination->used + size > destination->size) { + status = private_fl_string_dynamic_increase_by(size, destination); + if (F_status_is_error(status)) return status; } - } // for + + memcpy(destination->string + destination->used, source + first, size); + destination->used = destination->used + size; + } return F_none; } diff --git a/level_1/fl_string/c/string.c b/level_1/fl_string/c/string.c index 818e925..e712495f 100644 --- a/level_1/fl_string/c/string.c +++ b/level_1/fl_string/c/string.c @@ -867,13 +867,24 @@ extern "C" { #ifndef _di_level_1_parameter_checking_ if (source.used <= range.start) return F_status_set_error(F_parameter); if (source.used <= range.stop) return F_status_set_error(F_parameter); + if (range.stop < range.start) return F_status_set_error(F_parameter); if (!destination) return F_status_set_error(F_parameter); #endif // _di_level_1_parameter_checking_ if (!source.used) return F_data_not_eos; + + f_string_length_t begin = range.start; + f_string_length_t end = range.stop; + + const f_status_t status = private_fl_string_rip_find_range(source.string, &begin, &end); + + if (F_status_is_error(status)) return status; + if (status == F_data_not) return status; + + if (!source.used) return F_data_not_eos; if (range.start > range.stop) return F_data_not_stop; - return private_fl_string_append(source.string + range.start, (range.stop - range.start) + 1, destination); + return private_fl_string_append(source.string + begin, (end - begin) + 1, destination); } #endif // _di_fl_string_dynamic_rip_ @@ -882,13 +893,24 @@ extern "C" { #ifndef _di_level_1_parameter_checking_ if (source.used <= range.start) return F_status_set_error(F_parameter); if (source.used <= range.stop) return F_status_set_error(F_parameter); + if (range.stop < range.start) return F_status_set_error(F_parameter); if (!destination) return F_status_set_error(F_parameter); #endif // _di_level_1_parameter_checking_ if (!source.used) return F_data_not_eos; + + f_string_length_t begin = range.start; + f_string_length_t end = range.stop; + + const f_status_t status = private_fl_string_rip_find_range(source.string, &begin, &end); + + if (F_status_is_error(status)) return status; + if (status == F_data_not) return status; + + if (!source.used) return F_data_not_eos; if (range.start > range.stop) return F_data_not_stop; - return private_fl_string_append_nulless(source.string + range.start, (range.stop - range.start) + 1, destination); + return private_fl_string_append_nulless(source.string + begin, (end - begin) + 1, destination); } #endif // _di_fl_string_dynamic_rip_nulless_ @@ -1730,7 +1752,7 @@ extern "C" { f_string_length_t begin = 0; f_string_length_t end = length - 1; - f_status_t status = private_fl_string_rip_find_range(source, &begin, &end); + const f_status_t status = private_fl_string_rip_find_range(source, &begin, &end); if (F_status_is_error(status)) return status; if (status == F_data_not) return status; @@ -1750,7 +1772,7 @@ extern "C" { f_string_length_t begin = 0; f_string_length_t end = length - 1; - f_status_t status = private_fl_string_rip_find_range(source, &begin, &end); + const f_status_t status = private_fl_string_rip_find_range(source, &begin, &end); if (F_status_is_error(status)) return status; if (status == F_data_not) return status; diff --git a/level_1/fl_string/c/string.h b/level_1/fl_string/c/string.h index 3948f13..d7c9b85 100644 --- a/level_1/fl_string/c/string.h +++ b/level_1/fl_string/c/string.h @@ -1459,6 +1459,7 @@ extern "C" { * Allocate a new string from the provided range in the buffer. * * Ignores leading and trailing whitespace. + * Ignores leading and trailing NULL characters. * As a result, resulting size may be smaller than requested range. * * @param source @@ -1485,6 +1486,7 @@ extern "C" { * Allocate a new string from the provided range in the buffer. * * Ignores leading and trailing whitespace. + * Ignores leading and trailing NULL characters. * As a result, resulting size may be smaller than requested range. * * Skips over NULL characters from source when appending. @@ -2301,6 +2303,7 @@ extern "C" { * Allocate a new string from the provided range in the string. * * Ignores leading and trailing whitespace. + * Ignores leading and trailing NULL characters. * As a result, resulting size may be smaller than requested length. * * @param source @@ -2329,6 +2332,7 @@ extern "C" { * Allocate a new string from the provided range in the string. * * Ignores leading and trailing whitespace. + * Ignores leading and trailing NULL characters. * As a result, resulting size may be smaller than requested length. * * Skips over NULL characters from source when ripping. -- 1.8.3.1