Subject: | better memory handling |
Hi. CSS::Minifier::XS 0.03 may sometimes cause segfaults under my
environment (Win2K+ActivePerl 5.8.7+gcc3.4.2; actually "always" for the
condition written in t/99-benchmark.t). Using Perl's alternative New(z)/
Safefree macros (instead of built-in malloc/free) seems to remove this
problem constantly. A patch attached. Thanks.
Kenichi Ishigaki
Subject: | CSS-Minifier-XS-0.03.patch |
diff -ur CSS-Minifier-XS-0.03/XS.xs CSS-Minifier-XS-0.03-patched/XS.xs
--- CSS-Minifier-XS-0.03/XS.xs Thu Jul 17 15:21:23 2008
+++ CSS-Minifier-XS-0.03-patched/XS.xs Sun Oct 12 10:29:46 2008
@@ -168,7 +168,8 @@
*/
/* allocates a new node */
Node* CssAllocNode() {
- Node* node = malloc(sizeof(Node));
+ Node* node;
+ New(1234, node, 1, Node);
node->prev = NULL;
node->next = NULL;
node->contents = NULL;
@@ -181,8 +182,8 @@
/* frees the memory used by a node */
void CssFreeNode(Node* node) {
if (node->contents)
- free(node->contents);
- free(node);
+ Safefree(node->contents);
+ Safefree(node);
}
void CssFreeNodeList(Node* head) {
while (head) {
@@ -195,7 +196,7 @@
/* clears the contents of a node */
void CssClearNodeContents(Node* node) {
if (node->contents)
- free(node->contents);
+ Safefree(node->contents);
node->contents = NULL;
node->length = 0;
}
@@ -205,8 +206,7 @@
CssClearNodeContents(node);
node->length = len;
/* allocate string, fill with NULLs, and copy */
- node->contents = malloc( sizeof(char) * (len+1) );
- memset( node->contents, 0, len );
+ Newz( 1234, node->contents, len+1, char );
strncpy( node->contents, string, len );
}
@@ -538,7 +538,8 @@
/* allocate the result buffer to the same size as the original CSS; in
* a worst case scenario that's how much memory we'll need for it.
*/
- ptr = results = malloc( sizeof(char) * (strlen(string)+1) );
+ New( 1234, results, (strlen(string)+1), char );
+ ptr = results;
/* copy node contents into result buffer */
curr = head;
while (curr) {
@@ -572,7 +573,7 @@
/* hand back the minified CSS (if we had any) */
if (buffer != NULL) {
RETVAL = newSVpv(buffer, 0);
- free( buffer );
+ Safefree( buffer );
}
OUTPUT:
RETVAL