News for hackers. Stuff that matters.
Re:size_t-to-int vulnerability in Linux’s filesystem layer by dkirill on Tuesday July 20, 2021 @01:14PM (#27896455)
> and the compiler doesn't even warn you that size is bigger than x

That's not true tho, compiler with reasonable flags set will definitely warn you and if you really don't like this kind of code you can force compiler to issue an error instead

Re:size_t-to-int vulnerability in Linux’s filesystem layer by simias on Tuesday July 20, 2021 @01:24PM (#27896567)
What flags you have in mind? Because this code doesn't generate any warning with GCC 11.1.0 with -Wall -Wextra:

    int main(void) {
        int some_int = 1234567;
        char c = some_int;

        return c;
    }
Re:size_t-to-int vulnerability in Linux’s filesystem layer by kllrnohj on Tuesday July 20, 2021 @01:36PM (#27896711)
Re:size_t-to-int vulnerability in Linux’s filesystem layer by aaronmdjones on Tuesday July 20, 2021 @01:33PM (#27896668)
-Wall -Wextra -Wpedantic does not enable all diagnostics.

This is GNU's idea of "all".

Contrast to Clang's -Weverything, which will.

Re:size_t-to-int vulnerability in Linux’s filesystem layer by lazide on Tuesday July 20, 2021 @04:36PM (#27898955)
It seems though that the point is made, right? Even 'good' approaches miss on what should be a clear 'whoa, are you sure?' type warning. There are a lot of footguns wandering around in C/C++ land.
Re:size_t-to-int vulnerability in Linux’s filesystem layer by BoorishBears on Tuesday July 20, 2021 @04:53PM (#27899169)
No, the point was you want don't get a warning and it will silently wrap. You can scroll up if you've forgotten.

And it is false. My default configuration C++ project created in Clion shows it very clearly, and even pesters to use int32/int64 over int/long.

But as usual the default fallback when you're wrong about C++ is "uh yeah but lotta footguns amirite"

As if there aren't enough that we need to start making them up...

Re:size_t-to-int vulnerability in Linux’s filesystem layer by opheliate on Wednesday July 21, 2021 @04:15AM (#27903694)
And yet RedHat's recommended compiler flags for GCC [0], for example, do not appear to catch the wrapping assignment in the above example code.

0: https://developers.redhat.com/blog/2018/03/21/compiler-and-l...

Re:size_t-to-int vulnerability in Linux’s filesystem layer by BoorishBears on Wednesday July 21, 2021 @10:19AM (#27906625)
Ah yes, of course the goalpost was "you need to customize your settings to catch it" above.

Now that the default in the most beginner friendly of IDEs catches it, the goalpost is "my pet source of customization designed with C++98 in mind doesn't catch this"

Of course, even your pet source of customization caught up: https://developers.redhat.com/blog/2021/04/06/get-started-wi...

Re:size_t-to-int vulnerability in Linux’s filesystem layer by opheliate on Wednesday July 21, 2021 @01:51PM (#27909591)
If by "caught up", you mean talked about clang-tidy in a separate post, which is definitely not a GCC compiler flag, then sure.

The goalpost, since you're insistent on being explicit about it, was whether a C/C++ compiler "with reasonable flags" will catch the implicit wrap. GCC is a very popular compiler, and to be honest, I'm still not sure how to get it to warn on the above code, if doing so is possible.

Edit: Just read the rest of the thread, it's -Wconversion, which I suppose makes sense. Ignore me, point taken.

Re:size_t-to-int vulnerability in Linux’s filesystem layer by lazide on Wednesday July 21, 2021 @12:00PM (#27907995)
If said beginning friendly IDE is used by only a couple percent of the ecosystem, it seems disingenuous to use it as proof this isn't a problem in this context?
Re:size_t-to-int vulnerability in Linux’s filesystem layer by BoorishBears on Wednesday July 21, 2021 @01:02PM (#27908904)
Ok so we're going to keep shifting the goalposts, now it's "there aren't enough beginners relative to total usage so beginner friendly IDE isn't enough"...

I mean MSVS uses Clang-tidy too, Clang-tidy integrates style guides provided by Mozilla and Google.

Most C++ Google projects have clang-tidy configs.

Clang-tidy is literally table-stakes for modern C++ tooling.

Github shows 970,000 commits related to setting up clang-tidy

But uh, yeah, let's see where the goalpost skitters to next.

-

The irony is I said above, C++ has enough footguns without sticking your fingers in your ears and ignoring boring, easy to setup, widely well known and well used tooling.

But in the war against C++ no stone must be left unturned.

C++ is a tiny fraction of all the code I've written in my life but it irks me to no end that people can't deal with the idea that language safety can improve, that tooling can be considered part of that safety. Or rather they can... unless they're talking about C/C++

Re:size_t-to-int vulnerability in Linux’s filesystem layer by lazide on Wednesday July 21, 2021 @09:37PM (#27914359)
I’m definitely not moving any goalposts I know of!

I thought the point I had been making, as had others, is that by default this is an easy footgun.

There are all sorts of things that can be added on to all languages to help - if you know it’s a problem worth solving, etc. which is inevitably after you’ve footgunned yourself with it bad enough you felt the need to research how to prevent it.

Other languages just do the safer thing (or most compilers By default warn at least about common footguns) more - which is the whole point of this thread?

Re:size_t-to-int vulnerability in Linux’s filesystem layer by BoorishBears on Thursday July 22, 2021 @12:34PM (#27921023)
There was one point, C++ won't warn you by default .

But tooling that is incredibly common, that beginners will run into even if they take the path of least resistance, and experts will use because it enforces standards at the very least, covers it.

Like Js without linters is a minefield, but everyone accepts you should lint your Js. Why does that change when C++ is involved?

Re:size_t-to-int vulnerability in Linux’s filesystem layer by rocqua on Tuesday July 20, 2021 @02:21PM (#27897299)
The reason for this decision is so that compiler upgrades with -Wall and -Werror don't break builds.

I can see the reason behind it, but I feel that this behavior is something you opt into when you use -Werror.

Re:size_t-to-int vulnerability in Linux’s filesystem layer by derefr on Tuesday July 20, 2021 @03:35PM (#27898206)
> The reason for this decision is so that compiler upgrades with -Wall and -Werror don't break builds.

It feels like the "right thing" here would instead be for the compiler to allow build scripts to reference a specific point-in-time semantics for -Wall.

For example, `-Wall=9.3.0` could be used to mean "all the error checks that GCC v9.3.0 knew how to run".

Or better yet (for portability), a date, e.g. `-Wall=20210720` to mean "all the error checks built into the compiler as of builds up-to-and-including [date]."

To implement this, compilers would just need to know what version/date each of their error checks was first introduced. Errors newer than the user's specifier, could then be filtered out of -Wall, before -Wall is applied.

With such a flag, you could "lock" your CI buildscript to a specific snapshot of warnings, just like you "lock" dependencies to a specific set of resolved versions.

And just like dependency locking, if you have some time on your hands one day, you could "unlock" the error-check-suite snapshot, resolve all the new error-checks introduced, and then re-lock to the new error-check-suite timestamp.

Re:size_t-to-int vulnerability in Linux’s filesystem layer by HanyouHottie on Tuesday July 20, 2021 @04:06PM (#27898593)
Re:size_t-to-int vulnerability in Linux’s filesystem layer by antoinealb on Tuesday July 20, 2021 @04:12PM (#27898657)
I think it might be more of an headache: what if somebody fixes a bug in an analyzer so that it catches things it used to miss ? Should it be a breaking change ?

Personally i would vote for "Wall with Werror" means no guarantee for your build.

Re:size_t-to-int vulnerability in Linux’s filesystem layer by wheybags on Tuesday July 20, 2021 @05:46PM (#27899803)
The real solution: leave Werror off by default, activate it only during CI builds
Re:size_t-to-int vulnerability in Linux’s filesystem layer by derefr on Wednesday July 21, 2021 @10:56AM (#27907163)
That's even worse, because then an upgrade to the compiler in the managed CI runner (e.g. Github Actions') base-image will translate to the same version of the code failing where it previously succeeded, with nobody sure why.

At least with -Werror on at all times, devs will tend to upgrade before the very-stable CI environment does, and thereby catch the problem at development time (usually less time-pressure) rather than release-cutting time (usually more time-pressure, esp. if the release is a hotfix.)

-----

Mind you, it does work to enable -Werror only in CI, if you lock your CI environment / compiler Docker image / etc. to a specific stable version, and treat that as the thing to re-lock in place of the "error-check suite snapshot version."

This has the disadvantage, though, that you can't take advantage of newly-stable/newly-unstable language features, or of newly-introduced compiler optimizations, without biting the bullet and taking on the work of fixing the errors introduced by re-locking the base-image.

With a separate flag for locking down the error-check-suite snapshot version, you could continue to upgrade the compiler — and thereby get access to new features / optimizations — while staying on a particular build regression "scope."

Re:size_t-to-int vulnerability in Linux’s filesystem layer by account42 on Thursday July 22, 2021 @04:35AM (#27916731)
> That's even worse, because then an upgrade to the compiler in the managed CI runner (e.g. Github Actions') base-image will translate to the same version of the code failing where it previously succeeded

If you don't want your build to fail on warnings, don't use -Werror. If you want it to only fail on specific warnings, use -Werror=...

> with nobody sure why

Unless they look at the errors in the compiler output. What does it matter if it was brought on by a compiler update or a push?

> At least with -Werror on at all times, devs will tend to upgrade before the very-stable CI environment does

Nothing wrong with -Werror for devs - the problem is when you ship code to others and leave -Werror on by default.

Re:size_t-to-int vulnerability in Linux’s filesystem layer by GoblinSlayer on Wednesday July 21, 2021 @07:37AM (#27904929)
Is -Werror really supposed to not break builds?
Re:size_t-to-int vulnerability in Linux’s filesystem layer by account42 on Thursday July 22, 2021 @04:36AM (#27916741)
The whole point of -Werror is to break builds and -Wall / -Wextra are definitely not frozen. If you can't handle compiler updates resulting in errors, don't use -Werror in that environment.
Re:size_t-to-int vulnerability in Linux’s filesystem layer by 130e13a on Tuesday July 20, 2021 @01:46PM (#27896822)
keep in mind though that -Weverything is not intended to be used in production: https://quuxplusone.github.io/blog/2018/12/06/dont-use-wever...
Re:size_t-to-int vulnerability in Linux’s filesystem layer by account42 on Thursday July 22, 2021 @04:38AM (#27916748)
-Weverything is great for CI though, in compination with lots of -Wno-... flags to disable warnings you don't want. Instead of having to manually look out for new warning flags you will get all automatically.
Re:size_t-to-int vulnerability in Linux’s filesystem layer by aaronmdjones on Sunday July 25, 2021 @02:22PM (#27951558)
Yep, this is what I do; throw in -Weverything followed by a few things like -Wno-packed -Wno-padded -Wno-unused-parameter.
Re:size_t-to-int vulnerability in Linux’s filesystem layer by gumby on Tuesday July 20, 2021 @04:59PM (#27899240)
> This is GNU's idea of "all".

Unfortunately, over the years people baked the semantics of -Wall into their builds so new diagnostics could not be added to that flag.

And clang’s -Weverything shows how the opposite can fail as well

Re:size_t-to-int vulnerability in Linux’s filesystem layer by not2b on Tuesday July 20, 2021 @06:27PM (#27900125)
There are some very wrong-headed warning options in gcc, such that turning them on and avoiding getting them will make your code worse. So -Wall means 'all recommended warnings'.

Also there are some warnings that won't be produced if you compile without optimization, because the needed analysis isn't performed.

Re:size_t-to-int vulnerability in Linux’s filesystem layer by account42 on Thursday July 22, 2021 @04:50AM (#27916810)
And yet we have things like -Wmaybe-uninitialized in -Wall which by definition will occasionally warn on perfectly good code.
Re:size_t-to-int vulnerability in Linux’s filesystem layer by ridiculous_fish on Tuesday July 20, 2021 @01:30PM (#27896632)
-Wconversion will do it.
Re:size_t-to-int vulnerability in Linux’s filesystem layer by stjohnswarts on Tuesday July 20, 2021 @05:16PM (#27899459)
-Wconversion will catch this
Re:size_t-to-int vulnerability in Linux’s filesystem layer by rualca on Tuesday July 20, 2021 @01:58PM (#27897001)
>What flags you have in mind?

-Wconversion

https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

This is common knowledge for ages. Any cursory Google search returns countless answers.

Take this post made over a decade ago.

https://stackoverflow.com/questions/1730255/gcc-shouldnt-a-w...

Re:size_t-to-int vulnerability in Linux’s filesystem layer by tialaramex on Tuesday July 20, 2021 @01:33PM (#27896665)
Fair point although it seems "reasonable" varies from one platform to another, it doesn't warn out of the box for me but people have reported MSVC gets warnings here.