realloc()의 오류 ... ?

서지훈의 이미지

음... 뭔가 이상하군요.
realloc()을 하면은 문자열이 오염이 되어 garbage 값이 들어가게 됩니다.

    159             temp = (j_vector - ni_vector) + replace_len;
    160
    161         LOG_PRINT("ret_ = [%p][%d][%s]\n", ret_str, strlen(ret_str), ret_str);
    162
    163             if ((ret_str = (char *) realloc((char *) ret_str, temp)) == NULL) {
    164                 LOG_PRINT("malloc(): %s\n", strerror(errno));
    165
    166                 if (ret_str)
    167                     free(ret_str);
    168
    169                 return NULL;
    170             }
    171
    172         LOG_PRINT("ret_ = [%p][%d][%s]\n", ret_str, strlen(ret_str), ret_str);
    173
    174             strncat(ret_str, (pc_str + nj_vector), i_vector - nj_vector);
    175             strcat(ret_str, pc_replace);

실행결과 ...

JPCRE replace test ...
 ## [jpcre.c:pcre_replace():161]: ret_ = [0x80581d0][8][abcdeaaa]
 ## [jpcre.c:pcre_replace():172]: ret_ = [0x80581d0][8][abcdeaaa]
 ## [jpcre.c:pcre_replace():161]: ret_ = [0x80581d0][16][abcdeaaafghijaaa]
 ## [jpcre.c:pcre_replace():172]: ret_ = [0x80581d0][16][abcdeaaafghijaaa]
 ## [jpcre.c:pcre_replace():161]: ret_ = [0x80581d0][28][abcdeaaafghijaaaklmn abcdaaa]
 ## [jpcre.c:pcre_replace():172]: ret_ = [0x80581d0][28][abcdeaaafghijaaaklmn!abcdaaa]
[:] matched ...[abcdeaaafghijaaaklmn!abcdaaaefghijaaaklmn]

이게 일시적인게 아니라...
다른 컴에서도 같은 형상이 일어 나네요... ㅋㅋ

예전에도 socket()프로그래밍 하면서 socket descriptor가 마음되로 변하는 문제가 있었는데 ㅡㅡㅋ (이때또 처음 부터 다시 완전히 다 짰는데 ㅡㅡㅋ)

또 이런 황당한 일이 ?

역시 컴은 믿을게 못되는 군요.
인간이 문제인가 ???

<어떠한 역경에도 굴하지 않는 '하양 지훈'>

익명 사용자의 이미지

realloc은 free 한다음 malloc 하는것과 완전히 같기 때문에 포인터 위치가 유지된다는 보장이 없을텐데요.

서지훈의 이미지

제가 하면서 실수를 했군요.
저 temp값이 전체 사이트가 아니라...
추가될 사이즈라 잡은 메모리의 크기가 줄었다 늘었다 춤을 춘것이군요.

원래 realloc()시 크기를 키우는 경우는 별 문제는 없지만 줄일 경우에는 그 범위 밖의 값에 대해선 책임을 안지는게 당연한 것인데 ㅡㅡㅋ

함 ...

<어떠한 역경에도 굴하지 않는 '하양 지훈'>

#include <com.h> <C2H5OH.h> <woman.h>
do { if (com) hacking(); if (money) drinking(); if (women) loving(); } while (1);

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.