Skip to contentSkip to conversation

builtin: submodule--helper: Add allocation check of module_cb structure objects may be helpful in case of allocation failure

PATCH In review Li kunyu 3 participants 3 messages Nov 22, 2022

lore
Share & tools
lore

Li kunyu

author
#0 lore raw

PATCH 1/1 builtin: submodule--helper: Add allocation check of module_cb structure objects may be helpful in case of allocation failure

The purpose of using temp is unknown, but adding an allocation failure report should be helpful.

---
 builtin/submodule--helper.c | 3 +++
 1 file changed, 3 insertions(+)
builtin/submodule--helper.c +3 -0
index b63f420ece..2e379623ea 100644
@@ -1077,6 +1077,9 @@ static void submodule_summary_callback(struct diff_queue_struct *q,
 		if (!S_ISGITLINK(p->one->mode) && !S_ISGITLINK(p->two->mode))
 			continue;
 		temp = (struct module_cb*)malloc(sizeof(struct module_cb));
+		if (!temp)
+			 BUG("The module_cb structure object fails to be allocated and the program may terminate abnormally");
+
 		temp->mod_src = p->one->mode;
 		temp->mod_dst = p->two->mode;
 		temp->oid_src = p->one->oid;
Signature · 1 line
2.18.2
#1 lore raw
Quoted 2 lines · Li kunyu

The purpose of using temp is unknown, but adding an allocation failure report should be helpful.

Yuck. Use of xmalloc() may be more appropriate.

Use of BUG() in a context like this is never the right thing to do. BUG() is reserved for cases where the condition should NEVER hold true in a correctly written program, and even for a correctly written program, malloc() can fail after you allocated too much memory from the heap.

Quoted 20 lines
---
 builtin/submodule--helper.c | 3 +++
 1 file changed, 3 insertions(+)
builtin/submodule--helper.c +3 -0
index b63f420ece..2e379623ea 100644
@@ -1077,6 +1077,9 @@ static void submodule_summary_callback(struct diff_queue_struct *q,
 		if (!S_ISGITLINK(p->one->mode) && !S_ISGITLINK(p->two->mode))
 			continue;
 		temp = (struct module_cb*)malloc(sizeof(struct module_cb));
+		if (!temp)
+			 BUG("The module_cb structure object fails to be allocated and the program may terminate abnormally");
+
 		temp->mod_src = p->one->mode;
 		temp->mod_dst = p->two->mode;
 		temp->oid_src = p->one->oid;
#2 lore raw
In reply to Li kunyu
Quoted 3 lines · Li kunyu

The purpose of using temp is unknown, but adding an allocation failure report should be helpful.

Don't you understand the code?

Quoted 10 lines
---
 builtin/submodule--helper.c | 3 +++
 1 file changed, 3 insertions(+)
builtin/submodule--helper.c +0 -0
index b63f420ece..2e379623ea 100644

builtin/submodule--helper.c static void submodule_summary_callback(struct diff_queue_struct *q, · line 1082

Quoted 6 lines
builtin/submodule--helper.c +3 -0
@@ -1077,6 +1077,9 @@ static void submodule_summary_callback(struct diff_queue_struct *q,
 		if (!S_ISGITLINK(p->one->mode) && !S_ISGITLINK(p->two->mode))
 			continue;
 		temp = (struct module_cb*)malloc(sizeof(struct module_cb));
+		if (!temp)
+			 BUG("The module_cb structure object fails to be allocated and the program may terminate abnormally");
+

Why do you check whenever allocating temp is successful?

Signature · 1 line
An old man doll... just what I always wanted! - Clara