OSSP CVS Repository

ossp - Check-in [4244]
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Patchset]  [Tagging/Branching

Check-in Number: 4244
Date: 2001-Jul-24 17:44:18 (local)
2001-Jul-24 15:44:18 (UTC)
User:simons
Branch:
Comment: Comparing the new structure instance with the old one by memcmp() was bound to fail because on some architectures padding is inserted -- and those padding bytes didn't match even though the structure's values were identical. Thus we do now compare element by element.

Furthermore, the encoding and decoding engines way of predicting the correct buffer size ("sizeof(struct mystruct)") failed also because it didn't take padding into account. Fixed that also.

Tickets:
Inspections:
Files:
ossp-pkg/xds/regression-tests/xds-mystruct.c      1.4 -> 1.5     30 inserted, 22 deleted

ossp-pkg/xds/regression-tests/xds-mystruct.c 1.4 -> 1.5

--- xds-mystruct.c       2001/07/23 15:33:43     1.4
+++ xds-mystruct.c       2001/07/24 15:44:18     1.5
@@ -47,17 +47,12 @@
     assert(buffer_size != 0);
     assert(args != NULL);
 
-    if (buffer_size >= sizeof(struct mystruct))
-        {
-        ms = va_arg(*args, struct mystruct*);
-        rc = xds_encode(xds, "int32 int64 uint32", ms->small, ms->big, ms->positive);
-        if (rc != XDS_OK)
-            return rc;
-        }
+    ms = va_arg(*args, struct mystruct*);
+    rc = xds_encode(xds, "int32 int64 uint32", ms->small, ms->big, ms->positive);
+    if (rc == XDS_OK)
+        return 0;
     else
-        return sizeof(struct mystruct);
-
-    return 0;
+        return rc;
     }
 
 static int decode_mystruct_engine(xds_t* xds, void* engine_context, void* buffer, size_t buffer_size, va_list* args)
@@ -70,17 +65,12 @@
     assert(buffer_size != 0);
     assert(args != NULL);
 
-    if (buffer_size >= sizeof(struct mystruct))
-        {
-        ms = va_arg(*args, struct mystruct*);
-        rc = xds_decode(xds, "int32 int64 uint32", &(ms->small), &(ms->big), &(ms->positive));
-        if (rc != XDS_OK)
-            return rc;
-        }
+    ms = va_arg(*args, struct mystruct*);
+    rc = xds_decode(xds, "int32 int64 uint32", &(ms->small), &(ms->big), &(ms->positive));
+    if (rc == XDS_OK)
+        return 0;
     else
-        return XDS_ERR_UNDERFLOW;
-
-    return 0;
+        return rc;
     }
 
 int main()
@@ -111,17 +101,35 @@
         printf("Failed to register my encoding engines.\n");
         return 1;
         }
+    buffer_size = 4;
+    buffer = malloc(buffer_size);
+    if (buffer == NULL)
+        {
+        printf("Failed to allocate memory for buffer.\n");
+        return 1;
+        }
+    if (xds_setbuffer(xds, XDS_GIFT, buffer, buffer_size) != XDS_OK)
+        {
+        printf("xds_setbuffer() failed!\n");
+        return 1;
+        }
     if (xds_encode(xds, "mystruct", &ms) != XDS_OK)
         {
         printf("xds_encode() failed!\n");
         return 1;
         }
+    if (xds->buffer_capacity <= buffer_size)
+        {
+        printf("Buffer should have been enlarged after xds_encode()!\n");
+        return 1;
+        }
     if (xds_getbuffer(xds, XDS_GIFT, (void**)&buffer, &buffer_size) != XDS_OK)
         {
-        printf("xds_setbuffer() failed!\n");
+        printf("xds_getbuffer() failed!\n");
         return 1;
         }
     xds_destroy(xds);
+    printf("The encoded representation is %u bytes long.\n", buffer_size);
 
     /* Now create a decoding context and decode the whole thing again. */
 
@@ -153,7 +161,7 @@
 
     /* Both structures must be identical. */
 
-    if (memcmp(&ms, &new_ms, sizeof(struct mystruct)) != 0)
+    if (ms.small != new_ms.small || ms.big != new_ms.big || ms.positive != new_ms.positive)
         {
         printf("Decoded data does not match the original!\n");
         return 1;

CVSTrac 2.0.1