Floating point exception when calling `sf_current_byterate` on an opened file

Open Beginner friendly
#1,122 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
70/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
c
Domain
backend

Research direction

Start with sds.c:761 and the sds_byterate call path; the report identifies division by psf->sf.frames as the failure and supplies a minimal reproducer using sf_current_byterate. Check how sf_open can leave the frame count invalid for the provided input file, then verify the reproducer no longer raises a floating point exception and valid files still report a byte rate.

Written by the indexing model from the issue text.

Description

Hello, a floating point exception occurs in the below program when provided with a specially-constructed file. This exception occurs in the sds_byterate function at location sds.c:761. The offending line of code is below:
return (psf->datalength * psf->sf.samplerate) / psf->sf.frames ;

Although the sf_open function successfully returns a file, it seems that the sf.frames property is invalid. Moving forward, potentially adding a check to the frame count for the file before performing the division would avoid this, or perhaps this is indicative of corruption that occurred in the earlier sf_open function. Thank you!

Minimal Reproducer
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "sndfile.h"
int main(int argc, char *argv[])
{
    SF_INFO sfinfo;
    SNDFILE *sndfile = sf_open(argv[1], SFM_READ, &sfinfo);
    if(!sndfile){
        return 0;
    }
    sf_current_byterate(sndfile);
}
Input File

https://github.com/FuturesLab/Library_Bugs/blob/main/libsndfile/1

Commit

68f6c16fe1407eff4cdde158566694c3ed666c2f

ASAN Trace
==316915==ERROR: AddressSanitizer: FPE on unknown address 0x5634745d5dbb (pc 0x5634745d5dbb bp 0x7ffcdb644420 sp 0x7ffcdb644370 T0)
    #0 0x5634745d5dbb in sds_byterate /local_src/libsndfile/build_artifacts/code/src/sds.c:761:49
    #1 0x5634745a7a2a in main /bugs/libsndfile/./harness/1/harness.cpp:12:5
    #2 0x7f3582c29d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
    #3 0x7f3582c29e3f in __libc_start_main csu/../csu/libc-start.c:392:3
    #4 0x5634744bf474 in _start (/bugs/libsndfile/harness/1/harness+0x51474) (BuildId: 02cabdd8ac7f45f74e40a4f6fe7529f4eeba9554)

==316915==Register values:
rax = 0x000000000000bb80  rbx = 0x00007ffcdb644380  rcx = 0x0000000000003e80  rdx = 0x0000000000000000  
rdi = 0x00007d8581fe1ea8  rsi = 0x00007d8581fe1ea8  rbp = 0x00007ffcdb644420  rsp = 0x00007ffcdb644370  
 r8 = 0x00000f66b017e004   r9 = 0x0000000000000000  r10 = 0x00000f66b017e007  r11 = 0x0000000000000000  
r12 = 0x00007ffcdb644538  r13 = 0x00005634745a7900  r14 = 0x00005634746ccc30  r15 = 0x00007f3583136040  
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: FPE /local_src/libsndfile/build_artifacts/code/src/sds.c:761:49 in sds_byterate
==316915==ABORTING
Dominant language
C
Stars
1.7k
Forks
476
PR merge metrics
No merged PRs in 30d

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 libsndfile/libsndfile

All issues in libsndfile/libsndfile

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.