vext01/libdefile — comment on commit 1ffde1bb0c708374079a6e9b22252c93e5b39167
File:  line 0
By haesbaert:

On 25 October 2011 16:01, Edd Barrett

wrote:
> On Tue, Oct 25, 2011 at 10:43:06AM -0700, Christiano F. Haesbaert wrote:
>> Why heap alloc this ?
>
> How would you do it? I am trying to avoid the case where the fields can
> be either statically/dynamically allocated. Because, if you wanted to
> free this, how would you know if it was stack (or global) or heap allocated and thus if you should free the buffer?
>

You shouldn't free it, it's responsability of the caller, but then, we
don't even have a caller, it's a global state.

>> You also have a memory leak if the user specified -f.
>
> The reference to the buffer is never lost and memory consumption does
> not continually increase. Some would say this is not a leak.
>

I beg to disagree, the pointer is lost, so it's a leak. If you free it
before reassigning, I'm ok.
But then I don't see any point in heap allocing something which is
kept in a global state and can only come from a constant define or
from argv[].

> But strictly speaking, the buffer is not freed, you are right. But
> then *nothing* is freed when you exit due to errors either.
>
> I am not sure what the correct thing to do is. In my other project I
> never used err/errx, and just returned an error state which eventually
> frees *everything* that was ever heap allocated. However, I rarely see
> this in bsd code.

Sure when you call err/errx/exit there is no problem, everything gets freed.
The thing is that you *lose* a pointer, you lose the reference for
something which is malloced, and that is wrong.

>
> Thoughts?
>

Please don't get me wrong, I'm not bitching, remember when I wanted to
have df_state heap alloced so I didn't need to use the '.' instead of
the '->' ? and you said people would raise eyebrowns ? This is the
same thing, plus the leak.

Also have in mind that having a magic_file in df_state is wrong, since
we'll need to support multiple magic_files, as the current file does:

file -f /etc/magic:mymagic:/etc/balbalblab and so on.