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

Fixed compile error on latest GCC (14.2.1) #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

NickyBoy89
Copy link
Contributor

This PR contains a fix for a compile error that I noticed while compiling Cobra on my Linux machine:

cc -I. -Wall -O2 -pedantic -Werror -Wshadow -std=c99 -DYY_NO_INPUT -Wformat-truncation=1   -c -o cobra_prim.o cobra_prim.c
cobra_prim.c: In function ‘dogrep’:
cobra_prim.c:158:75: error: ‘%s’ directive output may be truncated writing up to 2030 bytes into a region of size between 0 and 2030 [-Werror=format-truncation=]
  158 |                         snprintf(cmd, sizeof(cmd), "grep -q -n -e  \"%s\" %s",
      |                                                                           ^~
cobra_prim.c:158:25: note: ‘snprintf’ output between 19 and 4079 bytes into a destination of size 2048
  158 |                         snprintf(cmd, sizeof(cmd), "grep -q -n -e  \"%s\" %s",
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  159 |                                 s, f->s);
      |                                 ~~~~~~~~
cc1: all warnings being treated as errors
make: *** [<builtin>: cobra_prim.o] Error 1

It appears that in the dogrep function, the compiler cannot adequately reason about the bounds check that we make to make sure that the resulting command that will be run through grep will not overflow the buffer that was created for it.

This PR separates those steps out into separate variables, and includes a minor refactor to re-copy the grep command into the buffer, instead of modifying the 5th and 6th indexes of the string to remove a flag before it is run again. This I thought made the intent of the code more readable, at the cost of an additional snprintf.

System Details

  • OS: Arch Linux, Kernel 6.11.6-arch1-1
  • gcc --version

    gcc (GCC) 14.2.1 20240910

This was caused by the compiler not being able to reason about the
bounds check done to make sure that the command size was never over the
size of the buffer.

This change separates out the checks into separate variables
@nimble-code
Copy link
Owner

it's interesting that the modified version doesn't trigger the same warning, since there's no more information there.
I believe this same warning came up in an earlier version but went away when the test just before the snprintf was added.
which version of gcc did you use?

@NickyBoy89
Copy link
Contributor Author

Hmm, it's interesting that the problem seems intermittent, and seems to be very version-dependent. I remember solving a similar type of issue in #65, just in a different place, so something as simple as separating out the variable name fixes it.

I'm using GCC gcc (GCC) 14.2.1 20240910 (also in the original PR text)

Perhaps it would be worth looking at multiple callsites for snprintf at once next time?

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

Successfully merging this pull request may close these issues.

2 participants