Topic
  • 3 replies
  • Latest Post - ‏2012-12-10T20:17:24Z by SystemAdmin
SystemAdmin
SystemAdmin
7929 Posts

Pinned topic Buffer overflow error in defining IloIntVarArray 3 Dimensional Array

‏2012-12-05T00:24:29Z |
Hi,

I have a piece of code which is perfectly working under windows version of CPLEX in my colleague's computer. I am running Ubuntu 10.10 (2.6.35-32-generic x86_64) on a 64 bit system. I do installed the linux 64 bit version of CPLEX 12.4 on my system I have a LICENSE IBM ILOG Optimization Suite for Academic Initiative. I also have installed the "IBM ILOG CPLEX Optimization Studio Fix Pack 12.4.0.1" and I am still getting the same error. This how I define my 3D array:

// My this portion of CPLEX code to deine the 3D variable
cout<<" alpha "<<endl;
//define the variable alpha[i][j][t]
alpha = IloIntVarArray3(env,N);
//alpha.setSize(N);
for(i=0;i<N;i++)
{
alpha[i] = IloIntVarArray2(env,N);
for(j=0;j<N;j++)
{
alpha[i][j] = IloIntVarArray(env,T);
for(t=0;t<T;t++)
{
alpha[i][j][t] = IloIntVar(env); //The problem is here when I comment this it works
sprintf((char*)str_name.c_str(),"alpha%d%d%d",i,j,t);
alpha[i][j][t].setName(str_name.c_str());
}
}
}
I also have check IBM suggested code in http://www-01.ibm.com/support/docview.wss?uid=swg21399963

And I got the following "Overflow buffer error" regardless:

  • buffer overflow detected ***: ./simulation terminated
======= Backtrace: =========
/lib/libc.so.6(__fortify_fail+0x37)0x7f05a1f2bb47
/lib/libc.so.6(+0xfea00)0x7f05a1f2aa00
/lib/libc.so.6(+0xfde69)0x7f05a1f29e69
/lib/libc.so.6(_IO_default_xsputn+0xcc)0x7f05a1ea1e4c
/lib/libc.so.6(_IO_vfprintf+0x66f)0x7f05a1e71caf
/lib/libc.so.6(__vsprintf_chk+0x99)0x7f05a1f29f09
/lib/libc.so.6(__sprintf_chk+0x7f)0x7f05a1f29e4f
./simulation0x4442f8
./simulation0x43ec44
/lib/libc.so.6(__libc_start_main+0xfe)0x7f05a1e4ad8e
./simulation0x43e699
======= Memory map: ========
00400000-015bc000 r-xp 00000000 08:03 3411339 /home/brian/Documents/CPP/MIMOSIC Simulation/simulation
017bb000-017de000 r-xp 011bb000 08:03 3411339 /home/brian/Documents/CPP/MIMOSIC Simulation/simulation
017de000-01811000 rwxp 011de000 08:03 3411339 /home/brian/Documents/CPP/MIMOSIC Simulation/simulation
01811000-0181e000 rwxp 00000000 00:00 0
01d6a000-01dee000 rwxp 00000000 00:00 0 heap
7f05a05f1000-7f05a05fd000 r-xp 00000000 08:01 2625989 /lib/libnss_files-2.12.1.so
7f05a05fd000-7f05a07fc000 ---p 0000c000 08:01 2625989 /lib/libnss_files-2.12.1.so
7f05a07fc000-7f05a07fd000 r-xp 0000b000 08:01 2625989 /lib/libnss_files-2.12.1.so
7f05a07fd000-7f05a07fe000 rwxp 0000c000 08:01 2625989 /lib/libnss_files-2.12.1.so
7f05a07fe000-7f05a0808000 r-xp 00000000 08:01 2625991 /lib/libnss_nis-2.12.1.so
7f05a0808000-7f05a0a07000 ---p 0000a000 08:01 2625991 /lib/libnss_nis-2.12.1.so
7f05a0a07000-7f05a0a08000 r-xp 00009000 08:01 2625991 /lib/libnss_nis-2.12.1.so
7f05a0a08000-7f05a0a09000 rwxp 0000a000 08:01 2625991 /lib/libnss_nis-2.12.1.so
7f05a0a09000-7f05a0a20000 r-xp 00000000 08:01 2625978 /lib/libnsl-2.12.1.so
7f05a0a20000-7f05a0c1f000 ---p 00017000 08:01 2625978 /lib/libnsl-2.12.1.so
7f05a0c1f000-7f05a0c20000 r-xp 00016000 08:01 2625978 /lib/libnsl-2.12.1.so
7f05a0c20000-7f05a0c21000 rwxp 00017000 08:01 2625978 /lib/libnsl-2.12.1.so
7f05a0c21000-7f05a0c23000 rwxp 00000000 00:00 0
7f05a0c23000-7f05a0c2b000 r-xp 00000000 08:01 2625987 /lib/libnss_compat-2.12.1.so
7f05a0c2b000-7f05a0e2a000 ---p 00008000 08:01 2625987 /lib/libnss_compat-2.12.1.so
7f05a0e2a000-7f05a0e2b000 r-xp 00007000 08:01 2625987 /lib/libnss_compat-2.12.1.so
7f05a0e2b000-7f05a0e2c000 rwxp 00008000 08:01 2625987 /lib/libnss_compat-2.12.1.so
7f05a0e2c000-7f05a1e2c000 rwxp 00000000 00:00 0
7f05a1e2c000-7f05a1fa6000 r-xp 00000000 08:01 2622224 /lib/libc-2.12.1.so
7f05a1fa6000-7f05a21a6000 ---p 0017a000 08:01 2622224 /lib/libc-2.12.1.so
7f05a21a6000-7f05a21aa000 r-xp 0017a000 08:01 2622224 /lib/libc-2.12.1.so
7f05a21aa000-7f05a21ab000 rwxp 0017e000 08:01 2622224 /lib/libc-2.12.1.so
7f05a21ab000-7f05a21b0000 rwxp 00000000 00:00 0
7f05a21b0000-7f05a21c8000 r-xp 00000000 08:01 2625994 /lib/libpthread-2.12.1.so
7f05a21c8000-7f05a23c7000 ---p 00018000 08:01 2625994 /lib/libpthread-2.12.1.so
7f05a23c7000-7f05a23c8000 r-xp 00017000 08:01 2625994 /lib/libpthread-2.12.1.so
7f05a23c8000-7f05a23c9000 rwxp 00018000 08:01 2625994 /lib/libpthread-2.12.1.so
7f05a23c9000-7f05a23cd000 rwxp 00000000 00:00 0
7f05a23cd000-7f05a23e2000 r-xp 00000000 08:01 2621498 /lib/libgcc_s.so.1
7f05a23e2000-7f05a25e1000 ---p 00015000 08:01 2621498 /lib/libgcc_s.so.1
7f05a25e1000-7f05a25e2000 r-xp 00014000 08:01 2621498 /lib/libgcc_s.so.1
7f05a25e2000-7f05a25e3000 rwxp 00015000 08:01 2621498 /lib/libgcc_s.so.1
7f05a25e3000-7f05a2665000 r-xp 00000000 08:01 2625976 /lib/libm-2.12.1.so
7f05a2665000-7f05a2864000 ---p 00082000 08:01 2625976 /lib/libm-2.12.1.so
7f05a2864000-7f05a2865000 r-xp 00081000 08:01 2625976 /lib/libm-2.12.1.so
7f05a2865000-7f05a2866000 rwxp 00082000 08:01 2625976 /lib/libm-2.12.1.so
7f05a2866000-7f05a294e000 r-xp 00000000 08:01 10492424 /usr/lib/libstdc++.so.6.0.14
7f05a294e000-7f05a2b4d000 ---p 000e8000 08:01 10492424 /usr/lib/libstdc++.so.6.0.14
7f05a2b4d000-7f05a2b55000 r-xp 000e7000 08:01 10492424 /usr/lib/libstdc++.so.6.0.14
7f05a2b55000-7f05a2b57000 rwxp 000ef000 08:01 10492424 /usr/lib/libstdc++.so.6.0.14
7f05a2b57000-7f05a2b6c000 rwxp 00000000 00:00 0
7f05a2b6c000-7f05a2b8c000 r-xp 00000000 08:01 2622220 /lib/ld-2.12.1.so
7f05a2d66000-7f05a2d6b000 rwxp 00000000 00:00 0
7f05a2d89000-7f05a2d8c000 rwxp 00000000 00:00 0
7f05a2d8c000-7f05a2d8d000 r-xp 00020000 08:01 2622220 /lib/ld-2.12.1.so
7f05a2d8d000-7f05a2d8e000 rwxp 00021000 08:01 2622220 /lib/ld-2.12.1.so
7f05a2d8e000-7f05a2d8f000 rwxp 00000000 00:00 0
7fff76c51000-7fff76c72000 rwxp 00000000 00:00 0 stack
7fff76c8f000-7fff76c90000 r-xp 00000000 00:00 0 vdso
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 vsyscall
Aborted

I really appreciate any help and unfortunately, my last resort is to run my code in our other systems here that are running CPLEX under Windows XP with the commercial licence. I really don't want to this as this means to me that CPLEX is not stable enough in LINUX which I don't want to accept that. I do appreciate any help and I think this might be an important bug (assuming I am not making any mistake).
Updated on 2012-12-10T20:17:24Z at 2012-12-10T20:17:24Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    7929 Posts

    Re: Buffer overflow error in defining IloIntVarArray 3 Dimensional Array

    ‏2012-12-05T06:48:21Z  
    Hi,

    the problem is in the sprintf statement used to generate the name. Use an explicitly allocated buffer instead of str_name.c_str(), e.g

    char psz_name100;
    sprintf(psz_name,"alpha%d%d%d",i,j,t);
    alpha[i][j][t].setName(psz_name);

    The line

    /lib/libc.so.6(__sprintf_chk+0x7f)0x7f05a1f29e4f

    in the backtrace is a telltale. Under Windows you just got lucky, since it is never OK to write to an std::string like that.

    Best,
    MCM
  • SystemAdmin
    SystemAdmin
    7929 Posts

    Re: Buffer overflow error in defining IloIntVarArray 3 Dimensional Array

    ‏2012-12-05T12:20:11Z  
    As MCM already said, the issue is completely unrelated to CPLEX. The problem is this statement
    
    sprintf((char*)str_name.c_str(),
    "alpha[%d][%d][%d]",i,j,t);
    

    which contains multiple programming errors:
    1. Like MCM said, the buffer returned by std::string::c_str() is not guaranteed to be large enough to hold the resulting string.
    2. The buffer returned by std::string::c_str() is of type 'char const *', that is, it is supposed to be a read-only value and you are not supposed to write to this buffer.
    3. If you know exactly what you are doing (since your program crashes this does not seem to be the case) and want to cast away the 'const' qualifier so that you can write to the buffer then you should not use an old-style cast but should instead use const_cast.

    There are two obvious ways to fix that (the first one was already suggested by MCM):
    1. Use a buffer that is guaranteed to be large enough. This also renders usage of std::string unnecessary:
    
    
    
    char buffer[128]; sprintf(buffer,
    "alpha[%d][%d][%d]",i,j,t); 
    // or even better: use snprintf alpha[i][j][t].setName(buffer);
    

    2. Use a std::stringstream:
    
    std::stringstream s; s << 
    "alpha[" << i << 
    "][" << j << 
    "][" << t << 
    "]"; alpha[i][j][t].setName(s.str().c_str());
    
  • SystemAdmin
    SystemAdmin
    7929 Posts

    Re: Buffer overflow error in defining IloIntVarArray 3 Dimensional Array

    ‏2012-12-10T20:17:24Z  
    Thank you as MCM and DanielJunglas mentioned. This has nothing to do with CPLEX and the problem was using sprintf and not allocating enough buffer. I have corrected the code and the whole program is running now. I am glad that CPLEX is perfectly work fine in Linux 64 bit.