Re: realloc(): invalid next size



Deephay <tudoxxx@xxxxxxxxx> wrote:

Richard Bos wrote:
Deephay <tudoxxx@xxxxxxxxx> wrote:

p = min(strcspn(&data[m], " "),
strcspn(&data[m], "\t"),
strcspn(&data[m], "\r"),
strcspn(&data[m], "\n"));

Why not simply use strcspn(&data[m], " \t\r\n")?
I have to get the index where the first "space" locates.

That one call does the same thing as those four calls plus a properly
written min() function.

key = realloc(key, p);
strncpy(key, &data[m], p);
key[p] = '\0';
trans(key, p);
m = m + p - 1;

and the "key" is already malloced before the loop:

char *key = malloc(1);

Hard to say what the real problem is without any object or function
definitions, but two questions spring to mind:
- does this occur immediately, or after a few iterations;
- what actually is the value of p (and if this code isn't complete, of
key) at the point where it produces the error?
the error occurred after a few iterations, the first realloc will always
success.
the p is always larger than 0, I have tested. Actually it will be a
quite normal value where the error produced, say, 7, 8, or whatever...

What I think now is that it might be a bug of glibc...

That is the last thing I would consider.

I searched with google and found some unsolved problem like the one I have.

You can find a great many confused bug-writers on the 'web, yes, many of
them blaming their tools for their own mistakes.

What I did now is, declare a key[50] static, this works fine.

It probably works around a bug - using strncpy() without thorough
understanding is asking for one, btw - and leaves a time bomb in your
code. What happens if you encounter a token larger than 50 characters?
Worse, what if you encounter one of _precisely_ 50 characters?

I still want to make sure what the problem is, though.

Without more code which demonstrates the problem precisely, it is
impossible to be certain. If the above is your exact code, you have
several off-by-one errors; then again, if the above is your exact code,
it doesn't compile, because there is a lot missing.

If you want more help, what you should do now is the following:
- make a copy of your existing code;
- whittle it down to the smallest _compilable_ program which still
exhibits the same problem;
- post _exactly_ that code. Paste, do not retype it into your post,
otherwise you'll make typos.

Richard
.



Relevant Pages

  • Re: realloc(): invalid next size
    ... Richard Bos wrote: ... What happens if you encounter a token larger than 50 characters? ... If the above is your exact code, ... it doesn't compile, because there is a lot missing. ...
    (comp.lang.c)
  • Re: help interpreting Gleemax posts
    ... I find that to be a poor metric, because party composition makes a big ... characters come in and out, including a super-righteous Monk, and a highly ... beating the encounter, the odds of a character death, the odds of a TPK) ... I guess my point was that using the corresponding metrics from the 2 ...
    (rec.games.frp.dnd)
  • Bug in mbstowcs() in DJGPPs C library
    ... the end of the buffer and write a NUL. ... If I compile this file using DJGPP, ... converts the input string into wide characters, ... n of them into the output buffer, and _if_ it sees a NUL byte in the ...
    (comp.os.msdos.djgpp)
  • Re: The 4e rules as a wargame
    ... "Monsters that are ... Since hazards are equivalent to monsters for encounter ... would almost certainly kill a wizard of the appropriate level. ... and you compare them to the characters' level to judge whether ...
    (rec.games.frp.dnd)
  • Re: a few more questions (3.5E)
    ... I thought the encounter was 50/50, and I think they only lost a couple ... of characters because they made a few mistakes. ... Anyway, the DMG says first of all find the average party level, but ... Clearly 8x3rd level characters is a more powerful force than 4x3rd ...
    (rec.games.frp.dnd)