Hi,

I just stumbled over this function in source/interpret.c:

/* Allocate a new string buffer of length chars */
char *AllocString(int length)
{
    char *mem;
    
    mem = XtMalloc(length + sizeof(char *) + 1);
    *((char **)mem) = AllocatedStrings;
    AllocatedStrings = mem;
#ifdef TRACK_GARBAGE_LEAKS
    ++numAllocatedStrings;
#endif
    return mem + sizeof(char *) + 1;
}

I have a number of questions about and/or issues with this functions.

- XtMalloc is never tested to be successful. It does not return NULL
  on errors, but calls XtErrorMsg(), which simply exits NEdit. We
  have more than 500 calls to XtMalloc; should we cover this by an
  error handler?
- It mallocs (length + sizeof(char *) + 1), which looks like a typo.
  Should this really be (length * sizeof(char *) + 1)?
- Also, it returns (mem + sizeof(char *) + 1), which is (char* +
  size_t + int). What is this supposed to bring about?
- Lastly, it promises in the comment:

    Length does not include the terminating null ** character, so to
    allocate space for a string of strlen == n, you must ** use
    AllocString(n+1).

  So what is the + 1 about?


Thorsten                               Tegan and Sara: I know I know I know
-- 
To announce that there must be no criticism of the president, or that we are
to stand by the president, right or wrong, is not only unpatriotic and servile,
but is morally treasonable to the American public.
    - Theodore Roosevelt

Attachment: pgpHDEvwMmiet.pgp
Description: PGP signature

-- 
NEdit Develop mailing list - [email protected]
http://www.nedit.org/mailman/listinfo/develop

Reply via email to