Skip to content

Commit 8a3acdf

Browse files
committed
rework filesize division check.
1 parent 09d07a5 commit 8a3acdf

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

src/data.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,7 @@ backup_data_file(pgFile *file, const char *from_fullpath, const char *to_fullpat
507507
* NOTE This is a normal situation, if the file size has changed
508508
* since the moment we computed it.
509509
*/
510-
file->n_blocks = (typeof(file->n_blocks))(file->size/BLCKSZ);
511-
Assert((int64_t)file->n_blocks * BLCKSZ == file->size);
510+
file->n_blocks = ft_div_i64u32_to_i32(file->size, BLCKSZ);
512511

513512
/*
514513
* Skip unchanged file only if it exists in previous backup.
@@ -618,8 +617,7 @@ backup_data_file(pgFile *file, const char *from_fullpath, const char *to_fullpat
618617
if (backup_mode == BACKUP_MODE_FULL ||
619618
backup_mode == BACKUP_MODE_DIFF_DELTA)
620619
{
621-
file->n_blocks = (typeof(file->n_blocks))(file->read_size / BLCKSZ);
622-
Assert((int64_t)file->n_blocks * BLCKSZ == file->read_size);
620+
file->n_blocks = ft_div_i64u32_to_i32(file->read_size, BLCKSZ);
623621
}
624622

625623
/* Determine that file didn`t changed in case of incremental backup */
@@ -666,7 +664,7 @@ catchup_data_file(pgFile *file, const char *from_fullpath, const char *to_fullpa
666664
* NOTE This is a normal situation, if the file size has changed
667665
* since the moment we computed it.
668666
*/
669-
file->n_blocks = file->size/BLCKSZ;
667+
file->n_blocks = ft_div_i64u32_to_i32(file->size, BLCKSZ);
670668

671669
/*
672670
* Skip unchanged file only if it exists in destination directory.
@@ -1599,8 +1597,7 @@ check_data_file(ConnectionArgs *arguments, pgFile *file,
15991597
* NOTE This is a normal situation, if the file size has changed
16001598
* since the moment we computed it.
16011599
*/
1602-
nblocks = (typeof(nblocks))(file->size/BLCKSZ);
1603-
Assert((int64_t)nblocks * BLCKSZ == file->size);
1600+
nblocks = ft_div_i64u32_to_i32(file->size, BLCKSZ);
16041601

16051602
for (blknum = 0; blknum < nblocks; blknum++)
16061603
{

src/fu_util/ft_util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ extern void ft_set_allocators(void *(*_realloc)(void *, size_t),
174174
ft_inline size_t ft_add_size(size_t a, size_t b);
175175
ft_inline size_t ft_mul_size(size_t a, size_t b);
176176

177+
/* division 64->32 bit */
178+
ft_inline int32_t ft_div_i64u32_to_i32(int64_t a, uint32_t b);
179+
177180
#define ft_new(type) ft_calloc(sizeof(type))
178181
#define ft_newar(type, cnt) ft_calloc(ft_mul_size(sizeof(type), (cnt)))
179182

src/fu_util/impl/ft_impl.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ ft_inline size_t ft_mul_size(size_t a, size_t b) {
175175
return r;
176176
}
177177

178+
/* division 64->32 bit */
179+
ft_inline int32_t ft_div_i64u32_to_i32(int64_t a, uint32_t b) {
180+
int64_t r;
181+
ft_assert(a >= 0);
182+
r = a / b;
183+
ft_assert(r <= INT32_MAX);
184+
return (int32_t)r;
185+
}
186+
178187
extern ft_gcc_malloc(ft_realloc, 1) void* ft_realloc(void* ptr, size_t new_sz);
179188
extern ft_gcc_malloc(ft_realloc, 1) void* ft_calloc(size_t sz);
180189

src/utils/file.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,8 +1913,7 @@ fio_send_pages(const char *to_fullpath, const char *from_fullpath, pgFile *file,
19131913
req.arg.bitmapsize = 0;
19141914
}
19151915

1916-
req.arg.nblocks = file->size/BLCKSZ;
1917-
Assert((int64_t)req.arg.nblocks * BLCKSZ == file->size);
1916+
req.arg.nblocks = ft_div_i64u32_to_i32(file->size, BLCKSZ);
19181917
req.arg.segmentno = file->segno * RELSEG_SIZE;
19191918
req.arg.horizonLsn = horizonLsn;
19201919
req.arg.checksumVersion = checksum_version;

0 commit comments

Comments
 (0)