Skip to content

Fix some valgrind alerts #388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions src/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -2001,13 +2001,14 @@ send_pages(ConnectionArgs* conn_arg, const char *to_fullpath, const char *from_f
{
FILE *in = NULL;
FILE *out = NULL;
int hdr_num = -1;
off_t cur_pos_out = 0;
char curr_page[BLCKSZ];
int n_blocks_read = 0;
BlockNumber blknum = 0;
datapagemap_iterator_t *iter = NULL;
int compressed_size = 0;
BackupPageHeader2 *header = NULL;
parray *harray = NULL;

/* stdio buffers */
char *in_buf = NULL;
Expand Down Expand Up @@ -2046,6 +2047,8 @@ send_pages(ConnectionArgs* conn_arg, const char *to_fullpath, const char *from_f
setvbuf(in, in_buf, _IOFBF, STDIO_BUFSIZE);
}

harray = parray_new();

while (blknum < file->n_blocks)
{
PageState page_st;
Expand All @@ -2063,17 +2066,15 @@ send_pages(ConnectionArgs* conn_arg, const char *to_fullpath, const char *from_f
if (!out)
out = open_local_file_rw(to_fullpath, &out_buf, STDIO_BUFSIZE);

hdr_num++;

if (!*headers)
*headers = (BackupPageHeader2 *) pgut_malloc(sizeof(BackupPageHeader2));
else
*headers = (BackupPageHeader2 *) pgut_realloc(*headers, (hdr_num+1) * sizeof(BackupPageHeader2));
header = pgut_new0(BackupPageHeader2);
*header = (BackupPageHeader2){
.block = blknum,
.pos = cur_pos_out,
.lsn = page_st.lsn,
.checksum = page_st.checksum,
};

(*headers)[hdr_num].block = blknum;
(*headers)[hdr_num].pos = cur_pos_out;
(*headers)[hdr_num].lsn = page_st.lsn;
(*headers)[hdr_num].checksum = page_st.checksum;
parray_append(harray, header);

compressed_size = compress_and_backup_page(file, blknum, in, out, &(file->crc),
rc, curr_page, calg, clevel,
Expand All @@ -2098,12 +2099,22 @@ send_pages(ConnectionArgs* conn_arg, const char *to_fullpath, const char *from_f
* Add dummy header, so we can later extract the length of last header
* as difference between their offsets.
*/
if (*headers)
if (parray_num(harray) > 0)
{
file->n_headers = hdr_num +1;
*headers = (BackupPageHeader2 *) pgut_realloc(*headers, (hdr_num+2) * sizeof(BackupPageHeader2));
(*headers)[hdr_num+1].pos = cur_pos_out;
size_t hdr_num = parray_num(harray);
size_t i;

file->n_headers = (int) hdr_num; /* is it valid? */
*headers = (BackupPageHeader2 *) pgut_malloc0((hdr_num + 1) * sizeof(BackupPageHeader2));
for (i = 0; i < hdr_num; i++)
{
header = (BackupPageHeader2 *)parray_get(harray, i);
(*headers)[i] = *header;
pg_free(header);
}
(*headers)[hdr_num] = (BackupPageHeader2){.pos=cur_pos_out};
}
parray_free(harray);

/* cleanup */
if (in && fclose(in))
Expand Down
2 changes: 1 addition & 1 deletion src/restore.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,8 @@ do_restore_or_validate(time_t target_backup_id, pgRecoveryTarget *rt,
elog(INFO, "shift LSN: %X/%X",
(uint32) (shift_lsn >> 32), (uint32) shift_lsn);

params->shift_lsn = shift_lsn;
}
params->shift_lsn = shift_lsn;

/* for validation or restore with enabled validation */
if (!params->is_restore || !params->no_validate)
Expand Down
2 changes: 1 addition & 1 deletion src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ writeControlFile(ControlFileData *ControlFile, const char *path, fio_location lo
#endif

/* copy controlFileSize */
buffer = pg_malloc(ControlFileSize);
buffer = pg_malloc0(ControlFileSize);
memcpy(buffer, ControlFile, sizeof(ControlFileData));

/* Write pg_control */
Expand Down
11 changes: 11 additions & 0 deletions src/utils/pgut.c
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,17 @@ pgut_malloc(size_t size)
return ret;
}

void *
pgut_malloc0(size_t size)
{
char *ret;

ret = pgut_malloc(size);
memset(ret, 0, size);

return ret;
}

void *
pgut_realloc(void *p, size_t size)
{
Expand Down
2 changes: 2 additions & 0 deletions src/utils/pgut.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ extern int pgut_wait(int num, PGconn *connections[], struct timeval *timeout);
* memory allocators
*/
extern void *pgut_malloc(size_t size);
extern void *pgut_malloc0(size_t size);
extern void *pgut_realloc(void *p, size_t size);
extern char *pgut_strdup(const char *str);

#define pgut_new(type) ((type *) pgut_malloc(sizeof(type)))
#define pgut_new0(type) ((type *) pgut_malloc0(sizeof(type)))
#define pgut_newarray(type, n) ((type *) pgut_malloc(sizeof(type) * (n)))

/*
Expand Down