[hipl-commit] [tiny] Rev 3699: Improved string handling in lmod_register_packet_type.

  • From: Tim Just <tim.just@xxxxxxxxxxxxxx>
  • To: hipl-commit@xxxxxxxxxxxxx
  • Date: Fri, 19 Mar 2010 10:33:42 +0200

Committer: Tim Just <tim.just@xxxxxxxxxxxxxx>
Date: 19/03/2010 at 10:33:42
Revision: 3699
Revision-id: tim.just@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Branch nick: tiny

Log:
  Improved string handling in lmod_register_packet_type.
  
  The identifier for the packet type should be copied, because otherwise it
  could be overwritten.
  
  Changed the packet type numer to uint16_t. This should be adequate for
  packet types.

Modified:
  M  lib/modularization/lmod.c

=== modified file 'lib/modularization/lmod.c'
--- lib/modularization/lmod.c   2010-03-18 17:06:56 +0000
+++ lib/modularization/lmod.c   2010-03-19 08:30:24 +0000
@@ -22,8 +22,8 @@
 };
 
 struct packet_type {
-    uint32_t    num;
-    const char *identifier;
+    uint16_t    num;
+    char *identifier;
 };
 
 
@@ -423,9 +423,10 @@
 int lmod_register_packet_type(const uint16_t packet_type,
                               const char *identifier)
 {
-    int                 index     = 0;
-    hip_ll_node_t      *iter      = NULL;
-    struct packet_type *new_entry = NULL;
+    int                 index          = 0;
+    size_t              identifier_len = 0;
+    hip_ll_node_t      *iter           = NULL;
+    struct packet_type *new_entry      = NULL;
 
     if (!identifier || (lmod_packet_type_exists(packet_type) != -1)) {
         return -1;
@@ -436,7 +437,12 @@
     }
 
     new_entry->num = packet_type;
-    new_entry->identifier = identifier;
+
+    identifier_len = strlen(identifier);
+    if (!(new_entry->identifier = malloc(identifier_len))) {
+        return -1;
+    }
+    strncpy(new_entry->identifier, identifier, identifier_len);
 
     while ((iter = hip_ll_iterate(&packet_types, iter))) {
         if (packet_type == ((struct packet_type *) iter->ptr)->num) {

Other related posts:

  • » [hipl-commit] [tiny] Rev 3699: Improved string handling in lmod_register_packet_type. - Tim Just