News for hackers. Stuff that matters.
Re:How to C in 2016 by raverbashing on Friday January 08, 2016 @02:20PM (#10867449)
Sorry if I think you're overstating your achievements.

- Was your code unit-tested?

- Did you use the 'strn' functions? (strncpy, instead of strcpy for example)

- Did your code run under Valgrind? (was there valgrind at that time? I'm not sure)

- Was it tested for memory leaks?

- Was there input fuzzying tests?

I agree that there are ways of writing great C code (like nginx, the linux and BSD kernels, etc)

But from a certain point on you're just wasting developer time when you would have a better solution in Java/Python/Ruby, etc with much less developer time and much less chance of bugs and security issues.

Re:How to C in 2016 by klodolph on Friday January 08, 2016 @05:21PM (#10868761)
The 'strn' functions are wrong, stop using them.

https://randomascii.wordpress.com/2013/04/03/stop-using-strn...

Re:How to C in 2016 by raverbashing on Saturday January 09, 2016 @03:11AM (#10870508)
This solution uses templates, which are C++, not C

But if you're using C++, use C++ strings, you don't need strcpy

There's strlcpy mentioned there, which seems a better C solution

Re:How to C in 2016 by nitrogen on Friday January 08, 2016 @06:49PM (#10869213)
strncpy is fine if you wrap it with a length error check or explicit 0 terminator.
Re:How to C in 2016 by icedchai on Friday January 08, 2016 @02:46PM (#10867642)
Unit tests? In C? I worked on a system processing millions of dollars a day in real time transactions, written in C++ with lower level libraries written in C. Some of it was written in K&R pre-ANSI C.

There was not one unit test. And there probably still isn't.

Re:How to C in 2016 by redahs on Friday January 08, 2016 @06:54PM (#10869234)
C is one of the easier languages to do unit testing in:

1. write a new one file script which parses function declarations from a source code file

2. have the script find all function declarations which start with "utest_", accept no parameters, and return "int" or "bool".

3. have the script write out a new C file which calls all of matching unit test functions and asserts their return value is equal to 1

4. have your Makefile build and run a unit test binary using code generated by your script for each source code file containing greater than 0 unit tests.

Writing a script to parse function declarations from your C source code is useful because you can extend the script later on to generate documentation, code statistics, or conduct project-specific static analysis.

Re:How to C in 2016 by 2trill2spill on Friday January 08, 2016 @04:44PM (#10868549)
> Unit tests? In C?

Why wouldn't you unit test in C? I do and I have found lot's of bugs as a result and maintaining code is much easier.

Re:How to C in 2016 by icedchai on Friday January 08, 2016 @04:52PM (#10868593)
I agree. But it seems to happen less than in other languages. I'd say it's not part of the culture.
[deleted]
Re:How to C in 2016 by icedchai on Friday January 08, 2016 @06:53PM (#10869233)
It is, of course, possible. It is just rarely practiced, I think. It is as necessary as it is in any other language, which means either not at all, absolutely necessary, or somewhere in between depending on your point of view...

I have seen a few approaches: google test (actually a C++ framework) used to test C code, "custom" test frameworks (makefiles, test-runner shell scripts, and small main() functions that actually run tests), etc.

Re:How to C in 2016 by brown9-2 on Friday January 08, 2016 @08:23PM (#10869592)
I don't think this is the boast that you think it is.
Re:How to C in 2016 by icedchai on Saturday January 09, 2016 @01:06AM (#10870286)
It wasn't meant to be a boast.