Skip to contentSkip to conversation

clk/ti/adpll: allocate room for terminating null

PATCH Superseded Stephen Kitt 3 participants 12 messages Sep 27, 2019 – Nov 8, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 clk/ti/adpll: allocate room for terminating null

The buffer allocated in ti_adpll_clk_get_name doesn't account for the terminating null. This patch adds the extra byte, and switches to snprintf to avoid overflowing.

---
 drivers/clk/ti/adpll.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/clk/ti/adpll.c +4 -3
index fdfb90058504..27933c4e8a27 100644
@@ -196,12 +196,13 @@ static const char *ti_adpll_clk_get_name(struct ti_adpll_data *d,
 	} else {
 		const char *base_name = "adpll";
 		char *buf;
+		size_t size = 8 + 1 + strlen(base_name) + 1 +
+			      strlen(postfix) + 1;
 
-		buf = devm_kzalloc(d->dev, 8 + 1 + strlen(base_name) + 1 +
-				    strlen(postfix), GFP_KERNEL);
+		buf = devm_kzalloc(d->dev, size, GFP_KERNEL);
 		if (!buf)
 			return NULL;
-		sprintf(buf, "%08lx.%s.%s", d->pa, base_name, postfix);
+		snprintf(buf, size, "%08lx.%s.%s", d->pa, base_name, postfix);
 		name = buf;
 	}
 
Signature · 1 line
2.20.1
#1 lore raw
Quoted 20 lines · Stephen Kitt

The buffer allocated in ti_adpll_clk_get_name doesn't account for the terminating null. This patch adds the extra byte, and switches to snprintf to avoid overflowing.

---
 drivers/clk/ti/adpll.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/clk/ti/adpll.c +2 -0
index fdfb90058504..27933c4e8a27 100644
@@ -196,12 +196,13 @@ static const char *ti_adpll_clk_get_name(struct ti_adpll_data *d,
 	} else {
 		const char *base_name = "adpll";
 		char *buf;
+		size_t size = 8 + 1 + strlen(base_name) + 1 +
+			      strlen(postfix) + 1;
 

drivers/clk/ti/adpll.c static const char *ti_adpll_clk_get_name(struct ti_adpll_data *d, · line 208

Quoted 10 lines
drivers/clk/ti/adpll.c +2 -3
-		buf = devm_kzalloc(d->dev, 8 + 1 + strlen(base_name) + 1 +
-				    strlen(postfix), GFP_KERNEL);
+		buf = devm_kzalloc(d->dev, size, GFP_KERNEL);
 		if (!buf)
 			return NULL;
-		sprintf(buf, "%08lx.%s.%s", d->pa, base_name, postfix);
+		snprintf(buf, size, "%08lx.%s.%s", d->pa, base_name, postfix);
 		name = buf;
 	}
 

Thanks for catching this. Maybe just use devm_kasprintf() here?

Regards,

Tony

#2 lore raw

Le 27/09/2019 17:23, Tony Lindgren a écrit :

Quoted 22 lines · Stephen Kitt
Quoted 21 lines

The buffer allocated in ti_adpll_clk_get_name doesn't account for the terminating null. This patch adds the extra byte, and switches to snprintf to avoid overflowing.

---
 drivers/clk/ti/adpll.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/clk/ti/adpll.c +2 -0
index fdfb90058504..27933c4e8a27 100644
@@ -196,12 +196,13 @@ static const char *ti_adpll_clk_get_name(struct 
 ti_adpll_data *d,
 	} else {
 		const char *base_name = "adpll";
 		char *buf;
+		size_t size = 8 + 1 + strlen(base_name) + 1 +
+			      strlen(postfix) + 1;
 
Quoted 12 lines

drivers/clk/ti/adpll.c static const char *ti_adpll_clk_get_name(struct ti_adpll_data *d, · line 207

Quoted 10 lines
drivers/clk/ti/adpll.c +2 -3
-		buf = devm_kzalloc(d->dev, 8 + 1 + strlen(base_name) + 1 +
-				    strlen(postfix), GFP_KERNEL);
+		buf = devm_kzalloc(d->dev, size, GFP_KERNEL);
 		if (!buf)
 			return NULL;
-		sprintf(buf, "%08lx.%s.%s", d->pa, base_name, postfix);
+		snprintf(buf, size, "%08lx.%s.%s", d->pa, base_name, postfix);
 		name = buf;
 	}

Thanks for catching this. Maybe just use devm_kasprintf() here?

Ah yes, that would be much better! V2 coming up, thanks for the suggestion.

Regards,

Stephen

#3 lore raw

PATCH v2 1/1 clk/ti/adpll: allocate room for terminating null

The buffer allocated in ti_adpll_clk_get_name doesn't account for the terminating null. This patch switches to ka_sprintf to avoid overflowing.

---
 drivers/clk/ti/adpll.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
drivers/clk/ti/adpll.c +2 -8
index fdfb90058504..021cf9e2b4db 100644
@@ -195,14 +195,8 @@ static const char *ti_adpll_clk_get_name(struct ti_adpll_data *d,
 			return NULL;
 	} else {
 		const char *base_name = "adpll";
-		char *buf;
-
-		buf = devm_kzalloc(d->dev, 8 + 1 + strlen(base_name) + 1 +
-				    strlen(postfix), GFP_KERNEL);
-		if (!buf)
-			return NULL;
-		sprintf(buf, "%08lx.%s.%s", d->pa, base_name, postfix);
-		name = buf;
+		name = devm_kasprintf(d->dev, GFP_KERNEL, "%08lx.%s.%s",
+				      d->pa, base_name, postfix);
 	}
 
 	return name;
Signature · 1 line
2.20.1
#4 lore raw

Le 27/09/2019 20:05, Stephen Kitt a écrit :

Quoted 2 lines

The buffer allocated in ti_adpll_clk_get_name doesn't account for the terminating null. This patch switches to ka_sprintf to avoid

Aargh, devm_kasprintf of course...

Quoted 29 lines

overflowing.

---
 drivers/clk/ti/adpll.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
drivers/clk/ti/adpll.c +2 -8
index fdfb90058504..021cf9e2b4db 100644
@@ -195,14 +195,8 @@ static const char *ti_adpll_clk_get_name(struct
 ti_adpll_data *d,
 			return NULL;
 	} else {
 		const char *base_name = "adpll";
-		char *buf;
-
-		buf = devm_kzalloc(d->dev, 8 + 1 + strlen(base_name) + 1 +
-				    strlen(postfix), GFP_KERNEL);
-		if (!buf)
-			return NULL;
-		sprintf(buf, "%08lx.%s.%s", d->pa, base_name, postfix);
-		name = buf;
+		name = devm_kasprintf(d->dev, GFP_KERNEL, "%08lx.%s.%s",
+				      d->pa, base_name, postfix);
 	}
 

return name;

maintainer
#5 lore raw
In reply to Stephen Kitt

Quoting Stephen Kitt (2019-09-27 11:05:59)

Quoted 14 lines

The buffer allocated in ti_adpll_clk_get_name doesn't account for the terminating null. This patch switches to ka_sprintf to avoid overflowing.

---
 drivers/clk/ti/adpll.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
drivers/clk/ti/adpll.c +0 -0
index fdfb90058504..021cf9e2b4db 100644

drivers/clk/ti/adpll.c static const char *ti_adpll_clk_get_name(struct ti_adpll_data *d, · line 197

Quoted 3 lines
drivers/clk/ti/adpll.c +0 -0
@@ -195,14 +195,8 @@ static const char *ti_adpll_clk_get_name(struct ti_adpll_data *d,
                        return NULL;
        } else {
                const char *base_name = "adpll";

This is used once.

drivers/clk/ti/adpll.c static const char *ti_adpll_clk_get_name(struct ti_adpll_data *d, · line 198

Quoted 9 lines
drivers/clk/ti/adpll.c +1 -8
-               char *buf;
-
-               buf = devm_kzalloc(d->dev, 8 + 1 + strlen(base_name) + 1 +
-                                   strlen(postfix), GFP_KERNEL);
-               if (!buf)
-                       return NULL;
-               sprintf(buf, "%08lx.%s.%s", d->pa, base_name, postfix);
-               name = buf;
+               name = devm_kasprintf(d->dev, GFP_KERNEL, "%08lx.%s.%s",

So why not make this "%08lx.adpll.%s"?

Quoted 2 lines
drivers/clk/ti/adpll.c +1 -0
+                                     d->pa, base_name, postfix);
        }
#6 lore raw
Quoted 31 lines · Stephen Boyd

Quoting Stephen Kitt (2019-09-27 11:05:59)

Quoted 14 lines

The buffer allocated in ti_adpll_clk_get_name doesn't account for the terminating null. This patch switches to ka_sprintf to avoid overflowing.

---
 drivers/clk/ti/adpll.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
drivers/clk/ti/adpll.c +0 -0
index fdfb90058504..021cf9e2b4db 100644

drivers/clk/ti/adpll.c static const char *ti_adpll_clk_get_name(struct · line 197

Quoted 3 lines
drivers/clk/ti/adpll.c +0 -0
@@ -195,14 +195,8 @@ static const char *ti_adpll_clk_get_name(struct
 ti_adpll_data *d, return NULL;
        } else {
                const char *base_name = "adpll";  

This is used once.

drivers/clk/ti/adpll.c static const char *ti_adpll_clk_get_name(struct ti_adpll_data *d, · line 198

Quoted 9 lines
drivers/clk/ti/adpll.c +1 -8
-               char *buf;
-
-               buf = devm_kzalloc(d->dev, 8 + 1 + strlen(base_name) + 1 +
-                                   strlen(postfix), GFP_KERNEL);
-               if (!buf)
-                       return NULL;
-               sprintf(buf, "%08lx.%s.%s", d->pa, base_name, postfix);
-               name = buf;
+               name = devm_kasprintf(d->dev, GFP_KERNEL, "%08lx.%s.%s",  
Quoted 1 line

So why not make this "%08lx.adpll.%s"?

Thanks for the review! I hesitated to do this because I thought the purely formatting string "%08lx.%s.%s" made the resulting code easier to understand than a combined "%08lx.adpll.%s". I’ll follow up with a v3 which merges the "adpll" string into the format string.

Regards,

Stephen

#7 lore raw

PATCH v3 1/1 clk/ti/adpll: allocate room for terminating null

The buffer allocated in ti_adpll_clk_get_name doesn't account for the terminating null. This patch switches to devm_kasprintf to avoid overflowing.

---
Changes since v2:
- Move "adpll" into the format string and drop base_name entirely.

Changes since v1:
- Use devm_kasprintf instead of manually allocating the target buffer.

---
 drivers/clk/ti/adpll.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
drivers/clk/ti/adpll.c +2 -9
index fdfb90058504..bb2f2836dab2 100644
@@ -194,15 +194,8 @@ static const char *ti_adpll_clk_get_name(struct ti_adpll_data *d,
 		if (err)
 			return NULL;
 	} else {
-		const char *base_name = "adpll";
-		char *buf;
-
-		buf = devm_kzalloc(d->dev, 8 + 1 + strlen(base_name) + 1 +
-				    strlen(postfix), GFP_KERNEL);
-		if (!buf)
-			return NULL;
-		sprintf(buf, "%08lx.%s.%s", d->pa, base_name, postfix);
-		name = buf;
+		name = devm_kasprintf(d->dev, GFP_KERNEL, "%08lx.adpll.%s",
+				      d->pa, postfix);
 	}
 
 	return name;
Signature · 1 line
2.20.1
Acked-by on v3 1/1
#8 lore raw
Quoted 4 lines · Stephen Kitt

The buffer allocated in ti_adpll_clk_get_name doesn't account for the terminating null. This patch switches to devm_kasprintf to avoid overflowing.

Quoted 8 lines

---
Changes since v2:
- Move "adpll" into the format string and drop base_name entirely.

Changes since v1:
- Use devm_kasprintf instead of manually allocating the target buffer.

Quoted 29 lines
---
 drivers/clk/ti/adpll.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
drivers/clk/ti/adpll.c +2 -9
index fdfb90058504..bb2f2836dab2 100644
@@ -194,15 +194,8 @@ static const char *ti_adpll_clk_get_name(struct ti_adpll_data *d,
 		if (err)
 			return NULL;
 	} else {
-		const char *base_name = "adpll";
-		char *buf;
-
-		buf = devm_kzalloc(d->dev, 8 + 1 + strlen(base_name) + 1 +
-				    strlen(postfix), GFP_KERNEL);
-		if (!buf)
-			return NULL;
-		sprintf(buf, "%08lx.%s.%s", d->pa, base_name, postfix);
-		name = buf;
+		name = devm_kasprintf(d->dev, GFP_KERNEL, "%08lx.adpll.%s",
+				      d->pa, postfix);
 	}
 
 	return name;
Signature · 1 line
2.20.1
maintainer
#9 lore raw
In reply to Stephen Kitt

Quoting Stephen Kitt (2019-10-19 07:06:34)

Quoted 6 lines

The buffer allocated in ti_adpll_clk_get_name doesn't account for the terminating null. This patch switches to devm_kasprintf to avoid overflowing.

---

Please don't send as replies to existing threads. It screws up my tooling and makes it more manual to apply the patch. I guess I'll have to go fix my scripts to ignore certain emails.

#10 lore raw
Quoted 11 lines · Stephen Boyd

Quoting Stephen Kitt (2019-10-19 07:06:34)

Quoted 6 lines

The buffer allocated in ti_adpll_clk_get_name doesn't account for the terminating null. This patch switches to devm_kasprintf to avoid overflowing.

---

Please don't send as replies to existing threads. It screws up my tooling and makes it more manual to apply the patch. I guess I'll have to go fix my scripts to ignore certain emails.

My bad, sorry about that, I misread the In-Reply-To section of submitting-patches :-(.

Regards,

Stephen