deallocate시 문자열 길이 단위는 0x10이며, 문자열 길이가 0x10이하면 _Count 에는 1이 들어가고, _Deallocate를 호출하면서 0x10 * 1하여 0x10 사이즈로 deallocate를 하게 된다.

예시)  string abc = "this is";

_Ptr은 이중 포인터이며, &abc의 주소를 담고 있다.

길이가 0x10을 초과하는 경우 param에 블록단위(0x10의 배수)가 들어간다.

예시) string abc = "live a live is not fun"; // 22바이트

 

긴 사이즈에서 짧은 사이즈로 리사이즈 하는 경우, 최초에 할당한 길이로 deallocate가 한 번만 수행된다.

string abc = "live a live is not fun";
abc = "papa";

vc++에서는 문자열의 길이를 deallocate의 인자로 확인할 수 없으므로, deallocate 재정의를 통한 memory wipe는 수행할 수 없다.

namespace secure
{
    template <class T> class allocator : public std::allocator<T>
    {
    public:

        template<class U> struct rebind { typedef allocator<U> other; };
        allocator() throw() {}
        allocator(const allocator&) throw() {}
        template <class U> allocator(const allocator<U>&) throw() {}

        void deallocate(T* const p, size_t _Count)
        {            
            memset((void*)p, 0, _Count); // _Count가 문자열 길이를 뜻하는 것이 아니다                    
            std::allocator<T>::deallocate(p, _Count);
        }
    };

    typedef std::basic_string<char, std::char_traits<char>, allocator<char> > string;
}

 

'Windows > Dev' 카테고리의 다른 글

detours 빌드 및 적용  (0) 2024.04.11
[Visual Studio 2022] curl 빌드  (0) 2023.01.30
get EIP (gcc / vc)  (0) 2019.04.11
C용 초경량 XML 파서 : Mini-XML 소개 및 사용법  (0) 2017.06.29
x86 __usercall 함수 후킹하기  (0) 2017.05.11

+ Recent posts