m8pple / arch2-2014-cw1

2014/2015 - EIE2 - Architecture 2, Coursework 1

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Possible ambiguity in mips_mem_free(...) documentation

p--b opened this issue · comments

I think there may be an ambiguity here: https://github.com/m8pple/arch2-2014-cw1/blob/master/include/mips_mem.h#L105

[1] Calling mips_mem_free on an empty (zero) handle is legal.
[2] Calling mips_mem_free twice on the same handle is illegal, and the resulting behaviour is undefined

Is the following code legal?

mips_mem_h h = NULL;
mips_mem_free(h);
mips_mem_free(h);

By [1], this is legal. By [2], this is illegal.

A fix would be to insert non-zero after the same in [2].

That's not an ambiguity, 0 == 0 ;)

The second zero-free is UB, but only implementation that makes sense is to handle it the same way as the first.

At least, that's how I read it.

The point is that the specification makes two contradictory statements, meaning that it is inconsistent, causing its meaning to be ambiguous.

I don't believe the description implies that the second zero-free is UB; see the wording on line 147 of include/mips_cpu.h which is the CPU free function - it makes the disclaimer suggested above.

Specifically, the suggestion of setting the pointer to zero to prevent it from being double freed implies that it's OK if zero is double freed.

Yup, the wording should include "non-zero" to be similar to the mips_mem_free one does. Will update.