Skip to content

v2.0: Total component update

Choose a tag to compare

@cMeWHou cMeWHou released this 30 May 22:03
· 35 commits to master since this release

Bitset

  • Fixed infinite loop in get_last_free_bit / get_last_busy_bit caused by decrementing an unsigned size_t past zero (wraparound to SIZE_MAX).
  • Changed index parameters and return types from size_t to int64_t — making index < 0 guards meaningful and allowing -1 as a valid "not found" sentinel.
  • Fixed NULL dereference after malloc failure in init_bitset (missing check before memset).
  • Changed bit-shift literals to 1u and added explicit uint8_t casts to avoid signed-integer UB.
  • Replaced exit() calls with return of typed error codes; changed void functions to int.

Dictionary

  • Fixed wrong logical operator in bounds checks: index < 0 && index >= size (always false) → ||.
  • Fixed dangling tail pointer: remove_record_from_dictionary and remove_record_from_dictionary_by_index did not update dict->tail when the last element was removed.
  • Fixed silent failure and memory leak in emplace_record_to_dictionary on an empty dictionary: add_by_index(dict, 0, ...) returned the node at index 0, which is NULL on an empty list, causing the newly allocated record to be silently dropped.
  • Fixed pointer-to-int narrowing in contains_key_in_dictionary / contains_value_in_dictionary (64-bit pointer truncated to int); added != NULL.
  • Eliminated O(n2) complexity in map, filter, reduce, and sort by replacing per-iteration get_index_from_dictionary_by_ref_record calls with a simple index++ counter.
  • Fixed reduce callback typedef: return type changed from p_record to void — the return value was always discarded.
  • Added defensive NULL check in add_record_by_index_with_metadata after get_record_by_index call.
  • Replaced all exit() calls with return of typed error codes; void functions converted to int.

Container

  • Fixed pointer truncation in release_service: const char args = get_value_from_dictionary(...) truncated an 8-byte pointer to 1 byte.
  • Added return NULL in the default branch of get_service_from_container_with_args (missing return caused UB).
  • Fixed size check blocking replacement: the MAX_CONTAINER_SIZE guard now uses contains_key_in_dictionary(elements_types, key) to detect replacements and skips the size limit for them.
  • Fixed function pointer casts from non-portable (void *)fn to typed callback casts (dictionary_iteration_callback_with_args, dictionary_iteration_values_callback).
  • Added mutex protection for singleton initialization in get_service_from_container_with_args to eliminate the race condition where two threads could both see instance == NULL and initialize it twice.
  • Fixed get_container to return NULL instead of exit() when containers are uninitialized.
  • Replaced all exit() calls with return of typed error codes; all void functions converted to int.

Event

  • Fixed function pointer type mismatch: observable_callback (1 arg, void *) was silently cast to observable_callback_with_args (2 args, void *); subscribe and global_subscribe now use an explicit cast, documenting the intent.
  • Fixed subscriber key collision: the subscription ID was based on event->size, which decrements on unsubscribe. Replaced with a monotonically increasing counter stored in event->metadata — keys are now unique for the lifetime of the event.
  • Replaced all exit() calls with return of typed error codes; void functions converted to int.

ThreadPool

  • Eliminated spin loop: replaced the busy-wait while(thread->is_running) { if (!task) continue; } loop in task_invoker with pthread_cond_wait, eliminating 100% CPU usage per thread.
  • Fixed deadlock in run_task_with_args: the function held the global mutex while waiting for a free thread, while worker threads needed the same mutex to mark themselves as free. Replaced the spin loop with pthread_cond_timedwait(&pool_cond, &mutex, &deadline) — mutex is released during the wait, allowing workers to signal availability via pool_cond.
  • Added pthread_cond_t cond per thread for task assignment, and pthread_cond_t pool_cond for notifying run_task_with_args when a thread becomes free.
  • Fixed missing pthread_join in destroy_thread: pthread_cancel is asynchronous and left threads accessing freed memory. Replaced with is_running = 0 + pthread_cond_signal + pthread_join for a clean exit.
  • Fixed memory leak in make_task: task was not freed when the metadata allocation failed.
  • Fixed memory leak in run_task_with_args: available_threads (result of filter_dictionary) was never freed on either the timeout or the success path.
  • Fixed NULL dereference before NULL check in filter_threads: record->value was read before checking !record.
  • Replaced all exit() calls with return of typed error codes; void functions converted to int.