Skip to content

[BUG] Buffer overflow in sf_putints, sf_putfloats (And suggestions)#315

Description

@cplusv2023

馃悰 Bug report

Programs (e.g. sflpef, sfhpef) that uses sf_putints and sf_putfloats may occur buffer overflow.

In (current version) api/c/file.c, Lines 998, 1000, 1037, 1039:

(L998 as example:)

// v defined as char val[1024], *v=val
// Some iterations of i
/* L998 */ v += snprintf(v,1024,"%g,",par[i]);

the pointer v moves forwards when iteration goes on, while the length of snprintf always being 1024, this could lead to buffer overlow.

To Reproduce

Steps to reproduce the behavior:

  1. Use some programs that employs sf_putints (or sf_putfloats)
  2. It happens.

Screenshots
Before debugging:
Image

Debugging:

Image

Desktop (please complete the following information):

  • OS: Ubuntu 24.04.02 LTS
  • GCC Version: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0

Suggestions

Use a smaller length in snprintf, like:

// Some iterations of i
v+=snprintf(v,1023-i*11,"%g,",par[i]);
//...
snprintf(v,1023-n*11,"%d",par[n-1]);
// I use 11 because maxlen 2147483647 = 10 digits plus 1 char ','
// Actually maybe some const value like 20 is ok.
// Or if the list is overlength, use a dynamic char array.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions