Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bcftools gtcheck --no-HWE-prob segfaults trying to access uninitialized args->nmatch array #2180

Closed
freeseek opened this issue May 3, 2024 · 1 comment

Comments

@freeseek
Copy link
Contributor

freeseek commented May 3, 2024

If you run bcftools gtcheck --no-HWE-prob the array args->nmatch is not initialized:

if ( args->calc_hwe_prob )
{
    // prob of the observed sequence of matches given site AFs and HWE
    args->hwe_prob = (double*) calloc(args->npairs,sizeof(*args->hwe_prob));
    if ( !args->hwe_prob ) error("Error: failed to allocate %.1f Mb. Run with --no-HWE-prob to save some memory.\n", args->npairs*sizeof(*args->hwe_prob)/1e6);
    args->nmatch = (uint32_t*) calloc(args->npairs,sizeof(*args->ncnt));         // number of matches, used only with calc_hwe_prob
    if ( !args->nmatch ) error("Error: failed to allocate %.1f Mb.\n", args->npairs*sizeof(*args->ncnt)/1e6);
}

At the end of the run gtcheck writes the report with the following command:

if ( args->ndiff )
{
    ksprintf(&args->kstr,"DCv2\t%s\t%s\t%u\t%e\t%u\t%u\n",
            args->qry_hdr->samples[iqry],
            args->gt_hdr?args->gt_hdr->samples[igt]:args->qry_hdr->samples[igt],
            args->ndiff[idx],
            (args->calc_hwe_prob && args->nmatch[idx]) ? args->hwe_prob[idx]/args->nmatch[idx] : 0,
            args->ncnt[idx],
            args->nmatch[idx]);
}
else
{
    ksprintf(&args->kstr,"DCv2\t%s\t%s\t%e\t%e\t%u\t%u\n",
            args->qry_hdr->samples[iqry],
            args->gt_hdr?args->gt_hdr->samples[igt]:args->qry_hdr->samples[igt],
            args->pdiff[idx],
            (args->calc_hwe_prob && args->nmatch[idx]) ? args->hwe_prob[idx]/args->nmatch[idx] : 0,
            args->ncnt[idx],
            args->nmatch[idx]);
}

But there is no check for args->calc_hwe_prob when accessing args->nmatch[idx]. The two instances of args->nmatch[idx] should be replaced with args->calc_hwe_prob ? args->nmatch[idx] : 0 or something similar

@pd3 pd3 closed this as completed in 5d61fce May 15, 2024
@pd3
Copy link
Member

pd3 commented May 15, 2024

The bug was introduced by 983e610ab. It is now fixed, thank you for reporting it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants