Debug size_t

调试BUG时发现,以下问题:

1568712490080

由代码中发现

BYTES_INVALID 定义如下:

1568712510794

奇怪问题:在于file->write_size == -1; 但是if判断中却没有走到contine代码分支, 而是继续走到 validate.c 351行输出;

关于数据结构定义如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
typedef struct pgFile
{
time_t mtime; /* time of last modification */
mode_t mode; /* protection (file type and permission) */
size_t size; /* size of the file */
size_t read_size; /* size of the portion read (if only some pages are
backed up partially, it's different from size) */
size_t write_size; /* size of the backed-up file. BYTES_INVALID means
that the file existed but was not backed up
because not modified since last backup. */
pg_crc32c crc; /* CRC value of the file, regular file only */
char *linked; /* path of the linked file */
bool is_datafile; /* true if the file is PostgreSQL data file */
char path[1]; /* path of the file */
} pgFile;

查看sizeof大小:

1
sizeof(size_t)

window: 8字节;

linux: 8字节;

cppreference.com size_t页面

size_tsizeof_Alignof (C11 起) 和 offsetof 的结果的无符号整数类型,定义取决于数据模型

size_t 定义如下:

1
typedef unsigned int size_t;  //8Byte

Linux size_t 应该用 %u 标识, 即 -1 应该为 4294967295;

按照内存格式输出:

1
printf("%u   %d", -1, -1);

输出为

1
4294967295  -1

即修改代码:

1
2
3
4
5
#ifdef WIN32
#define BYTES_INVALID (4294967295)
#else
#define BYTES_INVALID (-1)
#endi
欣赏此文? 求鼓励,求支持!