我正在开发一个将R与mdsplib连接的程序包,以便解析METAR报告。它在这里可用:https://github.com/jwijffels/RMETAR,您可以将其安装在R中。

require(devtools)
install_github("RMETAR", "jwijffels", subdir="/pkg")


R包基本上使用.Call执行mdsplib中提供的名为DcdMETAR的C函数。到目前为止,一切都很好。这适用于99.99%的METAR案件。使用一些奇怪的METAR代码,我崩溃了,指出:*检测到缓冲区溢出*。
以下是可重现的示例。

require(RMETAR)
givesbufferoverflow <- c(
  "MPTO 270000Z 00000KT 9999 FEW008SCT018CB BKN090 27/24 Q1010 RETSRA RWY DAMP",
  "GBYD 271100Z 09004KT 9999 BKN012?FEW018CB SCT120 29/24 Q1013 CB(NW-W)",
  "HKKI 262300Z 00000KT 9999 FEW019=BKN080 20/19 Q1017")
decodemetar(givesbufferoverflow[1])


那么该怎么办?我按照R-ext中所述运行valgrind。上面的R代码放在一个名为i_hate_memory_management.R的文件中,然后运行:

R -d "valgrind --tool=memcheck --leak-check=full" --vanilla < i_hate_memory_management.R


现在,这给了我以下信息:
有人可以帮助我确定此消息的含义。字符串似乎是内存使用问题,但这实际上是什么意思,我该如何解决?

> require(RMETAR)
Loading required package: RMETAR
> givesbufferoverflow <- c(
+   "MPTO 270000Z 00000KT 9999 FEW008SCT018CB BKN090 27/24 Q1010 RETSRA RWY DAMP",
+   "GBYD 271100Z 09004KT 9999 BKN012?FEW018CB SCT120 29/24 Q1013 CB(NW-W)",
+   "HKKI 262300Z 00000KT 9999 FEW019=BKN080 20/19 Q1017")
> decodemetar(givesbufferoverflow[1])
*** buffer overflow detected ***: /usr/lib/R/bin/exec/R terminated
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x37)[0x571d817]
/lib/x86_64-linux-gnu/libc.so.6(+0x109710)[0x571c710]
/lib/x86_64-linux-gnu/libc.so.6(+0x1089f6)[0x571b9f6]
/home/janw/R/x86_64-pc-linux-gnu-library/2.15/RMETAR/libs/RMETAR.so(+0xa589)[0x105a9589]
/home/janw/R/x86_64-pc-linux-gnu-library/2.15/RMETAR/libs/RMETAR.so(+0xa873)[0x105a9873]
/home/janw/R/x86_64-pc-linux-gnu-library/2.15/RMETAR/libs/RMETAR.so(DcdMETAR+0x1503)[0x105ab233]
/home/janw/R/x86_64-pc-linux-gnu-library/2.15/RMETAR/libs/RMETAR.so(decodeMETAR+0x279)[0x105a7669]
/usr/lib/R/lib/libR.so(+0xb9f08)[0x4eebf08]
/usr/lib/R/lib/libR.so(Rf_eval+0x73d)[0x4f25bcd]
/usr/lib/R/lib/libR.so(+0xf57a0)[0x4f277a0]
/usr/lib/R/lib/libR.so(Rf_eval+0x51f)[0x4f259af]
/usr/lib/R/lib/libR.so(+0xf5920)[0x4f27920]
/usr/lib/R/lib/libR.so(Rf_eval+0x51f)[0x4f259af]
/usr/lib/R/lib/libR.so(Rf_applyClosure+0x34d)[0x4f28e0d]
/usr/lib/R/lib/libR.so(Rf_eval+0x400)[0x4f25890]
/usr/lib/R/lib/libR.so(Rf_ReplIteration+0x1e3)[0x4f61fa3]
/usr/lib/R/lib/libR.so(+0x130230)[0x4f62230]
/usr/lib/R/lib/libR.so(run_Rmainloop+0x5a)[0x4f622ca]
/usr/lib/R/bin/exec/R(main+0x1b)[0x40078b]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x563476d]
/usr/lib/R/bin/exec/R[0x4007bd]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:05 6948377                            /usr/lib/R/bin/exec/R
00600000-00601000 r--p 00000000 08:05 6948377                            /usr/lib/R/bin/exec/R
00601000-00602000 rw-p 00001000 08:05 6948377                            /usr/lib/R/bin/exec/R
04000000-04022000 r-xp 00000000 08:05 10489457                           /lib/x86_64-linux-gnu/ld-2.15.so
04022000-04023000 rw-s 00000000 08:05 4456681                            /tmp/vgdb-pipe-shared-mem-vgdb-5873-by-janw-on-???
04023000-04026000 rw-p 00000000 00:00 0
04026000-04027000 rw-s 00000000 00:04 11927559                           /SYSV00000000 (deleted)
04027000-04028000 rw-s 00000000 00:04 5046278                            /SYSV00510510 (deleted)
04028000-04029000 rw-p 00000000 00:00 0
04029000-04030000 r--s 00000000 08:05 6964289                            /usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache
04030000-04031000 r--p 00000000 08:05 9581751                            /usr/share/R/share/locale/en/LC_MESSAGES/R.mo
04031000-04033000 rw-p 00000000 00:00 0
04046000-0404e000 rw-p 00000000 00:00 0
04222000-04223000 r--p 00022000 08:05 10489457                           /lib/x86_64-linux-gnu/ld-2.15.so
04223000-04225000 rw-p 00023000 08:05 10489457                           /lib/x86_64-linux-gnu/ld-2.15.so
04225000-04226000 rwxp 00000000 00:00 0
04a25000-04a26000 r-xp 00000000 08:05 6698284                            /usr/lib/valgrind/vgpreload_core-amd64-linux.so
04a26000-04c25000 ---p 00001000 08:05 6698284                            /usr/lib/valgrind/vgpreload_core-amd64-linux.so
04c25000-04c26000 r--p 00000000 08:05 6698284                            /usr/lib/valgrind/vgpreload_core-amd64-linux.so
04c26000-04c27000 rw-p 00001000 08:05 6698284                            /usr/lib/valgrind/vgpreload_core-amd64-linux.so
04c27000-04c31000 r-xp 00000000 08:05 6698333                            /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so
04c31000-04e30000 ---p 0000a000 08:05 6698333                            /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so
04e30000-04e31000 r--p 00009000 08:05 6698333                            /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so
04e31000-04e32000 rw-p 0000a000 08:05 6698333                            /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so
04e32000-050f5000 r-xp 00000000 08:05 7083949                            /usr/lib/R/lib/libR.so
050f5000-052f5000 ---p 002c3000 08:05 7083949                            /usr/lib/R/lib/libR.so
052f5000-052fb000 r--p 002c3000 08:05 7083949                            /usr/lib/R/lib/libR.so
052fb000-0530b000 rw-p 002c9000 08:05 7083949                            /usr/lib/R/lib/libR.so
0530b000-053f6000 rw-p 00000000 00:00 0
053f6000-0540e000 r-xp 00000000 08:05 10485801                           /lib/x86_64-linux-gnu/libpthread-2.15.so
0540e000-0560d000 ---p 00018000 08:05 10485801                           /lib/x86_64-linux-gnu/libpthread-2.15.so
0560d000-0560e000 r--p 00017000 08:05 10485801                           /lib/x86_64-linux-gnu/libpthread-2.15.so
0560e000-0560f000 rw-p 00018000 08:05 10485801                           /lib/x86_64-linux-gnu/libpthread-2.15.so
0560f000-05613000 rw-p 00000000 00:00 0
05613000-057c8000 r-xp 00000000 08:05 10485799                           /lib/x86_64-linux-gnu/libc-2.15.so
057c8000-059c7000 ---p 001b5000 08:05 10485799                           /lib/x86_64-linux-gnu/libc-2.15.so
059c7000-059cb000 r--p 001b4000 08:05 10485799                           /lib/x86_64-linux-gnu/libc-2.15.so
059cb000-059cd000 rw-p 001b8000 08:05 10485799                           /lib/x86_64-linux-gnu/libc-2.15.so
059cd000-059d2000 rw-p 00000000 00:00 0
059d2000-06c54000 r-xp 00000000 08:05 6815808                            /usr/lib/openblas-base/libopenblas.so.0
06c54000-06e54000 ---p 01282000 08:05 6815808                            /usr/lib/openblas-base/libopenblas.so.0
06e54000-06e5b000 r--p 01282000 08:05 6815808                            /usr/lib/openblas-base/libopenblas.so.0
06e5b000-06e69000 rw-p 01289000 08:05 6815808                            /usr/lib/openblas-base/libopenblas.so.0
06e69000-06e71000 rw-p 00000000 00:00 0
06e71000-06f6c000 r-xp 00000000 08:05 10486670                           /lib/x86_64-linux-gnu/libm-2.15.so
06f6c000-0716b000 ---p 000fb000 08:05 10486670                           /lib/x86_64-linux-gnu/libm-2.15.so
0716b000-0716c000 r--p 000fa000 08:05 10486670                           /lib/x86_64-linux-gnu/libm-2.15.so
0716c000-0716d000 rw-p 000fb000 08:05 10486670                           /lib/x86_64-linux-gnu/libm-2.15.so
0716d000-071a6000 r-xp 00000000 08:05 10489427                           /lib/x86_64-linux-gnu/libreadline.so.6.2
071a6000-073a6000 ---p 00039000 08:05 10489427                           /lib/x86_64-linux-gnu/libreadline.so.6.2
073a6000-073a8000 r--p 00039000 08:05 10489427                           /lib/x86_64-linux-gnu/libreadline.so.6.2
073a8000-073ae000 rw-p 0003b000 08:05 10489427                           /lib/x86_64-linux-gnu/libreadline.so.6.2
073ae000-073af000 rw-p 00000000 00:00 0
073af000-073eb000 r-xp 00000000 08:05 10489417                           /lib/x86_64-linux-gnu/libpcre.so.3.12.1
073eb000-075ea000 ---p 0003c000 08:05 10489417                           /lib/x86_64-linux-gnu/libpcre.so.3.12.1
075ea000-075eb000 r--p 0003b000 08:05 10489417                           /lib/x86_64-linux-gnu/libpcre.so.3.12.1
075eb000-075ec000 rw-p 0003c000 08:05 10489417                           /lib/x86_64-linux-gnu/libpcre.so.3.12.1
075ec000-0760d000 r-xp 00000000 08:05 6692207                            /usr/lib/x86_64-linux-gnu/liblzma.so.5.0.0
0760d000-0780c000 ---p 00021000 08:05 6692207                            /usr/lib/x86_64-linux-gnu/liblzma.so.5.0.0
0780c000-0780d000 r--p 00020000 08:05 6692207                            /usr/lib/x86_64-linux-gnu/liblzma.so.5.0.0
0780d000-0780e000 rw-p 00021000 08:05 6692207                            /usr/lib/x86_64-linux-gnu/liblzma.so.5.0.0
0780e000-0781d000 r-xp 00000000 08:05 10489343                           /lib/x86_64-linux-gnu/libbz2.so.1.0.4
0781d000-07a1c000 ---p 0000f000 08:05 10489343                           /lib/x86_64-linux-gnu/libbz2.so.1.0.4
07a1c000-07a1d000 r--p 0000e000 08:05 10489343                           /lib/x86_64-linux-gnu/libbz2.so.1.0.4
07a1d000-07a1e000 rw-p 0000f000 08:05 10489343                           /lib/x86_64-linux-gnu/libbz2.so.1.0.4
07a1e000-07a34000 r-xp 00000000 08:05 10489455                           /lib/x86_64-linux-gnu/libz.so.1.2.3.4
07a34000-07c33000 ---p 00016000 08:05 10489455                           /lib/x86_64-linux-gnu/libz.so.1.2.3.4
07c33000-07c34000 r--p 00015000 08:05 10489455                           /lib/x86_64-linux-gnu/libz.so.1.2.3.4
07c34000-07c35000 rw-p 00016000 08:05 10489455                           /lib/x86_64-linux-gnu/libz.so.1.2.3.4
07c35000-07c3c000 r-xp 00000000 08:05 10485803                           /lib/x86_64-linux-gnu/librt-2.15.so
07c3c000-07e3b000 ---p 00007000 08:05 10485803                           /lib/x86_64-linux-gnu/librt-2.15.so
07e3b000-07e3c000 r--p 00006000 08:05 10485803                           /lib/x86_64-linux-gnu/librt-2.15.so
07e3c000-07e3d000 rw-p 00007000 08:05 10485803                           /lib/x86_64-linux-gnu/librt-2.15.so
07e3d000-07e3f000 r-xp 00000000 08:05 10485798                           /lib/x86_64-linux-gnu/libdl-2.15.so
07e3f000-0803f000 ---p 00002000 08:05 10485798                           /lib/x86_64-linux-gnu/libdl-2.15.so
0803f000-08040000 r--p 00002000 08:05 10485798                           /lib/x86_64-linux-gnu/libdl-2.15.so
08040000-08041000 rw-p 00003000 08:05 10485798                           /lib/x86_64-linux-gnu/libdl-2.15.so
08041000-0804e000 r-xp 00000000 08:05 6692082                            /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0
0804e000-0824d000 ---p 0000d000 08:05 6692082                            /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0
0824d000-0824e000 r--p 0000c000 08:05 6692082                            /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0
0824e000-0824f000 rw-p 0000d000 08:05 6692082                            /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0
0824f000-08271000 r-xp 00000000 08:05 10489441                           /lib/x86_64-linux-gnu/libtinfo.so.5.9
08271000-08471000 ---p 00022000 08:05 10489441                           /lib/x86_64-linux-gnu/libtinfo.so.5.9
08471000-08475000 r--p 00022000 08:05 10489441                           /lib/x86_64-linux-gnu/libtinfo.so.5.9
08475000-08476000 rw-p 00026000 08:05 10489441                           /lib/x86_64-linux-gnu/libtinfo.so.5.9
08476000-08876000 rwxp 00000000 00:00 0
08876000-08f59000 r--p 00000000 08:05 6691142                            /usr/lib/locale/locale-archive
08f59000-09359000 rwxp 00000000 00:00 0
09359000-09361000 r-xp 00000000 08:05 10489326                           /lib/x86_64-linux-gnu/libnss_compat-2.15.so
09361000-09560000 ---p 00008000 08:05 10489326                           /lib/x86_64-linux-gnu/libnss_compat-2.15.so
09560000-09561000 r--p 00007000 08:05 10489326                           /lib/x86_64-linux-gnu/libnss_compat-2.15.so
09561000-09562000 rw-p 00008000 08:05 10489326                           /lib/x86_64-linux-gnu/libnss_compat-2.15.so
09562000-09579000 r-xp 00000000 08:05 10489462                           /lib/x86_64-linux-gnu/libnsl-2.15.so
09579000-09778000 ---p 00017000 08:05 10489462                           /lib/x86_64-linux-gnu/libnsl-2.15.so
09778000-09779000 r--p 00016000 08:05 10489462                           /lib/x86_64-linux-gnu/libnsl-2.15.so
09779000-0977a000 rw-p 00017000 08:05 10489462                           /lib/x86_64-linux-gnu/libnsl-2.15.so
0977a000-0977c000 rw-p 00000000 00:00 0
0977c000-09786000 r-xp 00000000 08:05 10485832                           /lib/x86_64-linux-gnu/libnss_nis-2.15.so
09786000-09986000 ---p 0000a000 08:05 10485832                           /lib/x86_64-linux-gnu/libnss_nis-2.15.so
09986000-09987000 r--p 0000a000 08:05 10485832                           /lib/x86_64-linux-gnu/libnss_nis-2.15.so
09987000-09988000 rw-p 0000b000 08:05 10485832                           /lib/x86_64-linux-gnu/libnss_nis-2.15.so
09988000-09994000 r-xp 00000000 08:05 10489325                           /lib/x86_64-linux-gnu/libnss_files-2.15.so
09994000-09b93000 ---p 0000c000 08:05 10489325                           /lib/x86_64-linux-gnu/libnss_files-2.15.so
09b93000-09b94000 r--p 0000b000 08:05 10489325                           /lib/x86_64-linux-gnu/libnss_files-2.15.so
09b94000-09b95000 rw-p 0000c000 08:05 10489325                           /lib/x86_64-linux-gnu/libnss_files-2.15.so
09b95000-09f95000 rwxp 00000000 00:00 0
09f95000-09f9d000 r-xp 00000000 08:05 9710069                            /usr/lib/R/library/methods/libs/methods.so
09f9d000-0a19c000 ---p 00008000 08:05 9710069                            /usr/lib/R/library/methods/libs/methods.so
0a19c000-0a19d000 r--p 00007000 08:05 9710069                            /usr/lib/R/library/methods/libs/methods.so
0a19d000-0a19e000 rw-p 00008000 08:05 9710069                            /usr/lib/R/library/methods/libs/methods.so
0a393000-0c393000 rw-p 00000000 00:00 0
0c393000-0cb93000 rwxp 00000000 00:00 0
0cb93000-0cb95000 r-xp 00000000 08:05 6964391                            /usr/lib/x86_64-linux-gnu/gconv/ISO8859-1.so
0cb95000-0cd94000 ---p 00002000 08:05 6964391                            /usr/lib/x86_64-linux-gnu/gconv/ISO8859-1.so
0cd94000-0cd95000 r--p 00001000 08:05 6964391                            /usr/lib/x86_64-linux-gnu/gconv/ISO8859-1.so
0cd95000-0cd96000 rw-p 00002000 08:05 6964391                            /usr/lib/x86_64-linux-gnu/gconv/ISO8859-1.so
0cd96000-0d996000 rwxp 00000000 00:00 0
0d996000-0d9bd000 r-xp 00000000 08:05 9709626                            /usr/lib/R/library/grDevices/libs/grDevices.so
0d9bd000-0dbbd000 ---p 00027000 08:05 9709626                            /usr/lib/R/library/grDevices/libs/grDevices.so
0dbbd000-0dbbe000 r--p 00027000 08:05 9709626                            /usr/lib/R/library/grDevices/libs/grDevices.so
0dbbe000-0dbbf000 rw-p 00028000 08:05 9709626                            /usr/lib/R/library/grDevices/libs/grDevices.so
0dbbf000-0ebbf000 rwxp 00000000 00:00 0
0ebbf000-0ec43000 r-xp 00000000 08:05 9701761                            /usr/lib/R/library/stats/libs/stats.so
0ec43000-0ee42000 ---p 00084000 08:05 9701761                            /usr/lib/R/library/stats/libs/stats.so
0ee42000-0ee44000 r--p 00083000 08:05 9701761                            /usr/lib/R/library/stats/libs/stats.so
0ee44000-0ee46000 rw-p 00085000 08:05 9701761                            /usr/lib/R/library/stats/libs/stats.so
0ee46000-0f72b000 r-xp 00000000 08:05 9571291                            /usr/lib/lapack/liblapack.so.3gf.0
0f72b000-0f92a000 ---p 008e5000 08:05 9571291                            /usr/lib/lapack/liblapack.so.3gf.0
0f92a000-0f92b000 r--p 008e4000 08:05 9571291                            /usr/lib/lapack/liblapack.so.3gf.0
0f92b000-0f92f000 rw-p 008e5000 08:05 9571291                            /usr/lib/lapack/liblapack.so.3gf.0
0f92f000-0fa3c000 rw-p 00000000 00:00 0
0fa3c000-0fb50000 r-xp 00000000 08:05 6691211                            /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0
0fb50000-0fd50000 ---p 00114000 08:05 6691211                            /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0
0fd50000-0fd51000 r--p 00114000 08:05 6691211                            /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0==5873==
==5873== HEAP SUMMARY:
==5873==     in use at exit: 28,551,157 bytes in 12,623 blocks
==5873==   total heap usage: 31,354 allocs, 18,731 frees, 48,823,198 bytes allocated
==5873==
==5873== 300 (60 direct, 240 indirect) bytes in 1 blocks are definitely lost in loss record 101 of 1,544
==5873==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5873==    by 0x57185A4: nss_parse_service_list (nsswitch.c:678)
==5873==    by 0x5719065: __nss_database_lookup (nsswitch.c:175)
==5873==    by 0x935D623: ???
==5873==    by 0x56D1C0C: getpwuid_r@@GLIBC_2.2.5 (getXXbyYY_r.c:256)
==5873==    by 0x56D1502: getpwuid (getXXbyYY.c:117)
==5873==    by 0x4F94108: ??? (in /usr/lib/R/lib/libR.so)
==5873==    by 0x4F18930: ??? (in /usr/lib/R/lib/libR.so)
==5873==    by 0x4F25609: Rf_eval (in /usr/lib/R/lib/libR.so)
==5873==    by 0x4F28E0C: Rf_applyClosure (in /usr/lib/R/lib/libR.so)
==5873==    by 0x4F21431: ??? (in /usr/lib/R/lib/libR.so)
==5873==    by 0x4F25609: Rf_eval (in /usr/lib/R/lib/libR.so)
==5873==
==5873== 300 (60 direct, 240 indirect) bytes in 1 blocks are definitely lost in loss record 102 of 1,544
==5873==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5873==    by 0x57185A4: nss_parse_service_list (nsswitch.c:678)
==5873==    by 0x5719065: __nss_database_lookup (nsswitch.c:175)
==5873==    by 0x935B3BB: ???
==5873==    by 0x56D022C: getgrgid_r@@GLIBC_2.2.5 (getXXbyYY_r.c:256)
==5873==    by 0x56CF972: getgrgid (getXXbyYY.c:117)
==5873==    by 0x4F94134: ??? (in /usr/lib/R/lib/libR.so)
==5873==    by 0x4F18930: ??? (in /usr/lib/R/lib/libR.so)
==5873==    by 0x4F25609: Rf_eval (in /usr/lib/R/lib/libR.so)
==5873==    by 0x4F28E0C: Rf_applyClosure (in /usr/lib/R/lib/libR.so)
==5873==    by 0x4F21431: ??? (in /usr/lib/R/lib/libR.so)
==5873==    by 0x4F25609: Rf_eval (in /usr/lib/R/lib/libR.so)
==5873==
==5873== LEAK SUMMARY:
==5873==    definitely lost: 120 bytes in 2 blocks
==5873==    indirectly lost: 480 bytes in 20 blocks
==5873==      possibly lost: 0 bytes in 0 blocks
==5873==    still reachable: 28,550,557 bytes in 12,601 blocks
==5873==         suppressed: 0 bytes in 0 blocks
==5873== Reachable blocks (those to which a pointer was found) are not shown.
==5873== To see them, rerun with: --leak-check=full --show-reachable=yes
==5873==
==5873== For counts of detected and suppressed errors, rerun with: -v
==5873== Use --track-origins=yes to see where uninitialised values come from
==5873== ERROR SUMMARY: 421 errors from 10 contexts (suppressed: 2 from 2)
Aborted (core dumped)

最佳答案

对。我使用一些printf语句跟踪了该错误。
基本上我必须在mdsplib库的dcdmetar.c的parseCldData中替换以下代码



strncpy(Mptr->cldTypHgt[next].other_cld_phenom,token+6, (strlen(token)-6));




strncpy(Mptr->cldTypHgt[next].other_cld_phenom,token+6, min(4, (strlen(token)-6)));


如Mptr-> cldTypHgt [next]。other_cld_phenom似乎是char other_cld_phenom [4]类型;
在我的METAR案例中,云现象的宽度大于6个字符。
只需将错误发送给mdsplib的作者即可。

现在,当回顾valgrind输出时,这根本没有显示问题的原因。我想知道是否有比使用printf更好的调试方法。

09-05 18:50