Thursday, 22 August 2013

How do I get rid of this error message :*** buffer overflow detected *** in C++

How do I get rid of this error message :*** buffer overflow detected ***
in C++

I am running the following c++ code in ubuntu:
/******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "interface.h"
#include "subs_inoutput.h"
#include "subs_common_string.h"
#include "subs_fits.h"
#include "subs_asciifile.h"
#include "subs_memory.h"
#include "subs_lambert.h"
/******************************************************************************/
void main
(int argc,
char * ppArgv[],
char * ppEnvp[])
{
int ic;
int imap;
int ii;
int qInterp;
int qVerbose;
int qNoloop;
int iGal;
int nGal;
int nCol;
int bitval[8];
float tmpl;
float tmpb;
float * pGall = NULL;
float * pGalb = NULL;
float * pMapval;
float * pData;
FILE * pFILEout;
static char pPrivW[] = "w";
/* Declarations for keyword input values */
int ienv;
int nkey;
char * pTemp;
char * pKeyname;
char * pKeyval;
char * pMapName;
char * pInFile = NULL;
char * pOutName = NULL;
char * pIPath = NULL;
char pDefPath[] = "./";
char pDefMap[] = "Ebv";
const char pDUST_DIR[] = "DUST_DIR";
const char pEq[] = "=";
const char pText_mask[] = "mask";
const char pText_mapdir[] = "/maps/";
/* Declarations for command-line keyword names */
const char pText_ipath[] = "ipath";
const char pText_infile[] = "infile";
const char pText_outfile[] = "outfile";
const char pText_map[] = "map";
const char pText_interp[] = "interp";
const char pText_noloop[] = "noloop";
const char pText_verbose[] = "verbose";
/* Bit mask string values */
char ppBitname[][8] = {
" " , " ",
" " , " ",
"OK " , "asteroi",
"OK " , "glitch ",
"OK " , "source ",
"OK " , "no_list",
"OK " , "big_obj",
"OK " , "no_IRAS" };
/* Declarations for data file names */
char pFileN[MAX_FILE_NAME_LEN];
char pFileS[MAX_FILE_NAME_LEN];
struct mapParms {
char * pName;
char * pFile1;
char * pFile2;
} ppMapAll[] = {
{ "Ebv" , "SFD_dust_4096_ngp.fits", "SFD_dust_4096_sgp.fits" },
{ "I100", "SFD_i100_4096_ngp.fits", "SFD_i100_4096_sgp.fits" },
{ "X" , "SFD_xmap_ngp.fits" , "SFD_xmap_sgp.fits" },
{ "T" , "SFD_temp_ngp.fits" , "SFD_temp_sgp.fits" },
{ "mask", "SFD_mask_4096_ngp.fits", "SFD_mask_4096_sgp.fits" }
};
const int nmap = sizeof(ppMapAll) / sizeof(ppMapAll[0]);
/* Set defaults */
pIPath = pDefPath;
pMapName = pDefMap;
qInterp = 0; /* no interpolation */
qVerbose = 0; /* not verbose */
qNoloop = 0; /* do not read entire image into memory */
/* Override default path by value in the environment variable DUST_DIR */
for (ienv=0; ppEnvp[ienv] != 0; ienv++) {
if (strcmp(pDUST_DIR,strtok(ppEnvp[ienv],pEq))==0 ) {
pIPath = strcat( strtok(NULL,pEq), pText_mapdir );
}
}
nkey = 0;
for (ic=1; ic < argc; ic++) {
/* Check if this argument is a keyword */
if ((pTemp=strchr(ppArgv[ic],'=')) != NULL) {
nkey++;
pKeyname = ppArgv[ic];
pKeyval = pTemp + 1;
pTemp[0] = '\0'; /* replace equals with NULL to terminate string */
if (strcmp(pKeyname,pText_infile) == 0) pInFile = pKeyval;
if (strcmp(pKeyname,pText_outfile) == 0) pOutName = pKeyval;
if (strcmp(pKeyname,pText_map) == 0) pMapName = pKeyval;
if (strcmp(pKeyname,pText_ipath) == 0) pIPath = pKeyval;
if (strcmp(pKeyname,pText_interp) == 0) {
if (strchr(pKeyval,'y') != NULL || strchr(pKeyval,'Y') != NULL)
qInterp = 1; /* do interpolation */
}
if (strcmp(pKeyname,pText_noloop) == 0) {
if (strchr(pKeyval,'y') != NULL || strchr(pKeyval,'Y') != NULL)
qNoloop=1; /* read entire image into memory */
}
if (strcmp(pKeyname,pText_verbose) == 0) {
if (strchr(pKeyval,'y') != NULL || strchr(pKeyval,'Y') != NULL)
qVerbose = 1; /* do interpolation */
}
}
}
/* If no input coordinate file, then read coordinates from either
* the command line or by query */
if (pInFile != NULL) {
asciifile_read_colmajor(pInFile, 2, &nGal, &nCol, &pData);
pGall = pData;
pGalb = pData + nGal;
} else {
if (argc-nkey > 2) {
sscanf(ppArgv[1], "%f", &tmpl);
sscanf(ppArgv[2], "%f", &tmpb);
} else {
printf("Galactic longitude (degrees):\n");
scanf("%f", &tmpl);
printf("Galactic latitude (degrees):\n");
scanf("%f", &tmpb);
}
nGal = 1;
pGall = ccvector_build_(nGal);
pGalb = ccvector_build_(nGal);
pGall[0] = tmpl;
pGalb[0] = tmpb;
}
/* Determine the file names to use */
for (imap=0; imap < nmap; imap++) {
if (strcmp(pMapName,ppMapAll[imap].pName) == 0) {
sprintf(pFileN, "%s/%s", pIPath, ppMapAll[imap].pFile1);
sprintf(pFileS, "%s/%s", pIPath, ppMapAll[imap].pFile2);
}
}
/* Disable interpolation if reading the mask */
if (strcmp(pMapName,pText_mask) == 0) qInterp = 0;
/* Read values from FITS files in Lambert projection */
pMapval = lambert_getval(pFileN, pFileS, nGal, pGall, pGalb,
qInterp, qNoloop, qVerbose);
/* If no output file, then output to screen */
if (pOutName != NULL) pFILEout = fopen(pOutName, pPrivW);
else pFILEout = stdout;
for (iGal=0; iGal < nGal; iGal++) {
fprintf(pFILEout, "%8.3f %7.3f", pGall[iGal], pGalb[iGal]);
if (strcmp(pMapName,pText_mask) == 0) {
/* Translate mask bits */
for (ii=0; ii < 8; ii++)
bitval[ii] = ( (int)pMapval[iGal] & (int)pow(2,ii) ) > 0;
fprintf(pFILEout, " %1dhcons", bitval[0]+2*bitval[1]);
for (ii=2; ii < 8; ii++) {
fprintf(pFILEout, " %7s", ppBitname[bitval[ii]+2*ii]);
}
fprintf(pFILEout, "\n");
} else {
fprintf(pFILEout, " %12.5f\n", pMapval[iGal]);
}
}
if (pOutName != NULL) fclose(pFILEout);
if (pInFile != NULL) {
ccfree_((void **)&pData);
} else {
ccfree_((void **)&pGall);
ccfree_((void **)&pGalb);
}
}
/******************************************************************************/
I run this command :
dust_getval 121 -21.5
and I get this error message:
*** buffer overflow detected ***: dust_getval terminated
======= Backtrace: =========
/lib/libc.so.6(__fortify_fail+0x37)[0x7f2c70ba1d27]
/lib/libc.so.6(+0x100be0)[0x7f2c70ba0be0]
/lib/libc.so.6(+0x100049)[0x7f2c70ba0049]
/lib/libc.so.6(_IO_default_xsputn+0xcc)[0x7f2c70b1731c]
/lib/libc.so.6(_IO_vfprintf+0x668)[0x7f2c70ae6d18]
/lib/libc.so.6(__vsprintf_chk+0x99)[0x7f2c70ba00e9]
/lib/libc.so.6(__sprintf_chk+0x7f)[0x7f2c70ba002f]
dust_getval[0x4060df]
dust_getval[0x40681a]
dust_getval[0x406a03]
dust_getval[0x406a83]
dust_getval[0x402458]
dust_getval[0x401779]
/lib/libc.so.6(__libc_start_main+0xfd)[0x7f2c70abec4d]
dust_getval[0x401069]
======= Memory map: ========
00400000-0040a000 r-xp 00000000 08:01 3315744820
/export/aibn84_1/zahra/Dust_Map/bin.i686/dust_getval
00609000-0060a000 r--p 00009000 08:01 3315744820
/export/aibn84_1/zahra/Dust_Map/bin.i686/dust_getval
0060a000-0060b000 rw-p 0000a000 08:01 3315744820
/export/aibn84_1/zahra/Dust_Map/bin.i686/dust_getval
01d4e000-01d6f000 rw-p 00000000 00:00 0
[heap]
7f2c70889000-7f2c7089f000 r-xp 00000000 08:11 359978
/lib/libgcc_s.so.1
7f2c7089f000-7f2c70a9e000 ---p 00016000 08:11 359978
/lib/libgcc_s.so.1
7f2c70a9e000-7f2c70a9f000 r--p 00015000 08:11 359978
/lib/libgcc_s.so.1
7f2c70a9f000-7f2c70aa0000 rw-p 00016000 08:11 359978
/lib/libgcc_s.so.1
7f2c70aa0000-7f2c70c1d000 r-xp 00000000 08:11 361967
/lib/libc-2.11.1.so
7f2c70c1d000-7f2c70e1c000 ---p 0017d000 08:11 361967
/lib/libc-2.11.1.so
7f2c70e1c000-7f2c70e20000 r--p 0017c000 08:11 361967
/lib/libc-2.11.1.so
7f2c70e20000-7f2c70e21000 rw-p 00180000 08:11 361967
/lib/libc-2.11.1.so
7f2c70e21000-7f2c70e26000 rw-p 00000000 00:00 0
7f2c70e26000-7f2c70ea8000 r-xp 00000000 08:11 361086
/lib/libm-2.11.1.so
7f2c70ea8000-7f2c710a7000 ---p 00082000 08:11 361086
/lib/libm-2.11.1.so
7f2c710a7000-7f2c710a8000 r--p 00081000 08:11 361086
/lib/libm-2.11.1.so
7f2c710a8000-7f2c710a9000 rw-p 00082000 08:11 361086
/lib/libm-2.11.1.so
7f2c710a9000-7f2c710c9000 r-xp 00000000 08:11 361493
/lib/ld-2.11.1.so
7f2c71289000-7f2c7128c000 rw-p 00000000 00:00 0
7f2c712c5000-7f2c712c8000 rw-p 00000000 00:00 0
7f2c712c8000-7f2c712c9000 r--p 0001f000 08:11 361493
/lib/ld-2.11.1.so
7f2c712c9000-7f2c712ca000 rw-p 00020000 08:11 361493
/lib/ld-2.11.1.so
7f2c712ca000-7f2c712cb000 rw-p 00000000 00:00 0
7fff77830000-7fff77847000 rw-p 00000000 00:00 0
[stack]
7fff778ae000-7fff778af000 r-xp 00000000 00:00 0
[vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0
[vsyscall]
Aborted
I have no idea what caused this problem. I tried to use strcnp instead of
strcmp but even my code doesn't get compiled. How could I get rid of this
error?

No comments:

Post a Comment