Refactor searchUUIDByName: update parameter type for UUIDs found, improve memory allocation handling, and enhance error checking
This commit is contained in:
+2
-1
@@ -28,4 +28,5 @@
|
|||||||
44,Alena,Matouskova,3037,1,0,1
|
44,Alena,Matouskova,3037,1,0,1
|
||||||
45,Josef,Suchy,3038,1,0,1
|
45,Josef,Suchy,3038,1,0,1
|
||||||
46,Marketa,Rezacova,3039,1,0,1
|
46,Marketa,Rezacova,3039,1,0,1
|
||||||
47,Libor,Hanak,3040,1,0,1
|
47,Libor,Hanak,3040,1,0,1
|
||||||
|
48,Libor,Hanak,3040,1,0,1
|
||||||
|
+1
-1
@@ -59,7 +59,7 @@ uint16_t generateUUID(list_t *_list);
|
|||||||
* @param _surname Pointer to a string containing the user's surname.
|
* @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.
|
* @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.
|
* @brief Searches for a person in a linked list by their UUID.
|
||||||
|
|||||||
@@ -404,7 +404,7 @@ void personSearch(list_t *person_list, int terminal_x, int terminal_y)
|
|||||||
}
|
}
|
||||||
else if (highlight == 2)
|
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;
|
list_t found_pesons;
|
||||||
found_pesons.head = NULL;
|
found_pesons.head = NULL;
|
||||||
node_t **last_ptr = &found_pesons.head;
|
node_t **last_ptr = &found_pesons.head;
|
||||||
|
|||||||
+9
-8
@@ -56,14 +56,14 @@ node_t *searchPersonByUUID(
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t searchUUIDByName(
|
uint16_t searchUUIDByName(list_t *_list, char *_name, char *_surname, uint16_t **_uuids_found)
|
||||||
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));
|
uint16_t *uuids = malloc(sizeof(uint16_t));
|
||||||
|
if (uuids == NULL) {
|
||||||
|
*_uuids_found = NULL;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
pthread_mutex_lock(&_list->lock);
|
pthread_mutex_lock(&_list->lock);
|
||||||
node_t *current = _list->head;
|
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)
|
if (strcmp(_name, current->user.name) == 0 && strcmp(_surname, current->user.surname) == 0)
|
||||||
{
|
{
|
||||||
sum++;
|
sum++;
|
||||||
uint16_t *uuids = realloc(uuids, sum * sizeof(uint16_t));
|
uint16_t *temp = realloc(uuids, sum * sizeof(uint16_t));
|
||||||
if (uuids == NULL)
|
if (temp == NULL)
|
||||||
{
|
{
|
||||||
pthread_mutex_unlock(&_list->lock);
|
pthread_mutex_unlock(&_list->lock);
|
||||||
free(uuids);
|
free(uuids);
|
||||||
puts("Fatal error: free memory is not sufficient for this operation");
|
puts("Fatal error: free memory is not sufficient for this operation");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
uuids = temp;
|
||||||
uuids[sum - 1] = current->user.uuid;
|
uuids[sum - 1] = current->user.uuid;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user