How to stop "Break called from code".

Started by
33 comments, last by MichaelT 19 years, 8 months ago
Quote:
use sizeof() instead of strlen() for better results


It's late so maybe I'm missing something but won't sizeof(dest) usually be the size of a pointer(4 on x86) since dest will be of type char*? That means the macro will not work right if you use sizeof instead of strlen.
Advertisement
Quote:Original post by Nurgle
strncpy(temp->filename, filename, strlen(temp->filename));

This completely destroys the possibility of a buffer overflow. In your code above, it's redundant, but still a good habit to get into.

If you find yourself not doing the right thing out of habit, you could always hack together a quick macro...

#define strcpy(dest, src) strncpy(dest, src, strlen(dest));

This really isn't a good idea. If you don't know what the destination buffer currently contains, you can't expect strlen to return its size. Nothing guarantees the buffer is null-terminated (and you can't do destBuffer[destSize-1] = '\0', since there might be a '\0' somewhere else in the buffer, in which case strlen will return a value smaller than the buffer's length).

What you can do:
1. Use std::string.
2. Dynamically allocate the destination buffer (strlen(srcBuffer)+1), and then copy (both strcpy and strncpy will work fine in this case).
3. Use a fixed-size destination buffer, and use strncpy(dest, src, destSize-1). However, in this case, you might truncate some of the string you're trying to copy. Also, note that in this case you must manually set the last byte of the destination buffer to '\0', since strncpy might not do it if the source string is too long.
Quote:Original post by eyal

This really isn't a good idea. If you don't know what the destination buffer currently contains, you can't expect strlen to return its size. Nothing guarantees the buffer is null-terminated (and you can't do destBuffer[destSize-1] = '\0', since there might be a '\0' somewhere else in the buffer, in which case strlen will return a value smaller than the buffer's length).


See my second post



clicoder:


I wrote a small test app which does sizeof on a char[20], and it works fine (using gcc).

After careful deliberation, I have come to the conclusion that Nazrix is not cool. I am sorry for any inconvienience my previous mistake may have caused. We now return you to the original programming

It seems to be working fine in my application too.

~Wave
Quote:Original post by MichaelT
Quote:Original post by Anonymous Poster
....which clearly is due to a bug.


I've tracked down the crashpoint to a location to a loop inside the the _crt.. function. ;) But thanks for the heads up.


No, it really is your fault. The crash in the CRT is because you're doing *something* wrong with memory. Either writing to memory that doesn't belong to you, or overwriting the end of an array or something.

I know it seems strange that the crash is outside your code, but if the CRT was bugged so badly it would have been fixed by now.

Then again, you're using the latest VC6 service pack, right?
Quote:Original post by siaspete
Quote:Original post by MichaelT
Quote:Original post by Anonymous Poster
....which clearly is due to a bug.


I've tracked down the crashpoint to a location to a loop inside the the _crt.. function. ;) But thanks for the heads up.


No, it really is your fault. The crash in the CRT is because you're doing *something* wrong with memory.


I can't tell exactly if this is the issue I had, but if the debugger is saying something along the lines of freed heap memory being written to at address. Then that probably means that siaspete is right about you going beyond an array bounds.

~Wave
Quote:Original post by siaspete
No, it really is your fault. The crash in the CRT is because you're doing *something* wrong with memory. Either writing to memory that doesn't belong to you, or overwriting the end of an array or something.

I know it seems strange that the crash is outside your code, but if the CRT was bugged so badly it would have been fixed by now.

Then again, you're using the latest VC6 service pack, right?


;) In all kindess, I know it is my fault (so please don't make that a point)(read previous post a bit higher up "...I released memory that contained...")

btw I am using .net not vs6.
No no no no! :)
Quote:Original post by Wavewash
I can't tell exactly if this is the issue I had, but if the debugger is saying something along the lines of freed heap memory being written to at address. Then that probably means that siaspete is right about you going beyond an array bounds.

~Wave


As I mentioned I did go beyond bounds but in my case it just halted with a vengeance ;) I used the debugger and traced the steps into a loop within the _crt.. function. I can't remake it since I rewrote the whole thing to be correct but it was during a loop within the _crt.. where it crashed. The only message was that it could not read the memory location. I sincerely hope that I don't have a faulty memory chip though (hmmm, didn't think of that until now)
No no no no! :)
Quote:Original post by MichaelT
The only message was that it could not read the memory location. I sincerely hope that I don't have a faulty memory chip though (hmmm, didn't think of that until now)


I had this issue with a library, where it said it could not write to a memory address of a certain library and thus had linker issues. I recreated the project and the issue went away. I don't think it'd be chip issue honestly (Fingers crossed, that'd be an awful issue to deal with). I think you'd see your programs randomly crashing if it was the problem.

~Wave
Quote:Original post by Nurgle
I wrote a small test app which does sizeof on a char[20], and it works fine (using gcc).

sizeof should work fine only on fixed-size arrays, but not on anything you dynamically allocate. Like cilcoder said, used on a pointer, it will return the size of the pointer (4 bytes on a x86). AFAIK, there is no way to get the size of a dynamically allocated array having only a pointer to the array.

This topic is closed to new replies.

Advertisement