Refactor searchUUIDByName: update parameter type for UUIDs found, improve memory allocation handling, and enhance error checking

This commit is contained in:
2025-05-17 18:30:55 +02:00
parent 05e8fefafc
commit c84987239e
4 changed files with 13 additions and 11 deletions
+1
View File
@@ -29,3 +29,4 @@
45,Josef,Suchy,3038,1,0,1
46,Marketa,Rezacova,3039,1,0,1
47,Libor,Hanak,3040,1,0,1
48,Libor,Hanak,3040,1,0,1
1 17 Klara Novotna 3010 1 0 1
29 45 Josef Suchy 3038 1 0 1
30 46 Marketa Rezacova 3039 1 0 1
31 47 Libor Hanak 3040 1 0 1
32 48 Libor Hanak 3040 1 0 1
+1 -1
View File
@@ -59,7 +59,7 @@ uint16_t generateUUID(list_t *_list);
* @param _surname Pointer to a string containing the user's surname.
* @return Pointer to the UUID of the user if found, or 0 if no match is found.
*/
uint16_t searchUUIDByName(list_t *_list, char *_name, char *_surname, uint16_t * _uuids_found);
uint16_t searchUUIDByName(list_t *_list, char *_name, char *_surname, uint16_t **_uuids_found);
/**
* @brief Searches for a person in a linked list by their UUID.
+1 -1
View File
@@ -404,7 +404,7 @@ void personSearch(list_t *person_list, int terminal_x, int terminal_y)
}
else if (highlight == 2)
{
uint16_t count = searchUUIDByName(person_list, name, surname, uuids_found);
uint16_t count = searchUUIDByName(person_list, name, surname, &uuids_found);
list_t found_pesons;
found_pesons.head = NULL;
node_t **last_ptr = &found_pesons.head;
+9 -8
View File
@@ -56,14 +56,14 @@ node_t *searchPersonByUUID(
return NULL;
}
uint16_t searchUUIDByName(
list_t *_list,
char *_name,
char *_surname,
uint16_t **_uuids_found)
uint16_t searchUUIDByName(list_t *_list, char *_name, char *_surname, uint16_t **_uuids_found)
{
int sum = 0;
uint16_t sum = 0;
uint16_t *uuids = malloc(sizeof(uint16_t));
if (uuids == NULL) {
*_uuids_found = NULL;
return 0;
}
pthread_mutex_lock(&_list->lock);
node_t *current = _list->head;
@@ -75,14 +75,15 @@ uint16_t searchUUIDByName(
if (strcmp(_name, current->user.name) == 0 && strcmp(_surname, current->user.surname) == 0)
{
sum++;
uint16_t *uuids = realloc(uuids, sum * sizeof(uint16_t));
if (uuids == NULL)
uint16_t *temp = realloc(uuids, sum * sizeof(uint16_t));
if (temp == NULL)
{
pthread_mutex_unlock(&_list->lock);
free(uuids);
puts("Fatal error: free memory is not sufficient for this operation");
exit(EXIT_FAILURE);
}
uuids = temp;
uuids[sum - 1] = current->user.uuid;
}
}