Bug: NULL pointer dereference in RecoverShardStatistic() - malloc return value unchecked

Open
#279 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
1/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
c
Domain
databases

Research direction

The issue identifies src/backend/pgxc/shard/shardmap.c and RecoverShardStatistic() as the entry point. Read the allocation and subsequent read(fd, rec, size) call, then check the project's existing handling for allocation failures. Done means the NULL allocation is handled safely so recovery cannot pass it to read(); the issue links fix PR #276.

Written by the indexing model from the issue text.

Description

Bug Description

In src/backend/pgxc/shard/shardmap.c, the RecoverShardStatistic() function calls malloc() to allocate a ShardRecord array of MAX_SHARDS * sizeof(ShardRecord) bytes, but never checks the return value. If malloc() returns NULL (out of memory), the subsequent read(fd, rec, size) call will write to a NULL pointer, causing a segmentation fault (SIGSEGV).

Reproduction

// Line 3355-3358
int size = sizeof(ShardRecord) * nelems;  // nelems = MAX_SHARDS (large allocation)
ShardRecord *rec = (ShardRecord *)malloc(size);
// No NULL check!
// ...
ret = read(fd, rec, size);  // NULL deref if malloc fails -> SIGSEGV

Impact

  • Severity: High — causes process crash (segfault)
  • Component: PGXC shard management
  • Trigger: Memory pressure / OOM condition during shard statistic recovery
  • Effect: Backend process crash

Fix PR

#276

Dominant language
C
Stars
946
Forks
693
Avg merge
5m
Merged PRs (30d)
1

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from OpenTenBase/OpenTenBase

All issues in OpenTenBase/OpenTenBase

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.