Fix memory error with citus_add_node reported by valgrind test (#5967)

The error comes due to the datum jsonb in pg_dist_metadata_node.metadata being 0 in some scenarios. This is likely due to not copying the data when receiving a datum from a tuple and pg deciding to deallocate that memory when the table that the tuple was from is closed.
Also fix another place in the code that might have been susceptible to this issue.
I tested on both multi-vg and multi-1-vg and the test were successful.
pull/5974/head
Gledis Zeneli 2022-05-28 00:22:00 +03:00 committed by GitHub
parent 26d927178c
commit beef392f5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 3 deletions

View File

@ -337,6 +337,7 @@ ExtractEncryptedPassword(Oid roleOid)
Datum passwordDatum = heap_getattr(tuple, Anum_pg_authid_rolpassword,
pgAuthIdDescription, &isNull);
char *passwordCstring = TextDatumGetCString(passwordDatum);
table_close(pgAuthId, AccessShareLock);
ReleaseSysCache(tuple);
@ -346,7 +347,7 @@ ExtractEncryptedPassword(Oid roleOid)
return NULL;
}
return pstrdup(TextDatumGetCString(passwordDatum));
return pstrdup(passwordCstring);
}

View File

@ -70,6 +70,7 @@
#include "utils/datum.h"
#include "utils/elog.h"
#include "utils/hsearch.h"
#include "utils/jsonb.h"
#if PG_VERSION_NUM >= PG_VERSION_13
#include "common/hashfn.h"
#endif
@ -4910,6 +4911,12 @@ DistNodeMetadata(void)
"could not find any entries in pg_dist_metadata")));
}
/*
* Copy the jsonb result before closing the table
* since that memory can be freed.
*/
metadata = JsonbPGetDatum(DatumGetJsonbPCopy(metadata));
systable_endscan(scanDescriptor);
table_close(pgDistNodeMetadata, AccessShareLock);

View File

@ -386,7 +386,7 @@ static void
ExplainPropertyBytes(const char *qlabel, int64 bytes, ExplainState *es)
{
Datum textDatum = DirectFunctionCall1(pg_size_pretty, Int64GetDatum(bytes));
ExplainPropertyText(qlabel, text_to_cstring(DatumGetTextP(textDatum)), es);
ExplainPropertyText(qlabel, TextDatumGetCString(textDatum), es);
}

View File

@ -577,7 +577,7 @@ JsonFieldValueString(Datum jsonDocument, const char *key)
return NULL;
}
char *valueString = text_to_cstring(DatumGetTextP(valueTextDatum));
char *valueString = TextDatumGetCString(valueTextDatum);
return valueString;
}