[ale] My eyes are tired

Christopher Fowler cfowler at outpostsentinel.com
Tue Feb 21 23:46:06 EST 2006


I think my problem may be deeper inside an XML parser.  I can crunch
these functions using much memory and never have death.  Its only when I
enter the parser is when things go crazy. I did not write the parser so
I may be trusting it too much.  I thought they may be something simple
here I've not thought of.

I'm trying to do something interesting in which I store a device's
config in XML format instead of the standard binary one.  It will still
be compressed and encrypted but the data will be XML.  The main reason
is that if I add features down the road I can simply add elements to the
XML format and the new format is backwards compat with the old.  In the
past anytime we've changed the structure of a configuration that was
binary.  Basically struct config { ... };  We would have to change
the config version.  Changing the config version could result in a
device going back to factory defaults.  The whole XML idea is a way to
get around that without having to code data conversion stuff each time
we decide to add something.

Also this device will be pushed a config from the Applcation server.  On
the device the only commands available are those for configuring
network.  You need those so the App server can communicate.  From then
on you use the App server.  This device is just a remote node of that
server.  

On Tue, 2006-02-21 at 21:18 -0700, Joe Knapka wrote:
> Christopher Fowler wrote:
> 
> >I'm bringing over some code from another device to a new device.  It is
> >basically a mini string library that helps us deal with strings in C.  I
> >think it is all working right but I'm in segfault hell with other code
> >that uses it and my suspicions lie on this API.  I need another set of
> >eyes to take a look and see if they see it.  I'm tired of looking at it.
> >  
> >
> What jumps out at first glance is that you should probably start by 
> checking the
> return values of all malloc() and realloc() calls.
> 
> How much are you paying me for this?
> 
> -- JK
> 
> >rn_string.c
> >-----------------------------------------------------------------------
> >#include <fcntl.h>
> >#include <stdio.h>
> >#include <stdlib.h>
> >#include <string.h>
> >#include <stdarg.h>
> >#include <unistd.h>
> >
> >#include <sys/types.h>
> >
> >char *
> >rn_string(const char *fmt, ...) {
> >        va_list p;
> >        char buffer[2048];
> >        int r_len = 0;
> >        char *ptr = NULL;
> >
> >        va_start(p, fmt);
> >        vsnprintf(buffer , sizeof(buffer), fmt, p);
> >        va_end(p);
> >
> >        r_len = strlen(buffer);
> >
> >        ptr = malloc(r_len + 1);
> >        strncpy(ptr, buffer, r_len);
> >
> >        return ptr;
> >
> >}
> >
> >void
> >rn_string_free(char *s) {
> >        if(s) {
> >                free(s);
> >        }
> >
> >        return;
> >}
> >
> >int
> >rn_string_length(char *s) {
> >        if(s) {
> >                return strlen(s);
> >        }
> >        return 0;
> >}
> >char *
> >rn_string_append(char *s, const char *fmt, ...) {
> >        va_list p;
> >        char buffer[2048];
> >        int r_len = 0;
> >        int s_len = 0;
> >
> >        if(s == NULL) {
> >                return 0;
> >        }
> >
> >        va_start(p, fmt);
> >        vsnprintf(buffer , sizeof(buffer), fmt, p);
> >        va_end(p);
> >
> >        r_len = strlen(buffer);
> >        s_len = strlen(s);
> >
> >        s = realloc(s, (s_len + r_len + 1));
> >        strncpy((s + s_len), buffer, (r_len));
> >
> >        return s;
> >
> >}
> >
> >
> >int
> >rn_string_to_file(char *s, const char *dest, mode_t mode) {
> >        int fd;
> >
> >        if((fd = open(dest, O_WRONLY | O_CREAT | O_TRUNC, mode)) == -1)
> >{
> >                return -1;
> >        }
> >
> >        write(fd, s, rn_string_length(s));
> >        close(fd);
> >
> >        return 0;
> >
> >}
> >char *
> >rn_string_from_file(const char *src) {
> >        FILE *fp;
> >        char *str = NULL;
> >        char buffer[128];
> >        int tot = 0;
> >
> >
> >        if((fp = fopen(src, "r")) == NULL) {
> >                return NULL;
> >        }
> >
> >
> >        while(fgets(buffer, sizeof(buffer), fp) != NULL) {
> >                if(str == NULL) {
> >                        str = realloc(str, (strlen(buffer)+1));
> >                        memcpy(str, buffer, (strlen(buffer)));
> >                } else {
> >                        str = realloc(str, (tot + (strlen(buffer)+1)));
> >                        memcpy((str + tot), buffer, (strlen(buffer)));
> >                }
> >
> >                tot += strlen(buffer);
> >        }
> >
> >        fclose(fp);
> >
> >        return str;
> >
> >}
> >
> >char *
> >rn_string_dup(const char *src) {
> >        if(src == NULL) {
> >                return NULL;
> >        }
> >
> >        return strdup(src);
> >}
> >-----------------------------------------------------------------------
> >
> >
> >_______________________________________________
> >Ale mailing list
> >Ale at ale.org
> >http://www.ale.org/mailman/listinfo/ale
> >
> >
> >  
> >
> 
> 
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale




More information about the Ale mailing list