Ah, you're casting the return value of fgetc() to a char and then testing that against EOF. If the comparison fails then you won't break the loop on EOF and you end up with an overrun. Accept the value of fgetc() as its native type and then test it against EOF, then write to the buffer based on the result.
Just out of curiosity, why not just use fread() here?
Remember to close your FILE pointer when you're done or you'll leak handles.
When you acquire a resource (opening a file or allocating memory, etc) it's important to test for success before continuing and it's important to remember to release the resource when you're done with it.
Oh, and:
*p = '\0';
Could just be:
*p = 0;
But if you're going to write an extra byte like that then you need to increase the allocation size by 1.
Ah, I was so busy looking for the bug I didn't realize - when you do things like this you should use the properties of the destination (length or boundary positions) to control the loop rather than the properties of the source.
Show differencesHistory of post edits
#ActualKhatharr
Posted 21 January 2013 - 03:40 AM
#5Khatharr
Posted 21 January 2013 - 03:28 AM
Ah, you're casting the return value of fgetc() to a char and then testing that against EOF. If the comparison fails then you won't break the loop on EOF and you end up with an overrun. Accept the value of fgetc() as its native type and then test it against EOF, then write to the buffer based on the result.
Just out of curiosity, why not just use fread() here?
Remember to close your FILE pointer when you're done or you'll leak handles.
When you acquire a resource (opening a file or allocating memory, etc) it's important to test for success before continuing and it's important to remember to release the resource when you're done with it.
Oh, and:
*p = '\0';
Could just be:
*p = 0;
But if you're going to write an extra byte like that then you need to increase the allocation size by 1.
Ah, I was so busy looking for the bug I didn't realize - when you do things like this you should use the properties of the buffer (length or boundary positions) to control the loop rather than the properties of the input.
Just out of curiosity, why not just use fread() here?
Remember to close your FILE pointer when you're done or you'll leak handles.
When you acquire a resource (opening a file or allocating memory, etc) it's important to test for success before continuing and it's important to remember to release the resource when you're done with it.
Oh, and:
*p = '\0';
Could just be:
*p = 0;
But if you're going to write an extra byte like that then you need to increase the allocation size by 1.
Ah, I was so busy looking for the bug I didn't realize - when you do things like this you should use the properties of the buffer (length or boundary positions) to control the loop rather than the properties of the input.
#4Khatharr
Posted 21 January 2013 - 03:03 AM
Ah, you're casting the return value of fgetc() to a char and then testing that against EOF. If the comparison fails then you won't break the loop on EOF and you end up with an overrun. Accept the value of fgetc() as its native type and then test it against EOF, then write to the buffer based on the result.
Just out of curiosity, why not just use fread() here?
Remember to close your FILE pointer when you're done or you'll leak handles.
When you acquire a resource (opening a file or allocating memory, etc) it's important to test for success before continuing and it's important to remember to release the resource when you're done with it.
Oh, and:
*p = '\0';
Could just be:
*p = 0;
But if you're going to write an extra byte like that then you need to increase the allocation size by 1.
Just out of curiosity, why not just use fread() here?
Remember to close your FILE pointer when you're done or you'll leak handles.
When you acquire a resource (opening a file or allocating memory, etc) it's important to test for success before continuing and it's important to remember to release the resource when you're done with it.
Oh, and:
*p = '\0';
Could just be:
*p = 0;
But if you're going to write an extra byte like that then you need to increase the allocation size by 1.
#3Khatharr
Posted 21 January 2013 - 03:01 AM
Ah, you're casting the return value of fgetc() to a char and then testing that against EOF. If the comparison fails then you won't break the loop on EOF and you end up with an overrun. Accept the value of fgetc() as its native type and then test it against EOF, then write to the buffer based on the result.
Just out of curiosity, why not just use fread() here?
Remember to close your FILE pointer when you're done or you'll leak handles.
When you acquire a resource (opening a file or allocating memory, etc) it's important to test for success before continuing and it's important to remember to release the resource when you're done with it.
Oh, and:
*p = '\0';
could just be:
*p = 0;
Just out of curiosity, why not just use fread() here?
Remember to close your FILE pointer when you're done or you'll leak handles.
When you acquire a resource (opening a file or allocating memory, etc) it's important to test for success before continuing and it's important to remember to release the resource when you're done with it.
Oh, and:
*p = '\0';
could just be:
*p = 0;
#2Khatharr
Posted 21 January 2013 - 03:00 AM
Ah, you're casting the return value of fgetc() to a char and then testing that against EOF. If the comparison fails then you won't break the loop on EOF and you end up with an overrun. Accept the value of fgetc() as its native type and then test it against EOF, then write to the buffer based on the result.
Just out of curiosity, why not just use fread() here?
Remember to close your FILE pointer when you're done or you'll leak handles.
When you acquire a resource (opening a file or allocating memory, etc) it's important to test for success before continuing and it's important to remember to release the resource when you're done with it.
Just out of curiosity, why not just use fread() here?
Remember to close your FILE pointer when you're done or you'll leak handles.
When you acquire a resource (opening a file or allocating memory, etc) it's important to test for success before continuing and it's important to remember to release the resource when you're done with it.
#1Khatharr
Posted 21 January 2013 - 03:00 AM
Ah, you're casting the return value of fgetc() to a char and then testing that against EOF. If the comparison fails then you won't break the loop on EOF and you end up with an overrun. Accept the value of fgetc() as its native type and then test it against EOF, then write to the buffer based on the result.<br /><br />Just out of curiosity, why not just use fread() here?<br /><br />Remember to close your FILE pointer when you're done or you'll leak handles.<br /><br />When you acquire a resource (opening a file or allocating memory, etc) it's important to test for success before continuing and it's important to remember to release the resource when you're done with it.