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.

(cherry picked from commit beef392f5a)
release-11.0-gledis
Gledis Zeneli 2022-05-28 00:22:00 +03:00 committed by gledis69
parent a64e135a36
commit c440cbb643
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

@ -67,6 +67,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
@ -4883,6 +4884,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

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