News for hackers. Stuff that matters.
Re:strcpy: A niche function you don't need by jonathrg on Friday July 30, 2021 @05:05PM (#28012350)
It's too bad that many of the string handling functions in the C standard library are ticking time bombs. I like the approach taken in e.g. git which converts problematic function calls into compile errors https://github.com/git/git/blob/master/banned.h
Re:strcpy: A niche function you don't need by pjmlp on Saturday July 31, 2021 @03:06AM (#28016039)
I would rather use C++, however if I really must write C, then a string handling library like the SDS from Redis project is a must.

https://github.com/antirez/sds

Naturally turning on all security features of the C compiler being used, warnings, warnings as errors, static analysers, FORTIFY like libraries, the whole package.

And like almost everyone else, I end up having my occasional segfault regardless of the amount of precautions taken, because I am not elite enough.

Re:strcpy: A niche function you don't need by Someone1234 on Friday July 30, 2021 @05:13PM (#28012448)
I like it too, but then you're going down the uncanny valley of:

- The project is new, in which case you can easily/safely ban functions, but then why are you starting a new C project in 2021?

- The project already exists, and now you need to refactor out all the compile-time errors in order to move forward (time-consuming).

Keep in mind the first is a real question that should be answered. If your goal is to avoid undefined behavior/potential security headaches, then C should be entered into after careful consideration of cost/benefits. There are better alternatives for some projects but not others YMMV.

Re:strcpy: A niche function you don't need by tick_tock_tick on Friday July 30, 2021 @05:33PM (#28012629)
> The project is new, in which case you can safely ban functions, but then why are you starting a new C project in 2021?

Interoperability? A tons of chips have a single C compiler forked from an old version of GCC without even full C99 support and that's it. Good luck getting anything else running on that hardware.

Re:strcpy: A niche function you don't need by staticassertion on Friday July 30, 2021 @11:51PM (#28015124)
Yep, for the 0.0001% of developers who target arcane architectures C is still pretty much it.
Re:strcpy: A niche function you don't need by Someone1234 on Friday July 30, 2021 @05:42PM (#28012730)
That's why you should evaluate the cost/benefit, like I said.

> If your goal is to avoid undefined behavior/potential security headaches, then C should be entered into after careful consideration of cost/benefits. There are better alternatives for some projects but not others YMMV.

Re:strcpy: A niche function you don't need by pjmlp on Saturday July 31, 2021 @03:17AM (#28016097)
One reason would be because you heard about Azure Sphere, their security marketing, and the 7 properties of highly secure platforms, so you order a devkit.

Then when you open the box you discover that despite all the security talk, the only way to officially program the Azure Sphere is with a C SDK.

Re:strcpy: A niche function you don't need by karottenreibe on Saturday July 31, 2021 @02:16AM (#28015765)
> - The project already exists, and now you need to refactor out all the compile-time errors in order to move forward (time-consuming).

Use a static analysis tool with proper, non-brittle tracking of new vs old findings (including proper move and copy file tracking) and forbid new findings, e.g. by preventing offending code from being merged to master with tools like Gitlab, Bitbucket, what have you. Works very well in practice. Bonus points if the tool can also tell you when you modified a function and didn't clean up the old findings in there. Then you can really enforce incremental improvement.

Disclaimer: I work on Teamscale (https://teamscale.com), which does all that.

Re:strcpy: A niche function you don't need by hdjjhhvvhga on Friday July 30, 2021 @05:18PM (#28012494)
Both cases are real. If you start a new project in C, it means you have a very good reason - and hopefully a strategy of dealing with strings and other problematic issues.

If you need to deal with an older codebase, the all-or-nothing approach might not be appropriate - incremental improvements might be a better option. Yes, you will have more problems to deal with initially, but with time the situation will get better.

Re:strcpy: A niche function you don't need by ncmncm on Friday July 30, 2021 @11:06PM (#28014898)
> If you start a new project in C, it means you have a very good reason

That does not follow.

If you do it, you ought to have a good reason, but very probably don't. Any reason you thought you had, if you had any at all, argues for C++ instead, because it works anywhere C does, and enables you to choose to use modern methods that are not inherently error-prone.

Whether you do choose to use modern methods, in any non-C language you end up using, is a whole other matter. But at least you can.

Re:strcpy: A niche function you don't need by tsimionescu on Saturday July 31, 2021 @05:57AM (#28016781)
There are still platforms with C compilers but no (or very bad) C++ compilers. It's much easier to write a decent C compiler than a C++ one.
Re:strcpy: A niche function you don't need by ncmncm on Saturday July 31, 2021 @05:19PM (#28021649)
The set of such platforms that one might start a new project for is very limited. Maybe, flight computer in a Boeing 737 variant? Betting that has no C compiler. 8051?

The BSDs have supported C++ user-space programs for long enough that "just working" should be well within range.

A new project involving a BSD or Linux kernel subsystem, or PostgreSQL or SQLite component, is a plausible interpretation, where C++ is actually forbidden for wholly non-technical reasons.

Re:strcpy: A niche function you don't need by gnubison on Saturday July 31, 2021 @04:07PM (#28021170)
Yeah. If you want your code to “just work” on stuff like the BSDs, then C (or an interpreter written in C) is still your best bet.
Re:strcpy: A niche function you don't need by yaur on Friday July 30, 2021 @07:22PM (#28013627)
We use C because significant parts of our platform are GStreamer plugins… so C. Though if I were going to greenfield GStreamer C would still be a pretty reasonable option in 2021 IMO.
Re:strcpy: A niche function you don't need by pcwalton on Friday July 30, 2021 @11:57PM (#28015155)
> Though if I were going to greenfield GStreamer C would still be a pretty reasonable option in 2021 IMO.

GStreamer is routinely exposed to hostile content in non-sandboxed contexts, such as your traditional Linux desktop; it even works as a browser plugin! Among projects you might want to write in C, GStreamer strikes me as one of the least reasonable. If GStreamer were more widely deployed on phones, it'd be prime attack surface for e.g. regimes attempting to surveil journalists.

Re:strcpy: A niche function you don't need by tedunangst on Saturday July 31, 2021 @12:04AM (#28015189)
"Yeah, but it's not that bad, because like, you can always choose not install the gstreamer-plugins-ugly package."
Re:strcpy: A niche function you don't need by tialaramex on Friday July 30, 2021 @10:30PM (#28014683)
You can have reasonable C FFI from several other languages. Notably in this context Rust.

Of course if your code is just a thin layer of glue it's not worth it, you'd spend all your time getting into and out of the FFI. But if you write a non-trivial amount of code that actually does something because it isn't just a glue layer, C might actually not be the obvious choice after all.

In the particular case of Rust, somebody else apparently already did this work (not tested, and thus not vouched for): https://crates.io/crates/gstreamer

Re:strcpy: A niche function you don't need by nemothekid on Saturday July 31, 2021 @01:47AM (#28015615)
>We use C because significant parts of our platform are GStreamer plugins… so C.

Ehh..., we have written and deployed gstreamer plugins for a proprietary project at $dayjob which was written in Rust. In the case of gstreamer, they have a plethora of examples for their Rust bindings (I think it's also officially supported).

Re:strcpy: A niche function you don't need by ncmncm on Friday July 30, 2021 @11:07PM (#28014911)
GStreamer plugins do not in any way dictate C. C++ works fine. As does Zig; and even Rust, with some annoyance.
Re:strcpy: A niche function you don't need by matheusmoreira on Saturday July 31, 2021 @12:23AM (#28015280)
> why are you starting a new C project in 2021?

Why not?

Re:strcpy: A niche function you don't need by speedgoose on Saturday July 31, 2021 @03:23AM (#28016118)
It's unsafe.
Re:strcpy: A niche function you don't need by x4e on Saturday July 31, 2021 @10:54AM (#28018626)
And what is safe? C has well defined, tested, and documented specifications which layout exactly how to use C safely. Languages like Rust are more safe but nothing is just safe. If you are an inexperienced developer it is still very easy to write insecure code in Rust.

Sure, moving to safer languages is good. But it is impossible to rule out the use of a language so established that every major operating system is written in it. It is practically impossible to not use C - safe languages are normally bootstrapped by it and eventually your “safe” code will run in an environment programmed in C.

Re:strcpy: A niche function you don't need by speedgoose on Saturday July 31, 2021 @11:13AM (#28018814)
I agree that nothing is fully safe and any developer will alway introduce security vulnerabilities eventually, but history has proven than C is unsafe and generations of very competent developers wrote unsafe C despite documentation to write C safely.
Re:strcpy: A niche function you don't need by matheusmoreira on Sunday August 01, 2021 @07:46AM (#28025597)
It's pretty fun though. Despite all the problems, I just don't enjoy programming in other languages as much as I enjoy writing C. I've tried C++, Rust, Go, Zig, Javascript, Ruby, Python, Lisp, Lua... I just don't feel as free as I do while writing C.
Re:strcpy: A niche function you don't need by jonathrg on Friday July 30, 2021 @05:39PM (#28012693)
The ban can be rolled out gradually by not including banned.h everywhere.
Re:strcpy: A niche function you don't need by Someone1234 on Friday July 30, 2021 @05:44PM (#28012758)
But then you're undermining your own hard compile time check, wherein specific files or areas of the project could fall through the cracks and continue to used supposedly "banned" functions.

I'd argue doing it globally but as warning would be a better alternative than doing it file by file.

Re:strcpy: A niche function you don't need by jonathrg on Friday July 30, 2021 @05:55PM (#28012886)
A warning would be nice, is there a way to implement that without changing the compiler though?
Re:strcpy: A niche function you don't need by shakna on Friday July 30, 2021 @07:27PM (#28013650)
Something along these lines should work, for at least both of GCC and Clang:

    extern void banned() __attribute__((deprecated("This function is problematic")));

    #define strcpy(...) strcpy(__VA_ARGS__); banned;
Which would output something along the lines of:

    test.c:11:3: warning: 'banned' is deprecated: This function is problematic [-Wdeprecated-declarations]
  strcpy(x, y);
Re:strcpy: A niche function you don't need by yaur on Friday July 30, 2021 @07:27PM (#28013653)
MSVC does this, or at least did the last time I had to compile C code for windows. You should be able to do it with the preprocessor if you wanted to.
Re:strcpy: A niche function you don't need by teknopurge on Friday July 30, 2021 @05:36PM (#28012670)
Honestly not trolling - I disagree with the "ticking time bombs" comment. If you feel that way the devs should be using Rust.

C is a sharp knife with no handle; this is it's purpose as a language and tool.

cc: [email protected]

Re:strcpy: A niche function you don't need by tialaramex on Friday July 30, 2021 @06:18PM (#28013116)
> the devs should be using Rust.

Yes, they should. Or several other things depending on what exactly they need.

> C is a sharp knife with no handle; this is it's purpose as a language and tool.

Help me out here HN resident survivalists, carpenters, maybe circus knife throwers. What is the "purpose" of a "sharp knife with no handle" exactly? How often have you thought, "Man, it'd be so much easier to gather firewood, carve decorations or score a bullseye if only the blade would sink into my own flesh while I was using it because it doesn't have a handle" ?

Historically the argument was, "We're using C because alternatives like Java or Python or whatever aren't fast enough or capable enough". OK. But, somewhere in the last few years it moved to, "We're using C because alternatives aren't dangerous enough" and that's crazy.

Re:strcpy: A niche function you don't need by teknopurge on Friday July 30, 2021 @06:24PM (#28013168)
BYOH - Bring You Own Handle

The purpose is not to assume what the developer wants, but provide access to all the resources(and manipulation) they need.

If you need guardrails there are 10s of languages designed for specific purposes.

Re:strcpy: A niche function you don't need by orf on Friday July 30, 2021 @08:01PM (#28013909)
However at some point you realize that there is an optimal and general-enough handle shape that everyone ends up individually re-creating.

You should use a knife that comes with such a handle but gives you the option to take it off when you want to impress your friends with your knife-juggling skills and feel you have a few fingers too many.

Re:strcpy: A niche function you don't need by pjmlp on Saturday July 31, 2021 @03:25AM (#28016130)
Before C was born, developers were using system programming languages with handles just fine.

C lacks a handle because its designers didn't care to provide one, it is after all based on a language whose main purpose was to Bootstrap CPL.

Re:strcpy: A niche function you don't need by hiccuphippo on Friday July 30, 2021 @07:16PM (#28013587)
FWIW, Zig is as sharp as C but with a handle.
Re:strcpy: A niche function you don't need by tored on Friday July 30, 2021 @08:01PM (#28013905)
Why should you pick Zig and not Rust, D or Go?
Re:strcpy: A niche function you don't need by chalst on Saturday July 31, 2021 @05:52AM (#28016771)
Zig is probably nicer if you want to write code that is to be called from C.
Re:strcpy: A niche function you don't need by jonathrg on Friday July 30, 2021 @05:51PM (#28012836)
These are just simple API design issues in the standard library, nothing to do with the language itself.