Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[Linux] For loop leads to segmentation fault

form for loop loop

  • Please log in to reply
8 replies to this topic

#1 sakishrist

sakishrist

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 107 posts

Posted 29 June 2011 - 07:46 AM

Hello everyone,

I would like a little help with some code of mine. The problem is not just what is written at the title of the thread, there is one more.

First of all, here is the code (if you need to know what the file that I process contains, then I will post that information too):
#include <iostream>
#include <zlib.h>
#include <fstream>

using namespace std;

int _decompress(Bytef * in_data, long in_size, Bytef * out_data, long * out_size){

    z_stream z;

    int status;
    
    z.zalloc = Z_NULL;
    z.zfree = Z_NULL;
    z.opaque = Z_NULL;
    
    z.avail_in = in_size;
    z.next_in = in_data;
    
    z.avail_out = *out_size;
    z.next_out = out_data;

    status = inflateInit(&z);   [B][COLOR=Red] //SEGMENTATION FAULT[/COLOR][/B]

    if ( status != Z_OK )
        return status;
    
    if ( z.avail_in != 0 && *out_size != 0 ) {
        status = inflate( &z, Z_NO_FLUSH );    [B][COLOR=Red]//SEGMENTATION FAULT[/COLOR][/B]
        if ( status < 0 ){
            cout << "Error: " << status << endl;
            return -11;
        }
        else if ( status == Z_OK && *out_size > 0 ){
            cout << "Not enough output space!\n";
            return -12;
        }

    }else if( *out_size != 0 ){
        cout << "No input?\n";
        return -10;
    }
    
    while ( status == Z_OK  ) {
        
        *out_size += 1024;
        z.avail_out = *out_size;
        delete out_data;
        out_data = new Bytef[*out_size];
        z.next_out = out_data;
        
        status = inflate( &z, Z_NO_FLUSH );    [B][COLOR=Red]//SEGMENTATION FAULT[/COLOR][/B]
    }
    
    if ( status != Z_STREAM_END );
        cout << "Final Error: " << status << endl;

    (void)inflateEnd(&z);
    
    return status;
} 



int main(){
    ifstream fp_in;
    fp_in.open("r.0.0.mcr", ios::in);
    
    char *a = new char[5]; 


    for(int i = 0; i<1024; i++){ 
        
        fp_in.seekg(i*4);
        fp_in.read(a, 4); 
        cout << i << ":";
        
        if((int)a[0]!=0 || (int)a[1]!=0 || (int)a[2]!=0 || (int)a[3]!=0){
            
            cout     << (i+1)%32 
                    << "x" 
                    << (i+1)/32 
                    << " ( Pos: " 
                    << (  (int)(unsigned char)a[0]*65536 
                        + (int)(unsigned char)a[1]*255 
                        + (int)(unsigned char)a[2]            )*4096 
                    << " Size: ";

            fp_in.seekg((      (int)(unsigned char)a[0]*65536 
                             + (int)(unsigned char)a[1]*256 
                             + (int)(unsigned char)a[2]            )*4096);
            

            fp_in.read(a,5);

            long size =   (int)(unsigned char)a[0]*16777216 
                        + (int)(unsigned char)a[1]*65536 
                        + (int)(unsigned char)a[2]*256 
                        + (int)(unsigned char)a[3];
            
            cout << (int)(unsigned char)a[0] << ":" ;        [B][COLOR=Red]//PROBLEM WITH COUT[/COLOR][/B]
            cout << (int)(unsigned char)a[1] << ":" ;
            cout << (int)(unsigned char)a[2] << ":" ;
            cout << (int)(unsigned char)a[3] << " = "; 
            cout << size << " ) \t:";

            char *buff = new char[size-1];

            long out_size = 0; 
            Bytef *out;
            
            fp_in.read(buff,size-1);
            int status = _decompress((Bytef*)buff,size-1,out,&out_size);

            if(status>=0)
                cout << "Decompressed size: " << out_size << " bytes.";
            else
                cout << "Error: " << status;
        }
        cout << endl;
    }

    for(int i = 0; i<1024; i++)        [B][COLOR=Red]//SEGMENTATION FAULT[/COLOR][/B]
        cout << "Anything";

    
    fp_in.close();

    return 0;
}
So, here are the problems I have

1) At the five lines with the "Problem with cout" comment, if I attempt to do that in one line I get a weird error that I think is from zlib, and not g++ (this is a runtime error):
*** glibc detected *** ./zlib_c: munmap_chunk(): invalid pointer: 0x00007f739d6f5048 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x76bb6)[0x7f739c826bb6]
./zlib_c[0x400f69]
./zlib_c[0x4013e8]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xff)[0x7f739c7ceeff]
./zlib_c[0x400d19]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:03 3014917                            /home/sakishrist/Desktop/c++/zlib/zlib_c
00601000-00602000 r--p 00001000 08:03 3014917                            /home/sakishrist/Desktop/c++/zlib/zlib_c
00602000-00603000 rw-p 00002000 08:03 3014917                            /home/sakishrist/Desktop/c++/zlib/zlib_c
00d2e000-00d4f000 rw-p 00000000 00:00 0                                  [heap]
7f739c7b0000-7f739c93a000 r-xp 00000000 08:03 265932                     /lib/x86_64-linux-gnu/libc-2.13.so
7f739c93a000-7f739cb39000 ---p 0018a000 08:03 265932                     /lib/x86_64-linux-gnu/libc-2.13.so
7f739cb39000-7f739cb3d000 r--p 00189000 08:03 265932                     /lib/x86_64-linux-gnu/libc-2.13.so
7f739cb3d000-7f739cb3e000 rw-p 0018d000 08:03 265932                     /lib/x86_64-linux-gnu/libc-2.13.so
7f739cb3e000-7f739cb44000 rw-p 00000000 00:00 0 
7f739cb44000-7f739cb59000 r-xp 00000000 08:03 265960                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f739cb59000-7f739cd58000 ---p 00015000 08:03 265960                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f739cd58000-7f739cd59000 r--p 00014000 08:03 265960                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f739cd59000-7f739cd5a000 rw-p 00015000 08:03 265960                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f739cd5a000-7f739cdde000 r-xp 00000000 08:03 265969                     /lib/x86_64-linux-gnu/libm-2.13.so
7f739cdde000-7f739cfdd000 ---p 00084000 08:03 265969                     /lib/x86_64-linux-gnu/libm-2.13.so
7f739cfdd000-7f739cfde000 r--p 00083000 08:03 265969                     /lib/x86_64-linux-gnu/libm-2.13.so
7f739cfde000-7f739cfdf000 rw-p 00084000 08:03 265969                     /lib/x86_64-linux-gnu/libm-2.13.so
7f739cfdf000-7f739d0c7000 r-xp 00000000 08:03 3416616                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14
7f739d0c7000-7f739d2c6000 ---p 000e8000 08:03 3416616                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14
7f739d2c6000-7f739d2ce000 r--p 000e7000 08:03 3416616                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14
7f739d2ce000-7f739d2d0000 rw-p 000ef000 08:03 3416616                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14
7f739d2d0000-7f739d2e5000 rw-p 00000000 00:00 0 
7f739d2e5000-7f739d2fd000 r-xp 00000000 08:03 3824230                    /usr/local/lib/libz.so.1.2.5
7f739d2fd000-7f739d4fc000 ---p 00018000 08:03 3824230                    /usr/local/lib/libz.so.1.2.5
7f739d4fc000-7f739d4fd000 r--p 00017000 08:03 3824230                    /usr/local/lib/libz.so.1.2.5
7f739d4fd000-7f739d4fe000 rw-p 00018000 08:03 3824230                    /usr/local/lib/libz.so.1.2.5
7f739d4fe000-7f739d51f000 r-xp 00000000 08:03 265919                     /lib/x86_64-linux-gnu/ld-2.13.so
7f739d6f2000-7f739d6f7000 rw-p 00000000 00:00 0 
7f739d71b000-7f739d71e000 rw-p 00000000 00:00 0 
7f739d71e000-7f739d71f000 r--p 00020000 08:03 265919                     /lib/x86_64-linux-gnu/ld-2.13.so
7f739d71f000-7f739d721000 rw-p 00021000 08:03 265919                     /lib/x86_64-linux-gnu/ld-2.13.so
7fffb9a67000-7fffb9a88000 rw-p 00000000 00:00 0                          [stack]
7fffb9bfd000-7fffb9bfe000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted
2) At the end, there is a for loop. That loop produces a Segmentation Fault. If I remove it, everything is fine. I also tried to remove all the three lines that have the "Segmentation Fault" comment. The Segmentation Fault is gone then as well. So my guess is that this has to do with zlib but I have absolutely no idea how a for loop could be related to zlib.

I appreciate any help with any of these problems!

Thanks in advance!
  • 0

#2 denarced

denarced

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 181 posts

Posted 29 June 2011 - 07:55 AM

in function _decompress and in the while-loop you:
delete out_data;
This looks bad. It's in a loop and there could be several attempts to delete the same stuff.
  • 0

#3 sakishrist

sakishrist

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 107 posts

Posted 29 June 2011 - 08:02 AM

Thanks a million!

That seems to be the solution for both problems, but could you please explain why such a thing has such effects on cout and on that for loop?
  • 0

#4 denarced

denarced

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 181 posts

Posted 29 June 2011 - 08:02 AM

Why would you have two casts in a row ?
(int)(unsigned char)

  • 0

#5 sakishrist

sakishrist

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 107 posts

Posted 29 June 2011 - 08:04 AM

Why would you have two casts in a row ?

Oh, yes ... That was something I also wanted to ask about. How can I convert a char to int but in the range of 0-255 and not -127 to 127. That was what I was trying to do there
  • 0

#6 denarced

denarced

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 181 posts

Posted 29 June 2011 - 08:11 AM

I have absolutely no idea. When using cout, most systems buffer the things you stick in them. That could be related to your problems and I think that by inserting end of lines the buffer is flushed so you have a way to test that.

On the other hand, you dynamically allocate space with the keyword new in your main function. To me it seems like your allocations could be huge as you use long int to calculate the allocated memory. Failed allocation would result in bad_alloc exception so that's probably not your problem thou. Just something to keep in mind.
  • 0

#7 denarced

denarced

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 181 posts

Posted 29 June 2011 - 08:14 AM

Oh, yes ... That was something I also wanted to ask about. How can I convert a char to int but in the range of 0-255 and not -127 to 127. That was what I was trying to do there


By using unsigned char instead of char. And by the way, if you have the option, use int instead of char to represent numbers to the extent that you count something with them (Optimizing C and C++ Code)
  • 0

#8 sakishrist

sakishrist

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 107 posts

Posted 29 June 2011 - 08:20 AM

Thanks a lot!!!

If I use unsigned char then what would be the best way to deal with the ifstream.read method? It needs to be char. Should I use (char*)a just there?
  • 0

#9 fkl

fkl

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 417 posts

Posted 29 June 2011 - 12:45 PM

How do you know that the data your are decompressing using inflate is valid and is not the cause of your seg fault. I would say to follow the example you should take standard input, first compress (deflate) it using the api and then deflate to be certain.

zlib Usage Example
  • 0
Today is the first day of the rest of my life





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download