好吧..我已经制作了一个使用zlib解压缩字符串的系统...我认为它可以工作,但是我的base64转换器有问题,所以我不确定数据是否正确...它适用于一个非常小的字符串,即“帮助”,但是此字符串导致缓冲区溢出
“eJxjZMAOmHCIM + MQZ8EhzgrEAAKAABA =”

this is the function in question!

#include <zlib.h>
#include <stdlib.h>
#include <string.h>
#include "../../inc/tools/Zunz.h"

using namespace tool;

static const int maxUncompressedSize = 4096;

std::string Zunz::UnZip(std::string const& s)
{
    char *cmprsd;
    char uncompressed[maxUncompressedSize];
    int sizeOfS = sizeof(s);
    cmprsd = (char*) malloc(sizeOfS);
    strcpy(cmprsd, s.c_str());

    // deflate
    // zlib struct
    z_stream defstream;
    defstream.zalloc = Z_NULL;
    defstream.zfree = Z_NULL;
    defstream.opaque = Z_NULL;
    defstream.avail_in = (uInt)strlen(cmprsd)+1; // size of input, string + terminator
    defstream.next_in = (Bytef *)cmprsd; // input char array
    defstream.avail_out = (uInt)sizeof(uncompressed); // size of output
    defstream.next_out = (Bytef *)uncompressed; // output char array

    deflateInit(&defstream, Z_DEFAULT_COMPRESSION);
    deflate(&defstream, Z_FINISH);
    deflateEnd(&defstream);

    return(std::string(uncompressed));
}

这段代码在调试中工作正常,但在释放中导致缓冲区溢出。.我认为可能导致此问题的唯一两件事是1. zlib的用法不正确(我对它非常陌生)或2. ive紧随其后指南:http://www.devside.net/guides/linux/zlib

这是输出:
    *** buffer overflow detected ***: ./sfml tests terminated
    ======= Backtrace: =========
    /lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x5c)[0x7f6b81c2182c]
    /lib/x86_64-linux-gnu/libc.so.6(+0x109700)[0x7f6b81c20700]
    ./sfml tests[0x4075d2]
    ./sfml tests[0x406e2a]
    ./sfml tests[0x406865]
    ./sfml tests[0x406339]
    /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f6b81b3876d]
    ./sfml tests[0x406771]
    ======= Memory map: ========
    00400000-0043a000 r-xp 00000000 08:01 788443                             /home/falconmick/sfml tests/bin/Release/sfml tests
    00639000-0063a000 r--p 00039000 08:01 788443                             /home/falconmick/sfml tests/bin/Release/sfml tests
    0063a000-0063b000 rw-p 0003a000 08:01 788443                             /home/falconmick/sfml tests/bin/Release/sfml tests
    0063b000-0063d000 rw-p 00000000 00:00 0
    01382000-013a3000 rw-p 00000000 00:00 0                                  [heap]
    7f6b7fe6d000-7f6b7fe72000 r-xp 00000000 08:01 6007                       /usr/lib/x86_64-linux-gnu/libXdmcp.so.6.0.0
    7f6b7fe72000-7f6b80071000 ---p 00005000 08:01 6007                       /usr/lib/x86_64-linux-gnu/libXdmcp.so.6.0.0
    7f6b80071000-7f6b80072000 r--p 00004000 08:01 6007                       /usr/lib/x86_64-linux-gnu/libXdmcp.so.6.0.0
    7f6b80072000-7f6b80073000 rw-p 00005000 08:01 6007                       /usr/lib/x86_64-linux-gnu/libXdmcp.so.6.0.0
    7f6b80073000-7f6b80075000 r-xp 00000000 08:01 5996                       /usr/lib/x86_64-linux-gnu/libXau.so.6.0.0
    7f6b80075000-7f6b80275000 ---p 00002000 08:01 5996                       /usr/lib/x86_64-linux-gnu/libXau.so.6.0.0
    7f6b80275000-7f6b80276000 r--p 00002000 08:01 5996                       /usr/lib/x86_64-linux-gnu/libXau.so.6.0.0
    7f6b80276000-7f6b80277000 rw-p 00003000 08:01 5996                       /usr/lib/x86_64-linux-gnu/libXau.so.6.0.0
    7f6b80277000-7f6b80280000 r-xp 00000000 08:01 6029                       /usr/lib/x86_64-linux-gnu/libXrender.so.1.3.0
    7f6b80280000-7f6b8047f000 ---p 00009000 08:01 6029                       /usr/lib/x86_64-linux-gnu/libXrender.so.1.3.0
    7f6b8047f000-7f6b80480000 r--p 00008000 08:01 6029                       /usr/lib/x86_64-linux-gnu/libXrender.so.1.3.0
    7f6b80480000-7f6b80481000 rw-p 00009000 08:01 6029                       /usr/lib/x86_64-linux-gnu/libXrender.so.1.3.0
    7f6b80481000-7f6b80483000 r-xp 00000000 08:01 921657                     /lib/x86_64-linux-gnu/libdl-2.15.so
    7f6b80483000-7f6b80683000 ---p 00002000 08:01 921657                     /lib/x86_64-linux-gnu/libdl-2.15.so
    7f6b80683000-7f6b80684000 r--p 00002000 08:01 921657                     /lib/x86_64-linux-gnu/libdl-2.15.so
    7f6b80684000-7f6b80685000 rw-p 00003000 08:01 921657                     /lib/x86_64-linux-gnu/libdl-2.15.so
    7f6b80685000-7f6b8068f000 r-xp 00000000 08:01 6180                       /usr/lib/x86_64-linux-gnu/libdrm.so.2.4.0
    7f6b8068f000-7f6b8088f000 ---p 0000a000 08:01 6180                       /usr/lib/x86_64-linux-gnu/libdrm.so.2.4.0
    7f6b8088f000-7f6b80890000 r--p 0000a000 08:01 6180                       /usr/lib/x86_64-linux-gnu/libdrm.so.2.4.0
    7f6b80890000-7f6b80891000 rw-p 0000b000 08:01 6180                       /usr/lib/x86_64-linux-gnu/libdrm.so.2.4.0
    7f6b80891000-7f6b80895000 r-xp 00000000 08:01 6039                       /usr/lib/x86_64-linux-gnu/libXxf86vm.so.1.0.0
    7f6b80895000-7f6b80a95000 ---p 00004000 08:01 6039                       /usr/lib/x86_64-linux-gnu/libXxf86vm.so.1.0.0
    7f6b80a95000-7f6b80a96000 r--p 00004000 08:01 6039                       /usr/lib/x86_64-linux-gnu/libXxf86vm.so.1.0.0
    7f6b80a96000-7f6b80a97000 rw-p 00005000 08:01 6039                       /usr/lib/x86_64-linux-gnu/libXxf86vm.so.1.0.0
    7f6b80a97000-7f6b80ab4000 r-xp 00000000 08:01 6749                       /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0
    7f6b80ab4000-7f6b80cb3000 ---p 0001d000 08:01 6749                       /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0
    7f6b80cb3000-7f6b80cb4000 r--p 0001c000 08:01 6749                       /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0
    7f6b80cb4000-7f6b80cb5000 rw-p 0001d000 08:01 6749                       /usr/lib/x86_64-linux-gnu/libxcb.so.1.1.0
    7f6b80cb5000-7f6b80cca000 r-xp 00000000 08:01 6739                       /usr/lib/x86_64-linux-gnu/libxcb-glx.so.0.0.0
    7f6b80cca000-7f6b80ec9000 ---p 00015000 08:01 6739                       /usr/lib/x86_64-linux-gnu/libxcb-glx.so.0.0.0
    7f6b80ec9000-7f6b80ecb000 r--p 00014000 08:01 6739                       /usr/lib/x86_64-linux-gnu/libxcb-glx.so.0.0.0
    7f6b80ecb000-7f6b80ecc000 rw-p 00016000 08:01 6739                       /usr/lib/x86_64-linux-gnu/libxcb-glx.so.0.0.0
    7f6b80ecc000-7f6b80ecd000 r-xp 00000000 08:01 5990                       /usr/lib/x86_64-linux-gnu/libX11-xcb.so.1.0.0
    7f6b80ecd000-7f6b810cc000 ---p 00001000 08:01 5990                       /usr/lib/x86_64-linux-gnu/libX11-xcb.so.1.0.0
    7f6b810cc000-7f6b810cd000 r--p 00000000 08:01 5990                       /usr/lib/x86_64-linux-gnu/libX11-xcb.so.1.0.0
    7f6b810cd000-7f6b810ce000 rw-p 00001000 08:01 5990                       /usr/lib/x86_64-linux-gnu/libX11-xcb.so.1.0.0
    7f6b810ce000-7f6b810d3000 r-xp 00000000 08:01 6011                       /usr/lib/x86_64-linux-gnu/libXfixes.so.3.1.0
    7f6b810d3000-7f6b812d2000 ---p 00005000 08:01 6011                       /usr/lib/x86_64-linux-gnu/libXfixes.so.3.1.0
    7f6b812d2000-7f6b812d3000 r--p 00004000 08:01 6011                       /usr/lib/x86_64-linux-gnu/libXfixes.so.3.1.0
    7f6b812d3000-7f6b812d4000 rw-p 00005000 08:01 6011                       /usr/lib/x86_64-linux-gnu/libXfixes.so.3.1.0
    7f6b812d4000-7f6b812d6000 r-xp 00000000 08:01 6005                       /usr/lib/x86_64-linux-gnu/libXdamage.so.1.1.0
    7f6b812d6000-7f6b814d5000 ---p 00002000 08:01 6005                       /usr/lib/x86_64-linux-gnu/libXdamage.so.1.1.0
    7f6b814d5000-7f6b814d6000 r--p 00001000 08:01 6005                       /usr/lib/x86_64-linux-gnu/libXdamage.so.1.1.0
    7f6b814d6000-7f6b814d7000 rw-p 00002000 08:01 6005                       /usr/lib/x86_64-linux-gnu/libXdamage.so.1.1.0
    7f6b814d7000-7f6b814e7000 r-xp 00000000 08:01 6009                       /usr/lib/x86_64-linux-gnu/libXext.so.6.4.0
    7f6b814e7000-7f6b816e7000 ---p 00010000 08:01 6009                       /usr/lib/x86_64-linux-gnu/libXext.so.6.4.0
    7f6b816e7000-7f6b816e8000 r--p 00010000 08:01 6009                       /usr/lib/x86_64-linux-gnu/libXext.so.6.4.0
    7f6b816e8000-7f6b816e9000 rw-p 00011000 08:01 6009                       /usr/lib/x86_64-linux-gnu/libXext.so.6.4.0
    7f6b816e9000-7f6b8170a000 r-xp 00000000 08:01 6280                       /usr/lib/x86_64-linux-gnu/libglapi.so.0.0.0
    7f6b8170a000-7f6b8190a000 ---p 00021000 08:01 6280                       /usr/lib/x86_64-linux-gnu/libglapi.so.0.0.0
    7f6b8190a000-7f6b8190d000 r--p 00021000 08:01 6280                       /usr/lib/x86_64-linux-gnu/libglapi.so.0.0.0
    7f6b8190d000-7f6b8190e000 rw-p 00024000 08:01 6280                       /usr/lib/x86_64-linux-gnu/libglapi.so.0.0.0
    7f6b8190e000-7f6b8190f000 rw-p 00000000 00:00 0
    7f6b8190f000-7f6b81916000 r-xp 00000000 08:01 921746                     /lib/x86_64-linux-gnu/librt-2.15.so
    7f6b81916000-7f6b81b15000 ---p 00007000 08:01 921746                     /lib/x86_64-linux-gnu/librt-2.15.so
    7f6b81b15000-7f6b81b16000 r--p 00006000 08:01 921746                     /lib/x86_64-linux-gnu/librt-2.15.so
    7f6b81b16000-7f6b81b17000 rw-p 00007000 08:01 921746                     /lib/x86_64-linux-gnu/librt-2.15.so
    7f6b81b17000-7f6b81ccc000 r-xp 00000000 08:01 921642                     /lib/x86_64-linux-gnu/libc-2.15.so
    7f6b81ccc000-7f6b81ecb000 ---p 001b5000 08:01 921642                     /lib/x86_64-linux-gnu/libc-2.15.so
    7f6b81ecb000-7f6b81ecf000 r--p 001b4000 08:01 921642                     /lib/x86_64-linux-gnu/libc-2.15.so
    7f6b81ecf000-7f6b81ed1000 rw-p 001b8000 08:01 921642                     /lib/x86_64-linux-gnu/libc-2.15.so
    7f6b81ed1000-7f6b81ed6000 rw-p 00000000 00:00 0
    7f6b81ed6000-7f6b81eeb000 r-xp 00000000 08:01 921667                     /lib/x86_64-linux-gnu/libgcc_s.so.1
    7f6b81eeb000-7f6b820ea000 ---p 00015000 08:01 921667                     /lib/x86_64-linux-gnu/libgcc_s.so.1
    7f6b820ea000-7f6b820eb000 r--p 00014000 08:01 921667                     /lib/x86_64-linux-gnu/libgcc_s.so.1
    7f6b820eb000-7f6b820ec000 rw-p 00015000 08:01 921667                     /lib/x86_64-linux-gnu/libgcc_s.so.1
    7f6b820ec000-7f6b821e7000 r-xp 00000000 08:01 921684                     /lib/x86_64-linux-gnu/libm-2.15.so
    7f6b821e7000-7f6b823e6000 ---p 000fb000 08:01 921684                     /lib/x86_64-linux-gnu/libm-2.15.so
    7f6b823e6000-7f6b823e7000 r--p 000fa000 08:01 921684                     /lib/x86_64-linux-gnu/libm-2.15.so
    7f6b823e7000-7f6b823e8000 rw-p 000fb000 08:01 921684                     /lib/x86_64-linux-gnu/libm-2.15.so
    7f6b823e8000-7f6b824cd000 r-xp 00000000 08:01 6655                       /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
    7f6b824cd000-7f6b826cc000 ---p 000e5000 08:01 6655                       /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
    7f6b826cc000-7f6b826d4000 r--p 000e4000 08:01 6655                       /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
    7f6b826d4000-7f6b826d6000 rw-p 000ec000 08:01 6655                       /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
    7f6b826d6000-7f6b826eb000 rw-p 00000000 00:00 0
    7f6b826eb000-7f6b82704000 r-xp 00000000 08:01 1065917                    /usr/local/lib/libz.so.1.2.5
    7f6b82704000-7f6b82903000 ---p 00019000 08:01 1065917                    /usr/local/lib/libz.so.1.2.5
    7f6b82903000-7f6b82904000 r--p 00018000 08:01 1065917                    /usr/local/lib/libz.so.1.2.5
    7f6b82904000-7f6b82905000 rw-p 00019000 08:01 1065917                    /usr/local/lib/libz.so.1.2.5
    7f6b82905000-7f6b82944000 r-xp 00000000 08:01 6426                       /usr/lib/x86_64-linux-gnu/libjpeg.so.8.0.2
    7f6b82944000-7f6b82b44000 ---p 0003f000 08:01 6426                       /usr/lib/x86_64-linux-gnu/libjpeg.so.8.0.2
    7f6b82b44000-7f6b82b45000 r--p 0003f000 08:01 6426                       /usr/lib/x86_64-linux-gnu/libjpeg.so.8.0.2
    7f6b82b45000-7f6b82b46000 rw-p 00040000 08:01 6426                       /usr/lib/x86_64-linux-gnu/libjpeg.so.8.0.2
    7f6b82b46000-7f6b82b56000 rw-p 00000000 00:00 0
    7f6b82b56000-7f6b82bce000 r-xp 00000000 08:01 5921                       /usr/lib/x86_64-linux-gnu/libGLEW.so.1.8.0
    7f6b82bce000-7f6b82dcd000 ---p 00078000 08:01 5921                       /usr/lib/x86_64-linux-gnu/libGLEW.so.1.8.0
    7f6b82dcd000-7f6b82dd3000 r--p 00077000 08:01 5921                       /usr/lib/x86_64-linux-gnu/libGLEW.so.1.8.0
    7f6b82dd3000-7f6b82dd4000 rw-p 0007d000 08:01 5921                       /usr/lib/x86_64-linux-gnu/libGLEW.so.1.8.0
    7f6b82dd4000-7f6b82dd8000 rw-p 00000000 00:00 0
    7f6b82dd8000-7f6b82de1000 r-xp 00000000 08:01 6027                       /usr/lib/x86_64-linux-gnu/libXrandr.so.2.2.0
    7f6b82de1000-7f6b82fe0000 ---p 00009000 08:01 6027                       /usr/lib/x86_64-linux-gnu/libXrandr.so.2.2.0
    7f6b82fe0000-7f6b82fe1000 r--p 00008000 08:01 6027                       /usr/lib/x86_64-linux-gnu/libXrandr.so.2.2.0
    7f6b82fe1000-7f6b82fe2000 rw-p 00009000 08:01 6027                       /usr/lib/x86_64-linux-gnu/libXrandr.so.2.2.0
    7f6b82fe2000-7f6b83116000 r-xp 00000000 08:01 5992                       /usr/lib/x86_64-linux-gnu/libX11.so.6.3.0
    7f6b83116000-7f6b83316000 ---p 00134000 08:01 5992                       /usr/lib/x86_64-linux-gnu/libX11.so.6.3.0
    7f6b83316000-7f6b83317000 r--p 00134000 08:01 5992                       /usr/lib/x86_64-linux-gnu/libX11.so.6.3.0
    7f6b83317000-7f6b8331c000 rw-p 00135000 08:01 5992                       /usr/lib/x86_64-linux-gnu/libX11.so.6.3.0
    7f6b8331c000-7f6b8337c000 r-xp 00000000 08:01 264024                     /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0
    7f6b8337c000-7f6b8357c000 ---p 00060000 08:01 264024                     /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0
    7f6b8357c000-7f6b8357e000 r--p 00060000 08:01 264024                     /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0
    7f6b8357e000-7f6b8357f000 rw-p 00062000 08:01 264024                     /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.2.0
    7f6b8357f000-7f6b83580000 rw-p 00000000 00:00 0
    7f6b83580000-7f6b83598000 r-xp 00000000 08:01 921738                     /lib/x86_64-linux-gnu/libpthread-2.15.so
    7f6b83598000-7f6b83797000 ---p 00018000 08:01 921738                     /lib/x86_64-linux-gnu/libpthread-2.15.so
    7f6b83797000-7f6b83798000 r--p 00017000 08:01 921738                     /lib/x86_64-linux-gnu/libpthread-2.15.so
    7f6b83798000-7f6b83799000 rw-p 00018000 08:01 921738                     /lib/x86_64-linux-gnu/libpthread-2.15.so
    7f6b83799000-7f6b8379d000 rw-p 00000000 00:00 0
    7f6b8379d000-7f6b837bf000 r-xp 00000000 08:01 921620                     /lib/x86_64-linux-gnu/ld-2.15.so
    7f6b8399d000-7f6b839aa000 rw-p 00000000 00:00 0
    7f6b839bc000-7f6b839bf000 rw-p 00000000 00:00 0
    7f6b839bf000-7f6b839c0000 r--p 00022000 08:01 921620                     /lib/x86_64-linux-gnu/ld-2.15.so
    7f6b839c0000-7f6b839c2000 rw-p 00023000 08:01 921620                     /lib/x86_64-linux-gnu/ld-2.15.so
    7fff110d9000-7fff110fa000 rw-p 00000000 00:00 0                          [stack]
    7fff1110e000-7fff1110f000 r-xp 00000000 00:00 0                          [vdso]
    ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
    Aborted (core dumped)

最佳答案

问题在于,通过使用sizeof(s),您将获得std::string类的大小,而不是需要输入的字符串。尝试替换此:

int sizeOfS = sizeof(s);

为了这:
int sizeOfS = s.length() + 1;

“+1”用于空终止符,它自动附加到c_str()

关于c++ - 在 Release模式下检测到缓冲区溢出-Linux,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13572144/

10-09 16:55