]> git.proxmox.com Git - mirror_corosync.git/commitdiff
Fix to display strings safely in debug messages.
authorAngus Salkeld <asalkeld@redhat.com>
Wed, 20 Aug 2008 01:00:44 +0000 (01:00 +0000)
committerAngus Salkeld <asalkeld@redhat.com>
Wed, 20 Aug 2008 01:00:44 +0000 (01:00 +0000)
Display strings safely, even if they are invalid, e.g. data in a received
message was corrupted.

Author: Tim Beale <tim.beale@alliedtelesis.co.nz>

git-svn-id: http://svn.fedorahosted.org/svn/corosync/trunk@1657 fd59a12c-fef9-0310-b244-a6a79926bd2f

exec/util.c

index 593cdba9583517d2f0766d668d84495656bd1b9d..7939a4a34431d7796eab75003d2f84ebfffc949a 100644 (file)
@@ -86,21 +86,18 @@ void _corosync_exit_error (
        exit (EXIT_FAILURE);
 }
 
+#define min(a,b) ((a) < (b) ? (a) : (b))
+
 char *getSaNameT (SaNameT *name)
 {
-#if 0
-       static char ret_name[300];
-
-       memset (ret_name, 0, sizeof (ret_name));
-       if (name->length > 299) {
-               memcpy (ret_name, name->value, 299);
-       } else {
+       static char ret_name[SA_MAX_NAME_LENGTH];
 
-               memcpy (ret_name, name->value, name->length);
+       /* if string is corrupt (non-terminated), ensure it's displayed safely */
+       if (name->length >= SA_MAX_NAME_LENGTH || name->value[name->length] != '\0') {
+               memset (ret_name, 0, sizeof (ret_name));
+               memcpy (ret_name, name->value, min(name->length, SA_MAX_NAME_LENGTH -1));
+               return (ret_name);
        }
-       return (ret_name);
-#endif
-// TODO
        return ((char *)name->value);
 }