]> andersk Git - splint.git/commitdiff
Added web version of the realloc document.
authordrl7x <drl7x>
Mon, 24 Mar 2003 07:53:23 +0000 (07:53 +0000)
committerdrl7x <drl7x>
Mon, 24 Mar 2003 07:53:23 +0000 (07:53 +0000)
(Created with save as html and then fixed up with Tidy.)

doc/html/realloc.htm [new file with mode: 0755]

diff --git a/doc/html/realloc.htm b/doc/html/realloc.htm
new file mode 100755 (executable)
index 0000000..e3691b1
--- /dev/null
@@ -0,0 +1,109 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">\r
+\r
+<html>\r
+<head>\r
+    <title>Using Wrapper Functions</title>\r
+</head>\r
+\r
+<body>\r
+    <div class="Section1">\r
+        <h1>Using Wrapper Functions</h1>\r
+\r
+        <p>The propose of this document is to provide an example of\r
+        how to use wrapper functions to create programs that are\r
+        more robust and that Splint can check more\r
+        effectively.&nbsp; The C standard library function realloc\r
+        has complex semantics and is easy to use incorrectly.&nbsp;\r
+        Still, it is a popular function, and we are frequently\r
+        asked how to use Splint to check code that uses\r
+        realloc.&nbsp; We hope to answer these questions in this\r
+        section.&nbsp; Additionally, we hope this example will\r
+        provide insight into other ways to work around some of the\r
+        limitations of Splint&rsquo;s checking, and serve as a\r
+        template for users wishing to create their own wrapper\r
+        functions.</p>\r
+\r
+        <p>Splint cannot currently describe general functions where\r
+        some of the attributes are dependent on one or more of the\r
+        possible return values.&nbsp; Often, functions are defined\r
+        to modify their outputs, and perhaps allocate storage, but\r
+        can also return an error indication, in which case none of\r
+        the modifications or allocations will take place.</p>\r
+\r
+        <p>One very common example of this is the C standard\r
+        library function realloc( void *pointer, size_t\r
+        size).&nbsp; When realloc succeeds, the pointer passed to\r
+        it is no longer valid.&nbsp; The returned pointer points to\r
+        available storage with the specified size, and the values\r
+        are copied from the leading portion of the original storage\r
+        indicated by the pointer passed to the function.&nbsp; When\r
+        realloc returns a NULL pointer, and more than zero bytes\r
+        were supposed to be allocated, no new storage has been\r
+        allocated.&nbsp; The original pointer passed in is not\r
+        deallocated and its contents are still accessible.&nbsp;\r
+        (Under ANSI C '89 and later, malloc and realloc may return\r
+        NULL if asked for zero bytes.&nbsp; In this case, realloc\r
+        would release the old storage.)</p>\r
+\r
+        <p>If you use realloc, -usereleased can be used to suppress\r
+        warnings.&nbsp; However, this may result in legitimate\r
+        warnings for other errors being suppressed as well.&nbsp;\r
+        If you don't mind crafting your code to work around\r
+        Splint's quirks, you can isolate this difficult-to-describe\r
+        behavior to a single function that's easy to verify by\r
+        inspection, and use that function elsewhere.&nbsp; For\r
+        example:</p>\r
+\r
+        &nbsp;&nbsp;&nbsp; /*@-usereleased@*/<br>\r
+\r
+        &nbsp;&nbsp;&nbsp; static /*@null@*/ void *<br>\r
+\r
+        &nbsp;&nbsp;&nbsp; grow_or_free (/*@only@*/ void *ptr,\r
+        size_t newsize)<br>\r
+\r
+        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\r
+        /*@*/<br>\r
+\r
+        &nbsp;&nbsp;&nbsp; {<br>\r
+\r
+        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void\r
+        *newptr;<br>\r
+\r
+        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newptr =\r
+        realloc (ptr, newsize);<br>\r
+\r
+        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (newptr ==\r
+        NULL &amp;&amp; newsize != 0) {<br>\r
+\r
+        \r
+        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\r
+        /* Splint would complain,<br>\r
+\r
+        \r
+        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\r
+        but this is correct.&nbsp; */<br>\r
+\r
+        \r
+        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\r
+        free (ptr);<br>\r
+\r
+        \r
+        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\r
+        return NULL;<br>\r
+\r
+        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>\r
+\r
+        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return\r
+        newptr;<br>\r
+\r
+        &nbsp;&nbsp;&nbsp; }<br>\r
+\r
+        &nbsp;&nbsp;&nbsp; /*@=usereleased@*/<br>\r
+\r
+        <p>It would also be possible to use a function that always\r
+        returns a valid pointer, and separately indicates whether\r
+        it managed to change the size to that desired by the\r
+        caller.</p>\r
+    </div>\r
+</body>\r
+</html>\r
This page took 0.075731 seconds and 5 git commands to generate.