libtraceevent: Improve exception handling in parse_arg_add()
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 3 lines · On Sun, 12 Mar 2023 10:02:24 +0100 Markus Elfring <[email protected]> wrote:
Hello,
I hope that the advice “If there is no cleanup needed then just return directly.”
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 16 lines
can be taken better into account.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.3-rc1#n532Another improvable implementation detail was detected by the means of the semantic patch language (Coccinelle software).
How will the chances evolve to integrate the following change suggestion?src/event-parse.c +0
-0@@ -6388,22 +6388,20 @@ static int parse_arg_add(struct tep_print_parse **parse, char *format,
parg =calloc(1, sizeof(*parg));
if (!parg)
Quoted 8 lines
- goto error;
+ return -1;
+
parg->format= strdup(format);
- if (!parg->format)
- goto error;
+ if (!parg->format) {
+ free(parg);
^^^^^^^^^^
That's clean up.
-- Steve
Quoted 22 lines
+ return -1;
+ }
+
parg->type= type;
parg->arg= arg;
parg->len_as_arg=len_as_arg;
parg->ls= ls;
*parse = parg;
return 0;
-error:
- if (parg) {
-free(parg->format);
- free(parg);
- }
- return -1;
}
static int parse_arg_format(structtep_print_parse**parse,Regards,
Markus
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 20 lines · On Mon, 13 Mar 2023 21:35:38 +0100 Markus Elfring <[email protected]> wrote:
Quoted 11 lines
Quoted 11 lines
src/event-parse.c +0
-0@@ -6388,22 +6388,20 @@ static int parse_arg_add(struct tep_print_parse **parse, char *format,
parg =calloc(1, sizeof(*parg));
if (!parg)
- goto error;
+ return -1;
+The first failed memory allocation does not need clean-up.
Quoted 5 lines
Quoted 5 lines
parg->format= strdup(format);
- if (!parg->format)
- goto error;
+ if (!parg->format) {
+ free(parg);
Quoted 6 lines
Quoted 3 lines
^^^^^^^^^^That's clean up.
Yes. ‒ I propose to move a single memory release into an if branch as a known design approach for better exception handling.
This has nothing to do with clean ups, but personal preferences. Neither is more correct than the other. Both work. I prefer the code as is. End of discussion.
-- Steve