Skip to content

Commit

Permalink
[agroal#190][agroal#312] malloc NULL error handling & calloc(3)
Browse files Browse the repository at this point in the history
  • Loading branch information
SudeepRed authored and jesperpedersen committed Mar 22, 2023
1 parent 70573f3 commit dc04446
Show file tree
Hide file tree
Showing 13 changed files with 307 additions and 160 deletions.
8 changes: 6 additions & 2 deletions src/admin.c
Original file line number Diff line number Diff line change
Expand Up @@ -916,8 +916,12 @@ generate_password(int pwd_length)

s = pwd_length + 1;

pwd = malloc(s);
memset(pwd, 0, s);
pwd = calloc(1, s);
if (pwd == NULL)
{
pgagroal_log_fatal("Couldn't allocate memory while generating password");
return NULL;
}

srand((unsigned)time(&t));

Expand Down
7 changes: 5 additions & 2 deletions src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -845,8 +845,11 @@ config_get(SSL* ssl, int socket, char* config_key, bool verbose)
}
else
{
buffer = malloc(MISC_LENGTH);
memset(buffer, 0, MISC_LENGTH);
buffer = calloc(1, MISC_LENGTH);
if (buffer == NULL)
{
goto error;
}
if (pgagroal_management_read_config_get(socket, &buffer))
{
free(buffer);
Expand Down
29 changes: 20 additions & 9 deletions src/libpgagroal/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -2050,8 +2050,11 @@ extract_key_value(char* str, char** key, char** value)

if (c < length)
{
k = malloc(c + 1);
memset(k, 0, c + 1);
k = calloc(1, c + 1);
if (k == NULL)
{
goto error;
}
memcpy(k, str, c);
*key = k;

Expand Down Expand Up @@ -2105,8 +2108,11 @@ extract_key_value(char* str, char** key, char** value)

if (c <= length)
{
v = malloc((c - offset) + 1);
memset(v, 0, (c - offset) + 1);
v = calloc(1, (c - offset) + 1);
if (v == NULL)
{
goto error;
}
memcpy(v, str + offset, (c - offset));
*value = v;
return 0;
Expand Down Expand Up @@ -2202,8 +2208,11 @@ as_logging_level(char* str)
if (strlen(str) > strlen("debug"))
{
size = strlen(str) - strlen("debug");
debug_value = (char*)malloc(size + 1);
memset(debug_value, 0, size + 1);
debug_value = (char*)calloc(1, size + 1);
if (debug_value == NULL)
{
return PGAGROAL_LOGGING_LEVEL_FATAL;
}
memcpy(debug_value, str + 5, size);
if (as_int(debug_value, &debug_level))
{
Expand Down Expand Up @@ -2501,8 +2510,11 @@ extract_value(char* str, int offset, char** value)
{
to = offset;

v = malloc(to - from + 1);
memset(v, 0, to - from + 1);
v = calloc(1, to - from + 1);
if (v == NULL)
{
return -1;
}
memcpy(v, str + from, to - from);
*value = v;

Expand Down Expand Up @@ -2629,7 +2641,6 @@ transfer_configuration(struct configuration* config, struct configuration* reloa
// changes the pgagroal-cli probably will not be able to connect in any case!
restart_string("unix_socket_dir", config->unix_socket_dir, reload->unix_socket_dir, false);


/* su_connection */

/* states */
Expand Down
8 changes: 6 additions & 2 deletions src/libpgagroal/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,9 @@ pgagroal_log_line(int level, char* file, int line, char* fmt, ...)
atomic_store(&config->log_lock, STATE_FREE);
}
else
SLEEP_AND_GOTO(1000000L,retry)
{
SLEEP_AND_GOTO(1000000L, retry)
}
}
}

Expand Down Expand Up @@ -488,6 +490,8 @@ pgagroal_log_mem(void* data, size_t size)
atomic_store(&config->log_lock, STATE_FREE);
}
else
SLEEP_AND_GOTO(1000000L,retry)
{
SLEEP_AND_GOTO(1000000L, retry)
}
}
}
36 changes: 26 additions & 10 deletions src/libpgagroal/management.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,11 @@ pgagroal_management_read_payload(int socket, signed char id, int* payload_i, cha
iov[0].iov_base = &buf2[0];
iov[0].iov_len = sizeof(buf2);

cmptr = malloc(CMSG_SPACE(sizeof(int)));
memset(cmptr, 0, CMSG_SPACE(sizeof(int)));
cmptr = calloc(1, CMSG_SPACE(sizeof(int)));
if (cmptr == NULL)
{
goto error;
}
cmptr->cmsg_len = CMSG_LEN(sizeof(int));
cmptr->cmsg_level = SOL_SOCKET;
cmptr->cmsg_type = SCM_RIGHTS;
Expand Down Expand Up @@ -148,8 +151,11 @@ pgagroal_management_read_payload(int socket, signed char id, int* payload_i, cha
}
size = pgagroal_read_int32(&buf4);

s = malloc(size + 1);
memset(s, 0, size + 1);
s = calloc(1, size + 1);
if (s == NULL)
{
goto error;
}
if (read_complete(NULL, socket, s, size))
{
goto error;
Expand All @@ -174,8 +180,11 @@ pgagroal_management_read_payload(int socket, signed char id, int* payload_i, cha
}
*payload_i = pgagroal_read_int32(&buf4);

s = malloc(*payload_i + 1);
memset(s, 0, *payload_i + 1);
s = calloc(1, *payload_i + 1);
if (s == NULL)
{
goto error;
}
if (read_complete(NULL, socket, s, *payload_i))
{
goto error;
Expand All @@ -184,8 +193,11 @@ pgagroal_management_read_payload(int socket, signed char id, int* payload_i, cha
break;
case MANAGEMENT_RESET_SERVER:
case MANAGEMENT_SWITCH_TO:
s = malloc(MISC_LENGTH);
memset(s, 0, MISC_LENGTH);
s = calloc(1, MISC_LENGTH);
if (s == NULL)
{
goto error;
}
if (read_complete(NULL, socket, s, MISC_LENGTH))
{
goto error;
Expand Down Expand Up @@ -250,8 +262,12 @@ pgagroal_management_transfer_connection(int32_t slot)
iov[0].iov_base = &buf2[0];
iov[0].iov_len = sizeof(buf2);

cmptr = malloc(CMSG_SPACE(sizeof(int)));
memset(cmptr, 0, CMSG_SPACE(sizeof(int)));
cmptr = calloc(1, CMSG_SPACE(sizeof(int)));
if (cmptr == NULL)
{
goto error;
}

cmptr->cmsg_level = SOL_SOCKET;
cmptr->cmsg_type = SCM_RIGHTS;
cmptr->cmsg_len = CMSG_LEN(sizeof(int));
Expand Down
7 changes: 2 additions & 5 deletions src/libpgagroal/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,18 @@ pgagroal_memory_size(size_t size)
{
pgagroal_memory_destroy();

message = (struct message*)malloc(sizeof(struct message));
message = (struct message*)calloc(1, sizeof(struct message));
if (message == NULL)
{
goto error;
}

data = malloc(size);
data = calloc(1, size);
if (data == NULL)
{
goto error;
}

memset(message, 0, sizeof(struct message));
memset(data, 0, size);

message->kind = 0;
message->length = 0;
message->max_length = size;
Expand Down
Loading

0 comments on commit dc04446

Please sign in to comment.