簡體   English   中英

***檢測到堆棧粉碎***:使用 HEALPix C 子例程終止中止(核心轉儲)

[英]*** stack smashing detected ***: terminated Aborted (core dumped) using HEALPix C subroutines

我正在嘗試將 python 代碼轉換為 C 代碼。 所以我試圖翻譯那行:

import healpy as hp  
OldTobySensitivMap = hp.read_map('file.fits', dtype=numpy.float64)

這將使用 HEALPIx 投影打開一個 FITS 文件。 所以在 C 我使用的是 HEALPIx 庫:

#include "chealpix.h"
#include "fitsio.h"
#include <string.h>

int main(int argc, char **argv){

        char coordsys[]="G";
        char order[]="NESTED";
        long nside=0;
        float *map;
        map=read_healpix_map("file.fits",&nside,coordsys,order);
        printf("nside:%ld, coordsys:%s, order:%s",nside,coordsys,order);     

return(0);

}

但是,此代碼返回錯誤:

WARNING: Could not find COORDSYS keyword in in file file.fits
*** stack smashing detected ***: terminated
Program received signal SIGABRT, Aborted.

GDB調試器output為:

Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
50  ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1  0x00007ffff76f5859 in __GI_abort () at abort.c:79
#2  0x00007ffff77603ee in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7ffff788a07c "*** %s ***: terminated\n") at ../sysdeps/posix/libc_fatal.c:155
#3  0x00007ffff7802b4a in __GI___fortify_fail (msg=msg@entry=0x7ffff788a064 "stack smashing detected") at fortify_fail.c:26
#4  0x00007ffff7802b16 in __stack_chk_fail () at stack_chk_fail.c:24
#5  0x00005555555556af in main (argc=1, argv=0x7fffffffdd18) at young_pulsar_pop.c:60

增加字符串的大小,以便:

        char coordsys[2024]="G";
        char order[2024]="NESTED";

有效,但是 output 是nside:512, coordsys:, order:RING ,所以我不太明白是什么導致了堆棧錯誤。

就像其他用戶所說的那樣,HEALPIx 接口對於 C 來說似乎很不安全,而且文檔真的很差。 因此,我想知道我是否不應該簡單地將 python 代碼嵌入到我的 C 程序中。

如果您檢查相關文檔

read_healpix_map

此例程從 FITS 文件中讀取滿天 HEALPIx map

HEALPIx 目錄樹中的位置:src/C/subs/read_healpix_map.c

格式 float *read_healpix_map(char *infile, long *nside, char *coordsys, char *ordering)

ARGUMENTS

 | name&dimensionality  | kind | in/out | description     |

   read_healpix_map       float   OUT     array containing the map read from the file 

   infile                 char    IN      FITS file containing a full sky to be read

   nside                   long   OUT     HEALPix resolution parameter of the map

   coordsys                char   OUT     astronomical coordinate system of 
 pixelisation (either 'C', 'E' or 'G' standing respectively for Celestial=equatorial, Ecliptic or Galactic) 

   ordering                char   OUT     HEALPix pixel ordering (either 'RING' or 'NESTED')

我們看到的是coordsysordering是 function 的output參數。 這意味着這個 function 期望具有定義大小的字符緩沖區來存儲相關的 output 字符串。 如本文檔中所述,為coordsys提供的緩沖區應該是ASCII 字符串char [2]類型(將坐標系存儲在一個字符加上 NULL 字節)並且ordering應該是char [7]類型(最長的字符串是'NESTED' , 6 個字符加上 NULL 字節)。

char coordsys[]="G"; char order[]="NESTED"; ,您正在程序的數據部分中定義字符串。 當您使用最長的字符串進行排序時,您應該為兩個字符串留出足夠的空間。

但。

read_healpix_map文檔不准確。 The function will store a TSTRING object in its coordsys and ordering parameters, as its source calls the fits_read_key function which store a TSRING object in theses parameters.

這就是為什么使用“大尺寸”緩沖區有效的原因。

避免任何麻煩的最好方法是直接初始化緩沖區coordsys並使用TSTRING object 為“G”和“NESTED” order fitsio.h header 文檔中,關鍵字的最大長度由FLEN_VALUE定義。 所以我建議你使用:

char coordsys[FLEN_VALUE]="G";
char order[FLEN_VALUE]="NESTED";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM