Skip to contentSkip to conversation

Fix sizeof usage in get_permutations

PATCH In review Matthew Daley 4 participants 4 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix sizeof usage in get_permutations

Currently it gets the size of an otherwise unrelated, unused variable instead of the expected struct size.

---
 builtin/pack-redundant.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
builtin/pack-redundant.c +3 -3
index f5c6afc..7544687 100644
@@ -301,14 +301,14 @@ static void pll_free(struct pll *l)
  */
 static struct pll * get_permutations(struct pack_list *list, int n)
 {
-	struct pll *subset, *ret = NULL, *new_pll = NULL, *pll;
+	struct pll *subset, *ret = NULL, *new_pll = NULL;
 
 	if (list == NULL || pack_list_size(list) < n || n == 0)
 		return NULL;
 
 	if (n == 1) {
 		while (list) {
-			new_pll = xmalloc(sizeof(pll));
+			new_pll = xmalloc(sizeof(struct pll));
 			new_pll->pl = NULL;
 			pack_list_insert(&new_pll->pl, list);
 			new_pll->next = ret;
@@ -321,7 +321,7 @@ static struct pll * get_permutations(struct pack_list *list, int n)
 	while (list->next) {
 		subset = get_permutations(list->next, n - 1);
 		while (subset) {
-			new_pll = xmalloc(sizeof(pll));
+			new_pll = xmalloc(sizeof(struct pll));
 			new_pll->pl = subset->pl;
 			pack_list_insert(&new_pll->pl, list);
 			new_pll->next = ret;
Signature · 1 line
1.7.10.4
#1 lore raw

Thanks; it shows how rarely this obscure tool is used these days.

#2 lore raw
In reply to Matthew Daley
Quoted 30 lines · Matthew Daley

Currently it gets the size of an otherwise unrelated, unused variable instead of the expected struct size.

---
builtin/pack-redundant.c | 6 +++---

1 file changed, 3 insertions(+), 3 deletions(-)
builtin/pack-redundant.c +2 -2
index f5c6afc..7544687 100644
@@ -301,14 +301,14 @@ static void pll_free(struct pll *l)
 */
 static struct pll * get_permutations(struct pack_list *list, int n)
 {
- struct pll *subset, *ret = NULL, *new_pll = NULL, *pll;
+ struct pll *subset, *ret = NULL, *new_pll = NULL;
 
 if (list == NULL || pack_list_size(list) < n || n == 0)
 return NULL;
 
 if (n == 1) {
 while (list) {
- new_pll = xmalloc(sizeof(pll));
+ new_pll = xmalloc(sizeof(struct pll));
 new_pll->pl = NULL;
 pack_list_insert(&new_pll->pl, list);
 new_pll->next = ret;

builtin/pack-redundant.c static struct pll * get_permutations(struct · line 324

Quoted 5 lines
builtin/pack-redundant.c +1 -1
@@ -321,7 +321,7 @@ static struct pll * get_permutations(struct
 pack_list *list, int n) while (list->next) {
 subset = get_permutations(list->next, n - 1);
 while (subset) {
- new_pll = xmalloc(sizeof(pll));
+ new_pll = xmalloc(sizeof(struct pll));

Why not just
new_pll = xmalloc(sizeof(*new_pll));

Quoted 3 lines
builtin/pack-redundant.c +0 -0
 new_pll->pl = subset->pl;
 pack_list_insert(&new_pll->pl, list);
 new_pll->next = ret;
#3 lore raw
Quoted 33 lines · Joachim Schmitz
Quoted 32 lines · Matthew Daley

Currently it gets the size of an otherwise unrelated, unused variable instead of the expected struct size.

---
builtin/pack-redundant.c | 6 +++---

1 file changed, 3 insertions(+), 3 deletions(-)
builtin/pack-redundant.c +2 -2
index f5c6afc..7544687 100644
@@ -301,14 +301,14 @@ static void pll_free(struct pll *l)
 */
 static struct pll * get_permutations(struct pack_list *list, int n)
 {
- struct pll *subset, *ret = NULL, *new_pll = NULL, *pll;
+ struct pll *subset, *ret = NULL, *new_pll = NULL;
 
 if (list == NULL || pack_list_size(list) < n || n == 0)
 return NULL;
 
 if (n == 1) {
 while (list) {
- new_pll = xmalloc(sizeof(pll));
+ new_pll = xmalloc(sizeof(struct pll));
 new_pll->pl = NULL;
 pack_list_insert(&new_pll->pl, list);
 new_pll->next = ret;
@@ -321,7 +321,7 @@ static struct pll * get_permutations(struct
 pack_list *list, int n) while (list->next) {
Quoted 8 lines

builtin/pack-redundant.c static struct pll * get_permutations(struct pack_list *list, int n) · line 324

Quoted 4 lines
builtin/pack-redundant.c +1 -1
 subset = get_permutations(list->next, n - 1);
 while (subset) {
- new_pll = xmalloc(sizeof(pll));
+ new_pll = xmalloc(sizeof(struct pll));

Why not just
new_pll = xmalloc(sizeof(*new_pll));

I prefer that one; it's Linux kernel style.

Signature · 1 line
Felipe Contreras