Skip to content

Commit

Permalink
Handle GUC memory right in older pgsql versions
Browse files Browse the repository at this point in the history
  • Loading branch information
pramsey committed Dec 6, 2024
1 parent 98b608f commit 1ef3a6e
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions http.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,25 +314,35 @@ http_strtolower(const char *input)


#if PG_VERSION_NUM < 160000
static void *
guc_malloc(int elevel, size_t size)
{
void *data;

/* Avoid unportable behavior of malloc(0) */
if (size == 0)
size = 1;
data = malloc(size);
if (data == NULL)
ereport(elevel,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
return data;
}

static char *
guc_strdup(int elevel, const char *src)
{
char *data;
MemoryContext oldcontext = MemoryContextSwitchTo(CacheMemoryContext);
data = pstrdup(src);
MemoryContextSwitchTo(oldcontext);

if (data == NULL)
ereport(elevel,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
return data;
size_t len = strlen(src) + 1;
char *dup = guc_malloc(elevel, len);
memcpy(dup, src, len);
return dup;
}

static void
guc_free(void *ptr)
{
pfree(ptr);
free(ptr);
}
#endif

Expand Down

0 comments on commit 1ef3a6e

Please # to comment.