[v3] compel: std_vprint_num returns a null-terminated string

Submitted by Joel Nider on May 8, 2018, 7:33 a.m.

Details

Message ID 1525764807-111981-1-git-send-email-joeln@il.ibm.com
State Accepted
Series "compel: std_vprint_num returns a null-terminated string"
Commit 0c41a4208b5e6fca7572fac50bd43e3796a4c8dd
Headers show

Commit Message

Joel Nider May 8, 2018, 7:33 a.m.
This function is an analogue to vsprintf(), and is used in very much the
same way. The caller expects the modified string pointer to be pointing to
a null-terminated string.

Signed-off-by: Joel Nider <joeln@il.ibm.com>
---
 compel/plugins/std/log.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Patch hide | download patch | download mbox

diff --git a/compel/plugins/std/log.c b/compel/plugins/std/log.c
index 2d622d6..81611d1 100644
--- a/compel/plugins/std/log.c
+++ b/compel/plugins/std/log.c
@@ -143,6 +143,7 @@  int std_vprint_num(char *buf, int blen, int num, char **ps)
 	char *s;
 
 	s = &buf[blen - 1];
+	*s-- = 0; /* make sure the returned string is NULL terminated */
 
 	if (num < 0) {
 		neg = 1;
@@ -166,15 +167,14 @@  int std_vprint_num(char *buf, int blen, int num, char **ps)
 done:
 	s++;
 	*ps = s;
-	return blen - (s - buf);
+	return blen - (s - buf) - 1;
 }
 
 static void print_num(int num, struct simple_buf *b)
 {
 	char buf[12], *s;
 
-	buf[11] = '\0';
-	std_vprint_num(buf, sizeof(buf) - 1, num, &s);
+	std_vprint_num(buf, sizeof(buf), num, &s);
 	print_string(s, b);
 }
 

Comments

Andrey Vagin May 8, 2018, 8:35 p.m.
Applied, thanks!

On Tue, May 08, 2018 at 10:33:27AM +0300, Joel Nider wrote:
> This function is an analogue to vsprintf(), and is used in very much the
> same way. The caller expects the modified string pointer to be pointing to
> a null-terminated string.
> 
> Signed-off-by: Joel Nider <joeln@il.ibm.com>
> ---
>  compel/plugins/std/log.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/compel/plugins/std/log.c b/compel/plugins/std/log.c
> index 2d622d6..81611d1 100644
> --- a/compel/plugins/std/log.c
> +++ b/compel/plugins/std/log.c
> @@ -143,6 +143,7 @@ int std_vprint_num(char *buf, int blen, int num, char **ps)
>  	char *s;
>  
>  	s = &buf[blen - 1];
> +	*s-- = 0; /* make sure the returned string is NULL terminated */
>  
>  	if (num < 0) {
>  		neg = 1;
> @@ -166,15 +167,14 @@ int std_vprint_num(char *buf, int blen, int num, char **ps)
>  done:
>  	s++;
>  	*ps = s;
> -	return blen - (s - buf);
> +	return blen - (s - buf) - 1;
>  }
>  
>  static void print_num(int num, struct simple_buf *b)
>  {
>  	char buf[12], *s;
>  
> -	buf[11] = '\0';
> -	std_vprint_num(buf, sizeof(buf) - 1, num, &s);
> +	std_vprint_num(buf, sizeof(buf), num, &s);
>  	print_string(s, b);
>  }
>  
> -- 
> 2.7.4
>