News for hackers. Stuff that matters.
Re:Reading the NSA’s codebase: LemonGraph review by drewg123 on Friday August 10, 2018 @11:17AM (#17734056)
I agree. I always do it this way for this exact reason.

However, I'm surprised the author did not point out that they are calling malloc() without checking for a NULL return value.

Re:Reading the NSA’s codebase: LemonGraph review by jimrandomh on Friday August 10, 2018 @12:41PM (#17734830)
Checking the return of malloc() for NULL is not currently considered good practice, because (a) on most platforms malloc is guaranteed not to return NULL, even if the system is out of memory, and (b) on the few platforms where malloc can return NULL, handling that case in practice basically never works.
Re:Reading the NSA’s codebase: LemonGraph review by loxias on Friday August 10, 2018 @08:09PM (#17737559)
> Checking the return of malloc() for NULL is not currently considered good practice

Why?

To quote malloc(3):

  On error, these functions return NULL.  NULL may also be returned by a
  successful call to malloc() with a size of zero, or by a successful call to
  calloc() with nmemb or size equal to zero.
I'm not disagreeing with your assertion that on linux, in practice, it doesn't return NULL -- (in fact, I didn't know that! and would appreciate being pointed at whatever I need to RTFM...) -- I just prefer code that's conservative, that follows the "letter of the law" when it comes to return values.
Re:Reading the NSA’s codebase: LemonGraph review by kazinator on Friday August 10, 2018 @08:17PM (#17737586)
If you write code that tests for null out of malloc, you have to commit to that code as a feature and have a test strategy for it. Simply porting the program to a platform where malloc returns null isn't enough; there are various cases to probe, like your very first malloc failing because the program was started in a low-memory environment. Or the case that the 99th malloc fails after 98 good ones.
Re:Reading the NSA’s codebase: LemonGraph review by loxias on Friday August 10, 2018 @08:31PM (#17737625)
Oh, I'm not saying you should test for NULL and then, uh, not care about the result!

If you port to some new platform, of course you need to go back and check the docs for new error codes.

For instance, I generally abort() on NULL after printing a message.

Re:Reading the NSA’s codebase: LemonGraph review by msbarnett on Friday August 10, 2018 @08:42PM (#17737654)
> For instance, I generally abort() on NULL after printing a message.

Ok, but...how are you printing the message?

On many platforms, with many standard libraries (glibc, for instance), printf et al will themselves malloc memory, so you're testing for a system telling you it couldn't allocate memory and then in response, potentially triggering further quite-likely-failing allocations that may or may not be handled by the library.

This is why people generally don't bother to check for NULL mallocs. Even if you're on a system that will even tell you you're in that situation (rare, these days, outside of the embedded world), there's almost nothing useful you can do.

Re:Reading the NSA’s codebase: LemonGraph review by convivialdingo on Friday August 10, 2018 @09:40PM (#17737835)
You’re absolutely correct, in this manner of development you just hit a wall and call it a day.

A few years back I read quite a few papers on NASA’s development standards and did quite a bit of kernel dev also.

I no longer do any allocations on daemonized or driver code like this. It’s far better to preallocate pools, store temp objects on stack and generally never malloc outside of the pool management.

When we hit the alloc wall, we now have well-defined behaviors and can gracefully choose whether to reject, clean or error out. Additionally our code is generally more performant, less buggy, and easier to debug.

Re:Reading the NSA’s codebase: LemonGraph review by loxias on Friday August 10, 2018 @11:02PM (#17738040)
You're absolutely correct. (full stop)

Honestly, in my own code I try to avoid lots of little heap allocations (duh), and when writing embedded things I don't have printf or malloc anyway.

Re:Reading the NSA’s codebase: LemonGraph review by kazinator on Friday August 10, 2018 @09:38PM (#17737828)
"care about the result" means:

- come up with a requirement about handling the bad result

- put in the code which does it

- test the code

> I generally abort() on NULL after printing a message.

On most modern systems, accessing the null pointer will also abort; you just won't get the nice error message. It will look the same as if the program hit a runaway recursion, or corrupt memory. The location in the program where it happened is equally traceable either way (with a core dump or debugger). Neither a segfault nor abort() do any graceful cleanup of anything. Basically, this is only a tiny increment in software quality over not handling the NULL at all.

Re:Reading the NSA’s codebase: LemonGraph review by PeCaN on Friday August 10, 2018 @07:48PM (#17737497)
>on most platforms malloc is guaranteed not to return NULL

This is only true on Linux, and isn't even guaranteed there. I'm pretty sure the default overcommit handling mode is still to refuse allocations that are much larger than there is free memory to support. (This was the case around 2.6.)

It's generally preferable for software to check if malloc() failed and have a chance to recover.

Re:Reading the NSA’s codebase: LemonGraph review by tjoff on Friday August 10, 2018 @01:14PM (#17735105)
Malloc will in some cases return null when the allocation is too large. This might happen long before system is out of memory and it is very much indeed preferred and practical to abort allocations in those cases.
Re:Reading the NSA’s codebase: LemonGraph review by anyfoo on Friday August 10, 2018 @01:50PM (#17735374)
Those are rather specific cases, though, aren't they? I'd imagine malloc only returns NULL when virtual address space is exhausted, not physical memory. So if you make an exceedingly large allocation for a very specific, massive thing, then yes, you might want to handle that. But if your allocation was rather small, then you are back to what your parent commentator said and are probably better off aborting.
Re:Reading the NSA’s codebase: LemonGraph review by drewg123 on Friday August 10, 2018 @03:08PM (#17735982)
It depends on whether the system you're using overcommits memory. Some OSes (eg Linux) provide settings to limit virtual memory use to some percentage of physical memory. See http://engineering.pivotal.io/post/virtual_memory_settings_i...

The only time you should avoid checking malloc() return values are for special mallocs that really cannot return NULL. One example is FreeBSD's kernel malloc() when called with the M_WAITOK flag.

Re:Reading the NSA’s codebase: LemonGraph review by tjoff on Friday August 10, 2018 @06:43PM (#17737223)
On a 32bit process that is easily achieved and also if the virtual memory has been fragmented.

I've encountered many situations where malloc will return null in real world applications. Some of which we really wanted to handle that scenario and fail gracefully or continue with less memory usage (and thus less performance).

Re:Reading the NSA’s codebase: LemonGraph review by kieckerjan on Friday August 10, 2018 @02:02PM (#17735491)
Eh? When did that get started? You are claiming resources. Not checking if you got them is a leap of faith, period. If nothing else, you can abort the program before nastier things happen.
Re:Reading the NSA’s codebase: LemonGraph review by flafla2 on Friday August 10, 2018 @07:10PM (#17737342)
This is correct. What happens when you are trying to rely on malloc'd memory for sensitive calculations? Reading an invalid pointer is undefined behavior. I see no reason why "it almost never works" to check for NULL and abort if necessary.

> Checking the return of malloc() for NULL is not currently considered good practice

This is simply false. I was a TA for the intro C class at CMU last year, and we still teach to check for NULL. In fact, we mark down students who do not do so.