[Qemu-devel] [PATCHv3 11/20] ui/vnc: simplify and avoid strncpy

2012-10-04 Thread Jim Meyering
From: Jim Meyering meyer...@redhat.com

Don't bother with strncpy.  There's no need for its zero-fill.
Use g_strndup in place of g_malloc+strncpy+NUL-terminate.

Signed-off-by: Jim Meyering meyer...@redhat.com
---
 ui/vnc-auth-sasl.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/ui/vnc-auth-sasl.c b/ui/vnc-auth-sasl.c
index 8fba770..bfdcb46 100644
--- a/ui/vnc-auth-sasl.c
+++ b/ui/vnc-auth-sasl.c
@@ -432,9 +432,7 @@ static int protocol_client_auth_sasl_start_len(VncState 
*vs, uint8_t *data, size

 static int protocol_client_auth_sasl_mechname(VncState *vs, uint8_t *data, 
size_t len)
 {
-char *mechname = g_malloc(len + 1);
-strncpy(mechname, (char*)data, len);
-mechname[len] = '\0';
+char *mechname = g_strndup((const char *) data, len);
 VNC_DEBUG(Got client mechname '%s' check against '%s'\n,
   mechname, vs-sasl.mechlist);

-- 
1.8.0.rc0.18.gf84667d




Re: [Qemu-devel] [PATCHv3 11/20] ui/vnc: simplify and avoid strncpy

2012-10-04 Thread Peter Maydell
On 4 October 2012 12:09, Jim Meyering j...@meyering.net wrote:
 From: Jim Meyering meyer...@redhat.com

 Don't bother with strncpy.  There's no need for its zero-fill.
 Use g_strndup in place of g_malloc+strncpy+NUL-terminate.

 Signed-off-by: Jim Meyering meyer...@redhat.com
 ---
  ui/vnc-auth-sasl.c | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)

 diff --git a/ui/vnc-auth-sasl.c b/ui/vnc-auth-sasl.c
 index 8fba770..bfdcb46 100644
 --- a/ui/vnc-auth-sasl.c
 +++ b/ui/vnc-auth-sasl.c
 @@ -432,9 +432,7 @@ static int protocol_client_auth_sasl_start_len(VncState 
 *vs, uint8_t *data, size

  static int protocol_client_auth_sasl_mechname(VncState *vs, uint8_t *data, 
 size_t len)
  {
 -char *mechname = g_malloc(len + 1);
 -strncpy(mechname, (char*)data, len);
 -mechname[len] = '\0';
 +char *mechname = g_strndup((const char *) data, len);
  VNC_DEBUG(Got client mechname '%s' check against '%s'\n,
mechname, vs-sasl.mechlist);


(Does the compiler really insist on that cast?)

Reviewed-by: Peter Maydell peter.mayd...@linaro.org

-- PMM



Re: [Qemu-devel] [PATCHv3 11/20] ui/vnc: simplify and avoid strncpy

2012-10-04 Thread Jim Meyering
Peter Maydell wrote:

 On 4 October 2012 12:09, Jim Meyering j...@meyering.net wrote:
 From: Jim Meyering meyer...@redhat.com

 Don't bother with strncpy.  There's no need for its zero-fill.
 Use g_strndup in place of g_malloc+strncpy+NUL-terminate.

 Signed-off-by: Jim Meyering meyer...@redhat.com
 ---
  ui/vnc-auth-sasl.c | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)

 diff --git a/ui/vnc-auth-sasl.c b/ui/vnc-auth-sasl.c
 index 8fba770..bfdcb46 100644
 --- a/ui/vnc-auth-sasl.c
 +++ b/ui/vnc-auth-sasl.c
 @@ -432,9 +432,7 @@ static int protocol_client_auth_sasl_start_len(VncState 
 *vs, uint8_t *data, size

  static int protocol_client_auth_sasl_mechname(VncState *vs, uint8_t *data, 
 size_t len)
  {
 -char *mechname = g_malloc(len + 1);
 -strncpy(mechname, (char*)data, len);
 -mechname[len] = '\0';
 +char *mechname = g_strndup((const char *) data, len);
  VNC_DEBUG(Got client mechname '%s' check against '%s'\n,
mechname, vs-sasl.mechlist);


 (Does the compiler really insist on that cast?)

Yes.  Without it, I get a warning/failure when char is signed.
See below.

 Reviewed-by: Peter Maydell peter.mayd...@linaro.org

Thanks for the review.

ui/vnc-auth-sasl.c: In function 'protocol_client_auth_sasl_mechname':
ui/vnc-auth-sasl.c:435:5: error: pointer targets in passing argument 1 of 
'g_strndup' differ in signedness [-Werror=pointer-sign]
 char *mechname = g_strndup(data, len);
 ^
In file included from /usr/include/glib-2.0/glib.h:81:0,
 from ./qemu-common.h:41,
 from ui/vnc.h:30,
 from ui/vnc-auth-sasl.c:25:
/usr/include/glib-2.0/glib/gstrfuncs.h:192:8: note: expected 'const gchar 
*' but argument is of type 'uint8_t *'
 gchar*   g_strndup(const gchar *str,
^
cc1: all warnings being treated as errors
make: *** [ui/vnc-auth-sasl.o] Error 1



Re: [Qemu-devel] [PATCHv3 11/20] ui/vnc: simplify and avoid strncpy

2012-10-04 Thread Peter Maydell
On 4 October 2012 19:56, Jim Meyering j...@meyering.net wrote:
 Peter Maydell wrote:
 (Does the compiler really insist on that cast?)

 Yes.  Without it, I get a warning/failure when char is signed.
 See below.

Ah yes, signed vs unsigned char. Thanks for the clarification.

-- PMM