Tíquete #47504

superfluous conditionals around free()

: 2023-03-01 21:58 Última Atualização: 2023-03-01 22:49

Relator:
Dono:
(Nenhum)
Tipo:
Estado:
Aberto
Componente:
(Nenhum)
Marcos:
(Nenhum)
Prioridade:
5 - Medium
Gravidade:
5 - Medium
Resolução:
Nenhum
Arquivo:
2

Details

Remove superfluous conditions around free().

It is perfectly valid to call free(NULL):

https://pubs.opengroup.org/onlinepubs/9699919799/functions/free.html

If ptr is a null pointer, no action shall occur.

Hence i suggest the following patch to remove the superfluous "if" statements before calling free():

The coccinelle patch below

$ cat free-without-if-null.0.cocci ; echo EOF
// POSIX: free(NULL) is perfectly valid
// quote: If ptr is a null pointer, no action shall occur.
@ rule1 @
expression e;
@@

- if (e != NULL)
-  { free(e); }
+ free(e);

EOF

would be run like

cd freeciv
# attention, we will modify the files in-place for simplicity here:
freeciv$ find ./ \( -name "*.[ch]" -o -name "*.cpp" \) -exec spatch --sp-file ~/free-without-if-null.0.cocci --in-place

I can attach a sample patch against the main branch.

Ticket History (3/4 Histories)

2023-03-01 21:58 Updated by: the-b
  • New Ticket "superfluous conditionals around free()" created
2023-03-01 22:49 Updated by: the-b
Comentário

From the looks we could remove the function string_free and use plain free() directly. WDYT?


And a quick question about strvec_new, why doesn't this simply use fc_calloc?


thanks,

Attachment File List

Editar

Please login to add comment to this ticket » Login