> Well, the documentation already says to avoid it:
>
> http://www.postgresql.org/docs/current/static/xfunc-c.html
>
> Another important point is to avoid leaving any uninitialized
> bits within data type values; for example, take care to zero out
> any alignment padding bytes that might be present in structs.
>
> so I don't think what you're suggesting would be controversial
> at all; it looks like what you've done is found a(t least one)
> bug where the documented practice wasn't followed, and it's good
> to find any such places.
Well in this case here is a patch that fixes "use of uninitialized
value" reports by MemorySanitizer I managed to catch so far.
--
Best regards,
Aleksander Alekseev
http://eax.me/
diff --git a/src/backend/access/gist/gistxlog.c b/src/backend/access/gist/gistxlog.c
index b48e97c..3e81865 100644
--- a/src/backend/access/gist/gistxlog.c
+++ b/src/backend/access/gist/gistxlog.c
@@ -328,7 +328,7 @@ gistXLogSplit(RelFileNode node, BlockNumber blkno, bool page_is_leaf,
BlockNumber origrlink, GistNSN orignsn,
Buffer leftchildbuf, bool markfollowright)
{
- gistxlogPageSplit xlrec;
+ gistxlogPageSplit xlrec = {0};
SplitedPageLayout *ptr;
int npage = 0;
XLogRecPtr recptr;
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index f090ca5..7bab290 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -202,7 +202,7 @@ static void
addLeafTuple(Relation index, SpGistState *state, SpGistLeafTuple leafTuple,
SPPageDesc *current, SPPageDesc *parent, bool isNulls, bool isNew)
{
- spgxlogAddLeaf xlrec;
+ spgxlogAddLeaf xlrec = {0};
xlrec.newPage = isNew;
xlrec.storesNulls = isNulls;
@@ -400,7 +400,7 @@ moveLeafs(Relation index, SpGistState *state,
OffsetNumber *toDelete;
OffsetNumber *toInsert;
BlockNumber nblkno;
- spgxlogMoveLeafs xlrec;
+ spgxlogMoveLeafs xlrec = {0};
char *leafdata,
*leafptr;
@@ -701,7 +701,7 @@ doPickSplit(Relation index, SpGistState *state,
int currentFreeSpace;
int totalLeafSizes;
bool allTheSame;
- spgxlogPickSplit xlrec;
+ spgxlogPickSplit xlrec = {0};
char *leafdata,
*leafptr;
SPPageDesc saveCurrent;
@@ -1492,7 +1492,7 @@ spgAddNodeAction(Relation index, SpGistState *state,
int nodeN, Datum nodeLabel)
{
SpGistInnerTuple newInnerTuple;
- spgxlogAddNode xlrec;
+ spgxlogAddNode xlrec = {0};
/* Should not be applied to nulls */
Assert(!SpGistPageStoresNulls(current->page));
@@ -1699,7 +1699,7 @@ spgSplitNodeAction(Relation index, SpGistState *state,
BlockNumber postfixBlkno;
OffsetNumber postfixOffset;
int i;
- spgxlogSplitTuple xlrec;
+ spgxlogSplitTuple xlrec = {0};
Buffer newBuffer = InvalidBuffer;
/* Should not be applied to nulls */
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index b119a47..50d3123 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -4755,7 +4755,7 @@ XLOGShmemInit(void)
void
BootStrapXLOG(void)
{
- CheckPoint checkPoint;
+ CheckPoint checkPoint = {0};
char *buffer;
XLogPageHeader page;
XLogLongPageHeader longpage;
@@ -4802,11 +4802,9 @@ BootStrapXLOG(void)
checkPoint.ThisTimeLineID = ThisTimeLineID;
checkPoint.PrevTimeLineID = ThisTimeLineID;
checkPoint.fullPageWrites = fullPageWrites;
- checkPoint.nextXidEpoch = 0;
checkPoint.nextXid = FirstNormalTransactionId;
checkPoint.nextOid = FirstBootstrapObjectId;
checkPoint.nextMulti = FirstMultiXactId;
- checkPoint.nextMultiOffset = 0;
checkPoint.oldestXid = FirstNormalTransactionId;
checkPoint.oldestXidDB = TemplateDbOid;
checkPoint.oldestMulti = FirstMultiXactId;
diff --git a/src/backend/commands/tablespace.c b/src/backend/commands/tablespace.c
index 1ff5728..e27a18f 100644
--- a/src/backend/commands/tablespace.c
+++ b/src/backend/commands/tablespace.c
@@ -667,7 +667,7 @@ destroy_tablespace_directories(Oid tablespaceoid, bool redo)
DIR *dirdesc;
struct dirent *de;
char *subfile;
- struct stat st;
+ struct stat st = {0};
linkloc_with_version_dir = psprintf("pg_tblspc/%u/%s", tablespaceoid,
TABLESPACE_VERSION_DIRECTORY);
diff --git a/src/backend/libpq/hba.c b/src/backend/libpq/hba.c
index 28f9fb5..123f068 100644
--- a/src/backend/libpq/hba.c
+++ b/src/backend/libpq/hba.c
@@ -823,7 +823,7 @@ parse_hba_line(List *line, int line_num, char *raw_line)
{
char *str;
struct addrinfo *gai_result;
- struct addrinfo hints;
+ struct addrinfo hints = {0};
int ret;
char *cidr_slash;
char *unsupauth;
@@ -1010,12 +1010,6 @@ parse_hba_line(List *line, int line_num, char *raw_line)
/* Get the IP address either way */
hints.ai_flags = AI_NUMERICHOST;
hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = 0;
- hints.ai_protocol = 0;
- hints.ai_addrlen = 0;
- hints.ai_canonname = NULL;
- hints.ai_addr = NULL;
- hints.ai_next = NULL;
ret = pg_getaddrinfo_all(str, NULL, &hints, &gai_result);
if (ret == 0 && gai_result)
diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index 1467355..fc492a7 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -318,7 +318,7 @@ pgstat_init(void)
ACCEPT_TYPE_ARG3 alen;
struct addrinfo *addrs = NULL,
*addr,
- hints;
+ hints = {0};
int ret;
fd_set rset;
struct timeval tv;
@@ -344,11 +344,6 @@ pgstat_init(void)
hints.ai_flags = AI_PASSIVE;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
- hints.ai_protocol = 0;
- hints.ai_addrlen = 0;
- hints.ai_addr = NULL;
- hints.ai_canonname = NULL;
- hints.ai_next = NULL;
ret = pg_getaddrinfo_all("localhost", NULL, &hints, &addrs);
if (ret || !addrs)
{
diff --git a/src/backend/utils/cache/inval.c b/src/backend/utils/cache/inval.c
index 924bebb..7a6bb93 100644
--- a/src/backend/utils/cache/inval.c
+++ b/src/backend/utils/cache/inval.c
@@ -327,7 +327,7 @@ static void
AddCatcacheInvalidationMessage(InvalidationListHeader *hdr,
int id, uint32 hashValue, Oid dbId)
{
- SharedInvalidationMessage msg;
+ SharedInvalidationMessage msg = {0};
Assert(id < CHAR_MAX);
msg.cc.id = (int8) id;
diff --git a/src/backend/utils/mmgr/aset.c b/src/backend/utils/mmgr/aset.c
index d26991e..46ab8a2 100644
--- a/src/backend/utils/mmgr/aset.c
+++ b/src/backend/utils/mmgr/aset.c
@@ -850,7 +850,7 @@ AllocSetAlloc(MemoryContext context, Size size)
blksize <<= 1;
/* Try to allocate it */
- block = (AllocBlock) malloc(blksize);
+ block = (AllocBlock) calloc(1, blksize);
/*
* We could be asking for pretty big blocks here, so cope if malloc
@@ -861,7 +861,7 @@ AllocSetAlloc(MemoryContext context, Size size)
blksize >>= 1;
if (blksize < required_size)
break;
- block = (AllocBlock) malloc(blksize);
+ block = (AllocBlock) calloc(1, blksize);
}
if (block == NULL)
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index ed3ba7b..3465786 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -1340,7 +1340,7 @@ setup_config(void)
*/
{
struct addrinfo *gai_result;
- struct addrinfo hints;
+ struct addrinfo hints = {0};
int err = 0;
#ifdef WIN32
@@ -1353,12 +1353,6 @@ setup_config(void)
/* for best results, this code should match parse_hba() */
hints.ai_flags = AI_NUMERICHOST;
hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = 0;
- hints.ai_protocol = 0;
- hints.ai_addrlen = 0;
- hints.ai_canonname = NULL;
- hints.ai_addr = NULL;
- hints.ai_next = NULL;
if (err != 0 ||
getaddrinfo("::1", NULL, &hints, &gai_result) != 0)
diff --git a/src/test/regress/regress.c b/src/test/regress/regress.c
index e7826a4..aad00b8 100644
--- a/src/test/regress/regress.c
+++ b/src/test/regress/regress.c
@@ -1018,7 +1018,7 @@ test_atomic_uint32(void)
static void
test_atomic_uint64(void)
{
- pg_atomic_uint64 var;
+ pg_atomic_uint64 var = {0};
uint64 expected;
int i;
--
Sent via pgsql-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers