From fdeeb2de7699bd2ca34203d5009d760b8e3f891d Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Sat, 19 Sep 2020 12:02:36 -0500 Subject: [PATCH] Cleanup: basic maintenance pass on some level_0 projects. More maintenance commits to come. --- level_0/f_account/c/account.c | 6 +++--- level_0/f_console/c/console.c | 7 +++++++ level_0/f_directory/c/directory.c | 2 -- level_0/f_environment/c/private-environment.c | 2 ++ level_0/f_file/c/file.c | 8 +++++--- level_0/f_file/c/private-file.c | 17 ++++++++++++----- level_0/f_file/c/private-file.h | 20 +++++++++++++------- level_0/f_fss/c/fss.c | 4 ++++ level_0/f_fss/c/fss.h | 2 +- level_0/f_iki/c/iki.c | 14 ++++++++++++++ level_0/f_memory/c/memory.c | 4 ++-- level_0/f_memory/c/memory.h | 2 +- 12 files changed, 64 insertions(+), 24 deletions(-) diff --git a/level_0/f_account/c/account.c b/level_0/f_account/c/account.c index a1091e0..c50493e 100644 --- a/level_0/f_account/c/account.c +++ b/level_0/f_account/c/account.c @@ -56,7 +56,7 @@ extern "C" { char buffer[f_account_pwd_length_fallback_second]; - int result = getgrnam_r(name, &grp, buffer, length, &pointer); + const int result = getgrnam_r(name, &grp, buffer, length, &pointer); if (result) { if (errno == EINTR) return F_status_set_error(F_interrupted); @@ -100,7 +100,7 @@ extern "C" { errno = 0; char buffer[length]; - int result = getpwnam_r(name, &password, buffer, length, &pointer); + const int result = getpwnam_r(name, &password, buffer, length, &pointer); if (result) { if (errno == EINTR) return F_status_set_error(F_interrupted); @@ -131,7 +131,7 @@ extern "C" { char buffer[f_account_pwd_length_fallback_second]; - int result = getpwnam_r(name, &password, buffer, length, &pointer); + const int result = getpwnam_r(name, &password, buffer, length, &pointer); if (result) { if (errno == EINTR) return F_status_set_error(F_interrupted); diff --git a/level_0/f_console/c/console.c b/level_0/f_console/c/console.c index e789cee..963f40a 100644 --- a/level_0/f_console/c/console.c +++ b/level_0/f_console/c/console.c @@ -136,9 +136,12 @@ extern "C" { } if (console_short != f_console_none) { + // The sub_location is used on a per increment basis (such as 'tar -xcf', the '-' would have an increment of 1, therefore x, c, and f would all be three separate parameters). while (sub_location < string_length) { + for (i = 0; i < parameters.used; i++) { + if (parameters.parameter[i].type != console_type) { continue; } @@ -240,6 +243,7 @@ extern "C" { found = F_false; for (i = 0; i < parameters.used; i++) { + if (parameters.parameter[i].type != f_console_type_other) continue; if (parameters.parameter[i].symbol_other == 0) continue; @@ -284,6 +288,7 @@ extern "C" { } // for if (!found) { + // populate list of remaining parameters.parameter not associated with anything. if (remaining->used == remaining->size) { f_macro_memory_structure_macro_increment(status, (*remaining), 1, f_console_default_allocation_step, f_macro_string_lengths_t_resize, F_buffer_too_large); @@ -329,6 +334,7 @@ extern "C" { f_console_parameter_id_t priority = 0; for (f_array_length_t i = 0; i < choices.used; i++) { + if (choices.id[i] > parameters.used) return F_status_set_error(F_parameter); if (parameters.parameter[choices.id[i]].result == f_console_result_found) { @@ -370,6 +376,7 @@ extern "C" { f_console_parameter_id_t priority = 0; for (f_array_length_t i = 0; i < choices.used; i++) { + if (choices.id[i] > parameters.used) return F_status_set_error(F_parameter); if (parameters.parameter[choices.id[i]].result == f_console_result_found) { diff --git a/level_0/f_directory/c/directory.c b/level_0/f_directory/c/directory.c index a141a1e..a8c2dbb 100644 --- a/level_0/f_directory/c/directory.c +++ b/level_0/f_directory/c/directory.c @@ -409,7 +409,6 @@ extern "C" { memset(&file_stat, 0, sizeof(struct stat)); if (fstatat(at_id, path, &file_stat, flag) < 0) { - if (errno == ENOENT) { return private_f_directory_create_at(at_id, path, mode); } @@ -428,7 +427,6 @@ extern "C" { } if (utimensat(at_id, path, 0, flag) < 0) { - if (errno == EACCES) return F_status_set_error(F_access_denied); if (errno == EBADF) return F_status_set_error(F_directory_descriptor); if (errno == EFAULT) return F_status_set_error(F_buffer); diff --git a/level_0/f_environment/c/private-environment.c b/level_0/f_environment/c/private-environment.c index 3004d01..c3792c4 100644 --- a/level_0/f_environment/c/private-environment.c +++ b/level_0/f_environment/c/private-environment.c @@ -40,6 +40,7 @@ extern "C" { #if !defined(_di_f_environment_set_) || !defined(_di_f_environment_set_dynamic_) f_return_status private_f_environment_set(const f_string_t name, const f_string_t value, const bool replace) { + if (setenv(name, value, replace) < 0) { if (errno == EINVAL) { return F_status_set_error(F_invalid); @@ -57,6 +58,7 @@ extern "C" { #if !defined(_di_f_environment_unset_) || !defined(_di_f_environment_unset_dynamic_) f_return_status private_f_environment_unset(const f_string_t name) { + if (unsetenv(name) < 0) { if (errno == EINVAL) { return F_status_set_error(F_invalid); diff --git a/level_0/f_file/c/file.c b/level_0/f_file/c/file.c index 35a7c06..8e6e512 100644 --- a/level_0/f_file/c/file.c +++ b/level_0/f_file/c/file.c @@ -1169,6 +1169,7 @@ extern "C" { syntax = 0; } else if (how == 2) { + // if there are only '0's then the standard and setuid/setgid/sticky bits are to be replaced. if (*mode == 0) { *replace = f_file_mode_t_replace_standard | f_file_mode_t_replace_special; @@ -1199,7 +1200,7 @@ extern "C" { memset(&stat_file, 0, sizeof(struct stat)); - f_status_t status = private_f_file_stat(path, F_true, &stat_file); + const f_status_t status = private_f_file_stat(path, F_true, &stat_file); if (F_status_is_error(status)) { return status; } @@ -1221,7 +1222,7 @@ extern "C" { memset(&stat_file, 0, sizeof(struct stat)); - f_status_t status = private_f_file_stat(path, F_true, &stat_file); + const f_status_t status = private_f_file_stat(path, F_true, &stat_file); if (F_status_is_error(status)) { return status; } @@ -1418,7 +1419,7 @@ extern "C" { memset(&stat_file, 0, sizeof(struct stat)); - f_status_t status = private_f_file_stat(path, F_true, &stat_file); + const f_status_t status = private_f_file_stat(path, F_true, &stat_file); if (F_status_is_error(status)) { return status; } @@ -1446,6 +1447,7 @@ extern "C" { memset(&buffer_read, 0, sizeof(file.size_read)); while ((size_read = read(file.id, buffer_read, file.size_read)) > 0) { + if (buffer->used + size_read > buffer->size) { if (buffer->size + size_read > f_string_length_t_size) { return F_status_set_error(F_string_too_large); diff --git a/level_0/f_file/c/private-file.c b/level_0/f_file/c/private-file.c index 508f513..2216647 100644 --- a/level_0/f_file/c/private-file.c +++ b/level_0/f_file/c/private-file.c @@ -7,6 +7,7 @@ extern "C" { #if !defined(_di_f_file_close_) || !defined(_di_f_file_copy_) f_return_status private_f_file_close(int *id) { + if (*id == -1) return F_none; if (F_status_is_error(private_f_file_flush(*id))) return F_status_set_error(F_file_synchronize); @@ -38,6 +39,7 @@ extern "C" { if (F_status_is_error(status)) return status; status = private_f_file_open(destination, 0, &file_destination); + if (F_status_is_error(status)) { private_f_file_close(&file_source.id); return status; @@ -50,6 +52,7 @@ extern "C" { memset(buffer, 0, size_block); while ((size_read = read(file_source.id, buffer, size_block)) > 0) { + size_write = write(file_destination.id, buffer, size_read); if (size_write < 0 || size_write != size_read) { @@ -81,6 +84,7 @@ extern "C" { if (F_status_is_error(status)) return status; status = private_f_file_open_at(at_id, destination, 0, &file_destination); + if (F_status_is_error(status)) { private_f_file_close(&file_source.id); return status; @@ -93,6 +97,7 @@ extern "C" { memset(buffer, 0, size_block); while ((size_read = read(file_source.id, buffer, size_block)) > 0) { + size_write = write(file_destination.id, buffer, size_read); if (size_write < 0 || size_write != size_read) { @@ -122,9 +127,9 @@ extern "C" { file.flag |= f_file_flag_exclusive; } - f_status_t status = private_f_file_open(path, mode, &file); + const f_status_t status = private_f_file_open(path, mode, &file); - if (file.id > 0) { + if (file.id != -1) { return private_f_file_close(&file.id); } @@ -142,9 +147,9 @@ extern "C" { file.flag |= f_file_flag_exclusive; } - f_status_t status = private_f_file_open_at(at_id, path, mode, &file); + const f_status_t status = private_f_file_open_at(at_id, path, mode, &file); - if (file.id > 0) { + if (file.id != -1) { return private_f_file_close(&file.id); } @@ -495,6 +500,7 @@ extern "C" { #if !defined(_di_f_file_open_) || !defined(_di_f_file_copy_) f_return_status private_f_file_open(const f_string_t path, const mode_t mode, f_file_t *file) { + if (mode == 0) { file->id = open(path, file->flag); } @@ -532,6 +538,7 @@ extern "C" { #if !defined(_di_f_file_open_at_) || !defined(_di_f_file_copy_at_) f_return_status private_f_file_open_at(const int at_id, const f_string_t path, const mode_t mode, f_file_t *file) { + if (mode == 0) { file->id = openat(at_id, path, file->flag); } @@ -696,7 +703,7 @@ extern "C" { #if !defined(_di_f_file_stat_by_id_) || !defined(_di_f_file_size_by_id_) f_return_status private_f_file_stat_by_id(const int id, struct stat *file_stat) { - int result = fstat(id, file_stat); + const int result = fstat(id, file_stat); if (result < 0) { if (errno == ENAMETOOLONG) return F_status_set_error(F_name); diff --git a/level_0/f_file/c/private-file.h b/level_0/f_file/c/private-file.h index 0f649c0..0f33582 100644 --- a/level_0/f_file/c/private-file.h +++ b/level_0/f_file/c/private-file.h @@ -205,7 +205,6 @@ extern "C" { * F_failure (with error bit) for any other error. * * @see f_file_copy() - * @see mkdir() */ #if !defined(_di_f_file_copy_) extern f_return_status private_f_file_create_directory(const f_string_t path, const mode_t mode) f_gcc_attribute_visibility_internal; @@ -242,7 +241,7 @@ extern "C" { * F_space_not (with error bit) if filesystem is out of space (or filesystem quota is reached). * F_failure (with error bit) for any other error. * - * @see mkdirat() + * @see f_file_copy_at() */ #if !defined(_di_f_file_copy_at_) extern f_return_status private_f_file_create_directory_at(const int at_id, const f_string_t path, const mode_t mode) f_gcc_attribute_visibility_internal; @@ -273,7 +272,8 @@ extern "C" { * F_unsupported (with error bit) for unsupported file types. * F_failure (with error bit) for any other error. * - * @see mkfifo() + * @see f_file_create_fifo() + * @see f_file_copy() */ #if !defined(_di_f_file_create_fifo_) || !defined(_di_f_file_copy_) extern f_return_status private_f_file_create_fifo(const f_string_t path, const mode_t mode) f_gcc_attribute_visibility_internal; @@ -307,7 +307,8 @@ extern "C" { * F_unsupported (with error bit) for unsupported file types. * F_failure (with error bit) for any other error. * - * @see mkfifoat() + * @see f_file_create_fifo_at() + * @see f_file_copy_at() */ #if !defined(_di_f_file_create_fifo_at_) || !defined(_di_f_file_copy_at_) extern f_return_status private_f_file_create_fifo_at(const int at_id, const f_string_t path, const mode_t mode) f_gcc_attribute_visibility_internal; @@ -344,6 +345,7 @@ extern "C" { * F_unsupported (with error bit) for unsupported file types. * F_failure (with error bit) for any other error. * + * @see f_file_copy() * @see f_file_create_device() * @see f_file_create_node() */ @@ -385,6 +387,7 @@ extern "C" { * F_failure (with error bit) for any other error. * F_unsupported (with error bit) for unsupported file types. * + * @see f_file_copy_at() * @see f_file_create_device_at() * @see f_file_create_node_at() */ @@ -519,6 +522,7 @@ extern "C" { * F_string_too_large (with error bit) if link target path is too large for string buffer size. * F_failure (with error bit) for any other error. * + * @see f_file_copy() * @see f_file_link_read() */ #if !defined(_di_f_file_link_read_) || !defined(_di_f_file_copy_) @@ -555,6 +559,7 @@ extern "C" { * F_string_too_large (with error bit) if link target path is too large for string buffer size. * F_failure (with error bit) for any other error. * + * @see f_file_copy_at() * @see f_file_link_read_at() */ #if !defined(_di_f_file_link_read_at_) || !defined(_di_f_file_copy_at_) @@ -585,8 +590,8 @@ extern "C" { * F_read_only (with error bit) if file is read-only. * F_failure (with error bit) for any other error. * - * @see f_file_mode_set() * @see f_file_copy() + * @see f_file_mode_set() */ #if !defined(_di_f_file_mode_set_) || !defined(_di_f_file_copy_) extern f_return_status private_f_file_mode_set(const f_string_t path, const mode_t mode) f_gcc_attribute_visibility_internal; @@ -712,8 +717,8 @@ extern "C" { * F_read_only (with error bit) if file is read-only. * F_failure (with error bit) for any other error. * - * @see f_file_role_change() * @see f_file_copy() + * @see f_file_role_change() */ #if !defined(_di_f_file_role_change_) || !defined(_di_f_file_copy_) extern f_return_status private_f_file_role_change(const f_string_t path, const uid_t uid, const gid_t gid, const bool dereference) f_gcc_attribute_visibility_internal; @@ -783,8 +788,9 @@ extern "C" { * F_parameter (with error bit) if a parameter is invalid. * * @see f_file_copy() - * @see f_file_stat() * @see f_file_exists() + * @see f_file_is() + * @see f_file_stat() * @see f_file_touch() */ #if !defined(_di_f_file_stat_) || !defined(_di_f_file_copy_) || !defined(_di_f_file_exists_) || !defined(_di_f_file_is_) || !defined(_di_f_file_touch_) diff --git a/level_0/f_fss/c/fss.c b/level_0/f_fss/c/fss.c index 58f1dde..8d9cb07 100644 --- a/level_0/f_fss/c/fss.c +++ b/level_0/f_fss/c/fss.c @@ -147,6 +147,7 @@ extern "C" { position = range.start; while (position < buffer->used && position <= range.stop) { + if (buffer->string[position] == f_fss_delimit_placeholder) { distance++; } @@ -159,6 +160,7 @@ extern "C" { utf_width = f_macro_utf_byte_width_is(buffer->string[position]); if (utf_width > 1) { + // not enough space in buffer or in range range to process UTF-8 character. if (position + utf_width >= buffer->used || position + utf_width > range.stop) { return F_status_set_error(F_utf); @@ -173,6 +175,7 @@ extern "C" { } } else { + // shift everything down one for each placeholder found if (distance > 0) { buffer->string[position] = buffer->string[position + distance]; @@ -286,6 +289,7 @@ extern "C" { } for (;;) { + if (buffer.string[range->start] != f_fss_delimit_placeholder) { status = f_utf_is_graph(buffer.string + range->start, width_max); diff --git a/level_0/f_fss/c/fss.h b/level_0/f_fss/c/fss.h index bb08930..430301f 100644 --- a/level_0/f_fss/c/fss.h +++ b/level_0/f_fss/c/fss.h @@ -10,7 +10,7 @@ * The purpose of compression is not to compression the entire file's contents but only and individual objects content, so the file is still partially readable. * NOTE: all start/stop locations must be defined as a (start < stop) and not (start <= stop), therefore if (start == stop) then stop. * - * @todo identify all special UTF-8 characters that would violate the concepts, such as "Ogham space mark ( )" is not valid as whitespace in FSS because it is a visible non-whitespace character. + * @todo identify all special UTF-8 characters that would violate the FSS design concepts, such as "Ogham space mark ( )" is not valid as whitespace in FSS because it is a visible non-whitespace character. */ #ifndef _F_fss_h #define _F_fss_h diff --git a/level_0/f_iki/c/iki.c b/level_0/f_iki/c/iki.c index e227a18..f196c6d 100644 --- a/level_0/f_iki/c/iki.c +++ b/level_0/f_iki/c/iki.c @@ -105,8 +105,10 @@ extern "C" { f_string_lengths_t delimits = f_string_lengths_t_initialize; do { + // find the start and end of the vocabulary name. while (range->start <= range->stop && range->start < buffer->used) { + if (buffer->string[range->start] == f_iki_syntax_placeholder) { range->start++; continue; @@ -115,6 +117,7 @@ extern "C" { if (buffer->string[range->start] == f_iki_syntax_separator) { if (range->start == found_vocabulary.start) { status = f_utf_buffer_increment(*buffer, range, 1); + if (F_status_is_error(status)) { f_macro_string_lengths_t_delete(status, delimits); return status; @@ -186,6 +189,7 @@ extern "C" { } status = f_utf_buffer_increment(*buffer, range, 1); + if (F_status_is_error(status)) { f_macro_string_lengths_t_delete(status, delimits); return status; @@ -198,6 +202,7 @@ extern "C" { f_macro_iki_determine_width_max(buffer, range, width_max); status = f_utf_is_word_dash_plus(buffer->string + range->start, width_max, F_false); + if (F_status_is_error(status)) { f_macro_string_lengths_t_delete(status, delimits); return status; @@ -225,6 +230,7 @@ extern "C" { } status = f_utf_buffer_increment(*buffer, range, 1); + if (F_status_is_error(status)) { f_macro_string_lengths_t_delete(status, delimits); return status; @@ -236,6 +242,7 @@ extern "C" { found_content = range->start; while (range->start <= range->stop && range->start < buffer->used) { + if (buffer->string[range->start] == f_iki_syntax_placeholder) { range->start++; continue; @@ -246,6 +253,7 @@ extern "C" { // this is a valid vocabulary name and content, but if it is delimited, save the delimit and ignore. if (vocabulary_delimited) { f_macro_iki_allocate_delimits_if_necessary(status, delimits); + if (F_status_is_error(status)) { f_macro_string_lengths_t_delete(status, delimits); return status; @@ -314,6 +322,7 @@ extern "C" { f_array_length_t content_slash_total = 0; while (range->start <= range->stop && range->start < buffer->used) { + if (buffer->string[range->start] == f_iki_syntax_placeholder) { range->start++; continue; @@ -346,6 +355,7 @@ extern "C" { content_range.stop = range->stop; while (i < content_slash_delimits) { + if (buffer->string[content_range.start] == f_iki_syntax_slash) { delimits.array[delimits.used] = content_range.start; delimits.used++; @@ -354,6 +364,7 @@ extern "C" { } status = f_utf_buffer_increment(*buffer, (&content_range), 1); + if (F_status_is_error(status)) { f_macro_string_lengths_t_delete(status, delimits); return status; @@ -366,6 +377,7 @@ extern "C" { // this is a valid vocabulary name and content, but if it is delimited, save the delimit and ignore. if (vocabulary_delimited) { f_macro_iki_allocate_delimits_if_necessary(status, delimits); + if (F_status_is_error(status)) { f_macro_string_lengths_t_delete(status, delimits); return status; @@ -442,6 +454,7 @@ extern "C" { content_slash_total++; status = f_utf_buffer_increment(*buffer, range, 1); + if (F_status_is_error(status)) { f_macro_string_lengths_t_delete(status, delimits); return status; @@ -450,6 +463,7 @@ extern "C" { } status = f_utf_buffer_increment(*buffer, range, 1); + if (F_status_is_error(status)) { f_macro_string_lengths_t_delete(status, delimits); return status; diff --git a/level_0/f_memory/c/memory.c b/level_0/f_memory/c/memory.c index 2a5e1dc..b0e4ea8 100644 --- a/level_0/f_memory/c/memory.c +++ b/level_0/f_memory/c/memory.c @@ -11,11 +11,11 @@ extern "C" { if (pointer == 0) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - // prevent double-allocations + // prevent double-allocations. if (*pointer != 0) return F_none; // Some people use malloc(type * length) to produce the same results. - // I have noticed this sometimes causes an increase in L1/L2 cache misses (0.02% L1 increase, 0.01% L2 increase). + // This has been observed to sometimes causes an increase in L1/L2 cache misses (0.02% L1 increase, 0.01% L2 increase). *pointer = calloc(type, length); if (*pointer) { diff --git a/level_0/f_memory/c/memory.h b/level_0/f_memory/c/memory.h index dbfec8f..b3dc323 100644 --- a/level_0/f_memory/c/memory.h +++ b/level_0/f_memory/c/memory.h @@ -7,7 +7,7 @@ * * Provide means to use memory routines, with error checking. * - * @todo consider adding f_memory__scramble() and f_memory__juggle(). + * @todo consider adding f_memory_scramble() and f_memory_juggle(). * f_memory_scramble() is like f_destroy() but it writes random data instead of 0. * f_memory_juggle() is like f_adjust() but it writes random data instead of 0. */ -- 1.8.3.1