Coder Social home page Coder Social logo

Comments (2)

LADSoft avatar LADSoft commented on June 7, 2024

Hi LionFaith!

I looked at your code and the sample below has a small problem in the
fread statement. You take the address of 'the_text' when I think you
mean to use it directly (taking the address of a pointer variable and
using it as an fread buffer would be a bug unless you were intentionally
meaning to read in a pointer value).

When I fixed that the below program ran to completion but that says
nothing about the original problem you were having. I'm wondering if
you could try to refine your example of the problem some more?

Thanks,

David

lionfaith wrote:

Hello David!
I was writing a C program which reads a file and puts it in a buffer
and was manipulating two others. As long as I was working with fixed
size buffer (aka char mybuffer[SIZE]), I had no problem at all. But
when switching to allocated buffers and sys/stat for the size of the
file, things began to go wrong.

So, my algorithm does first stat the size of the file, then malloc a
buffer with the given size, then read the file into that buffer, then
malloc a second buffer and then a third. Whe doing so, the third
allocation fails with a "Not enough memory" through a perror.

If I refactor the code and puting the algorithm in this order: stat,
first malloc, second malloc, third malloc, read the file, then I get a
page fault the very first time the first buffer is accessed. But it is
not null, all allocations are checked against null.

I have tried to reduce my code to a minimal sample... and the bug
changed, now fread is hanging! Here is the reduced version (also tried
the cpp version, same problem):

/*cpp version: #include <stdlib.h> */
#include <stdio.h>
#include <sys/stat.h>

char* the_text;
unsigned int text_size;

char* buff1;
char* buff2;

int initialize(void) {
char* file_name ="mytext.txt";
struct stat st;
FILE* myfile;
unsigned int actual_size;

 puts("ok2"  );
 stat( file_name, &st );
 text_size = st.st_size;
 the_text =malloc( text_size );/*cpp version: (char*) malloc( text_size )*/
 if( !the_text ) {
     perror("No text allocation"  );
     return  -1;
 }

 puts("ok3"  );
 myfile =fopen( file_name,"r"  );
     if( !myfile ) {
     perror("file couldn't be open"  );
     return  -1;
 }
 printf("text_size: %d\n", text_size );/* => 12064 */
 fread( &the_text,1, text_size, myfile );/* HANGS HERE! */
 puts("ok3b"  );/* no ok3b */
 fclose( myfile );

 puts("ok4"  );
 buff1 =malloc( text_size );/*cpp version: (char*) malloc( text_size ) */
 if( !buff1 ) {
     perror("No buff1 allocation"  );
     return  -1;
 }

 puts("ok5"  );
 buff2 =malloc( text_size );/*cpp version: (char*) malloc( text_size ) */
 if( !buff2 ) {
     perror("No buff2 allocation"  );
     return  -1;
 }

 puts("ok6"  );

 return  0;

}

int main(void) {
puts("ok1" );
if(initialize() == -1 )return 1;

 return  0;

}

I haven't check with older version of occ yet.


Reply to this email directly or view it on GitHub
#19.

from orangec.

lionfaith avatar lionfaith commented on June 7, 2024

oh my! How confusing!

I switched from char the_text[SIZE] to char* the_text and change no code using it and there were no problem... Excepted for fread to which I was now actually passing a char** implicitly casted into a void* and undefined behavior has occured. Most probably, fread was corrupting every variables declared after the_text. Because now, after applying your advice, everything is working fine.

My fault! I was too tired when programming. Sorry for the disturbance. Big thank for your help.

from orangec.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.