Skip to contentSkip to conversation

fix potential memory loss and exit codes

PATCH Superseded Donald Yandt 2 participants 9 messages Mar 1, 2024 – Mar 4, 2024 netfilter

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover fix potential memory loss and exit codes

Vector data will be lost if reallocation fails, leading to undefined behaviour. Additionally, size_t should be used to provide the indices and sizes of vector elements.

If no configuration file or an invalid parameter is provided, the daemon should exit with a failure status.

Donald Yandt (3):
conntrackd: prevent memory loss if reallocation fails
conntrackd: use size_t for element indices
conntrackd: exit with failure status

 src/main.c   | 5 ++---
 src/vector.c | 9 ++++-----
 2 files changed, 6 insertions(+), 8 deletions(-)
Signature · 1 line
2.44.0
#1 lore raw

PATCH 1/3 conntrackd: prevent memory loss if reallocation fails

---
 src/vector.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
src/vector.c +3 -2
index c81e7ce..7f9bc3c 100644
@@ -62,11 +62,12 @@ int vector_add(struct vector *v, void *data)
 {
 	if (v->cur_elems >= v->max_elems) {
 		v->max_elems += DEFAULT_VECTOR_GROWTH;
-		v->data = realloc(v->data, v->max_elems * v->size);
-		if (v->data == NULL) {
+		void *ptr = realloc(v->data, v->max_elems * v->size);
+		if (ptr == NULL) {
 			v->max_elems -= DEFAULT_VECTOR_GROWTH;
 			return -1;
 		}
+		v->data = ptr;
 	}
 	memcpy(v->data + (v->size * v->cur_elems), data, v->size);
 	v->cur_elems++;
Signature · 1 line
2.44.0
#2 lore raw

Re: [PATCH conntrack-tools 1/3] conntrackd: prevent memory loss if reallocation fails

Quoted 10 lines · Donald Yandt
---
 src/vector.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
src/vector.c +0 -0
index c81e7ce..7f9bc3c 100644

src/vector.c int vector_add(struct vector *v, void *data) · line 66

Quoted 5 lines
src/vector.c +0 -2
@@ -62,11 +62,12 @@ int vector_add(struct vector *v, void *data)
 {
 	if (v->cur_elems >= v->max_elems) {
 		v->max_elems += DEFAULT_VECTOR_GROWTH;
-		v->data = realloc(v->data, v->max_elems * v->size);
-		if (v->data == NULL) {

Good catch.

src/vector.c int vector_add(struct vector *v, void *data) · line 65

Quoted 1 line
src/vector.c +1 -0
+		void *ptr = realloc(v->data, v->max_elems * v->size);

Could you declare void *ptr at the top of the function? Following old style variable declarations?

Thanks.

Quoted 12 lines
src/vector.c +2 -0
+		if (ptr == NULL) {
 			v->max_elems -= DEFAULT_VECTOR_GROWTH;
 			return -1;
 		}
+		v->data = ptr;
 	}
 	memcpy(v->data + (v->size * v->cur_elems), data, v->size);
 	v->cur_elems++;
Signature · 1 line
2.44.0
#3 lore raw

Re: [PATCH conntrack-tools 1/3] conntrackd: prevent memory loss if reallocation fails

Quoted 6 lines · Pablo Neira Ayuso

Could you declare void *ptr at the top of the function? Following old style variable declarations?

Thanks.

I moved the variable declaration to the top of the function in v2 as suggested.

Thanks!

#4 lore raw

PATCH 2/3 conntrackd: use size_t for element indices

In reply to Donald Yandt
---
 src/vector.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
src/vector.c +1 -3
index 7f9bc3c..ac1f5d9 100644
@@ -23,9 +23,7 @@
 
 struct vector {
 	char *data;
-	unsigned int cur_elems;
-	unsigned int max_elems;
-	size_t size;
+	size_t cur_elems, max_elems, size;
 };
 
 #define DEFAULT_VECTOR_MEMBERS	8
Signature · 1 line
2.44.0
#5 lore raw

Re: [PATCH conntrack-tools 2/3] conntrackd: use size_t for element indices

Hi,

Could you describe why these are needed?

Thanks!

Quoted 24 lines · Donald Yandt
---
 src/vector.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
src/vector.c +1 -3
index 7f9bc3c..ac1f5d9 100644
@@ -23,9 +23,7 @@
 
 struct vector {
 	char *data;
-	unsigned int cur_elems;
-	unsigned int max_elems;
-	size_t size;
+	size_t cur_elems, max_elems, size;
 };
 
 #define DEFAULT_VECTOR_MEMBERS	8
Signature · 1 line
2.44.0
#6 lore raw

Re: [PATCH conntrack-tools 2/3] conntrackd: use size_t for element indices

Quoted 7 lines · Pablo Neira Ayuso

Hi,

Could you describe why these are needed?

Thanks!

Hi Pablo,

I mentioned it briefly in the cover letter and explained why it should be used in the commit message for version 2.
If you require any additional detail, please let me know.

Thank you

Quoted 32 lines · Pablo Neira Ayuso

Hi,

Could you describe why these are needed?

Thanks!

Quoted 24 lines · Donald Yandt
---
 src/vector.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
src/vector.c +1 -4
index 7f9bc3c..ac1f5d9 100644
@@ -23,9 +23,7 @@
 
 struct vector {
      char *data;
-     unsigned int cur_elems;
-     unsigned int max_elems;
-     size_t size;
+     size_t cur_elems, max_elems, size;
 };
 
 #define DEFAULT_VECTOR_MEMBERS       8
--

2.44.0

#7 lore raw

Re: [PATCH conntrack-tools 2/3] conntrackd: use size_t for element indices

Quoted 6 lines · Donald Yandt
Quoted 5 lines · Pablo Neira Ayuso

Hi,

Could you describe why these are needed?

Quoted 8 lines
Quoted 2 lines

Thanks!

Hi Pablo,

I mentioned it briefly in the cover letter and explained why it should be used in the commit message for version 2.
If you require any additional detail, please let me know.

Cover letter gets lost when applying patches.

Split descriptions into each individual commit, it really helps us.

Thanks a lot

#8 lore raw

PATCH 3/3 conntrackd: exit with failure status

In reply to Donald Yandt
---
 src/main.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
src/main.c +2 -3
index de4773d..c6b2600 100644
@@ -175,7 +175,7 @@ int main(int argc, char *argv[])
 			}
 			show_usage(argv[0]);
 			dlog(LOG_ERR, "Missing config filename");
-			break;
+			exit(EXIT_FAILURE);
 		case 'F':
 			set_operation_mode(&type, REQUEST, argv);
 			i = set_action_by_table(i, argc, argv,
@@ -309,8 +309,7 @@ int main(int argc, char *argv[])
 		default:
 			show_usage(argv[0]);
 			dlog(LOG_ERR, "Unknown option: %s", argv[i]);
-			return 0;
-			break;
+			exit(EXIT_FAILURE);
 		}
 	}
 
Signature · 1 line
2.44.0