[En-Nut-Discussion] Memory leak in UxmlParseStream?

Bernd Walter enut at cicely.de
Mon Dec 14 03:23:49 CET 2009


While hunting for lost heap I'd noticed the following code (ethernut-4.8.2):

                /* Parse the attributes. */
                for (;;) {
                    if ((tp = UxmlParseTag(tp, tkn, MAX_UXMLTKN_SIZE)) == NULL || *tkn == '>') {
                        /* End of this tag or error. */
                        break;
                    }
                    if (isalpha(*tkn) && UxmlFilterMatch(tkn, f_attr)) {
                        size_t len = strlen(tkn) + 1;
                        char *name = malloc(len);

                        if (name) {
                            memcpy(name, tkn, len);
                            if ((tp = UxmlParseTag(tp, tkn, MAX_UXMLTKN_SIZE)) == NULL || *tkn != '=') {
                                break;
                            }
                            if ((tp = UxmlParseTag(tp, tkn, MAX_UXMLTKN_SIZE)) == NULL || *tkn == '>') {
                                break;
                            }
                            UxmlNodeAddAttrib(node, name, tkn);
                            free(name);
                        }
                    }
                }

Inside the for loop the variable name gets memory, but before free'ing
it there is a chance of breaking out of it.
This is an error condition, so likely not the reason for my lost memory,
but still doesn't look Ok.

-- 
B.Walter <bernd at bwct.de> http://www.bwct.de
Modbus/TCP Ethernet I/O Baugruppen, ARM basierte FreeBSD Rechner uvm.



More information about the En-Nut-Discussion mailing list