long 형식의 경우 윈도우즈 Visual C++ 의 경우 32bit 로 처리되고 unix 에서는 64 bit 로 처리된다. max 가 다르니 cast 도 주의해야겠지만 이번에 겪은 문제는 long* 에 int * 주소를 넘기는 바람에 access violation 을 일으켰다.

참고 : https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models

 

64-bit computing - Wikipedia

From Wikipedia, the free encyclopedia Jump to navigation Jump to search Computer architecture bit width "64-bit" redirects here. For 64-bit images in computer graphics, see Deep color. In computer architecture, 64-bit integers, memory addresses, or other d

en.wikipedia.org

http rest api 테스트로 많이 사용하는 curl 이라는 cli 로 더 유명하겠지만 libcurl 이라고 http client library 가 있다.

https://curl.se/

 

curl

command line tool and library for transferring data with URLs (since 1998) Time to donate to the curl project? Everything curl is a detailed and totally free book that explains basically everything there is to know about curl, libcurl and the associated pr

curl.se

curl_easy_getinfo api 에서 CURLINFO_RESPONSE_CODE 에 대해 long* 로 넘긴 주소에 결과값을 전달해 준다.

#include <curl/curl.h>
 
CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ... );
CURL *curl = curl_easy_init();
if(curl) {
  CURLcode res;
  curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
  res = curl_easy_perform(curl);
  if(res == CURLE_OK) {
    long response_code;
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
  }
  curl_easy_cleanup(curl);
}

 

 

출처 : https://curl.se/libcurl/c/curl_easy_getinfo.html

 

libcurl - curl_easy_getinfo()

curl_easy_getinfo - extract information from a curl handle Name curl_easy_getinfo - extract information from a curl handle Synopsis #include   CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ... ); Description Request internal information from the c

curl.se

https://curl.se/libcurl/c/CURLINFO_RESPONSE_CODE.html

 

CURLINFO_RESPONSE_CODE

CURLINFO_RESPONSE_CODE explained Name CURLINFO_RESPONSE_CODE - get the last response code Synopsis #include   CURLcode curl_easy_getinfo(CURL *handle, CURLINFO_RESPONSE_CODE, long *codep); Description Pass a pointer to a long to receive the last received

curl.se

int responseCode = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);

vc++ 에서는 이 코드가 컴파일도 잘 되고 문제가 없었지만 linux 에서는 curl 호출 후 scope 나갈 때 std::string destructor 에서 터지는 이상한 문제가 발생했다.

case CURLINFO_RESPONSE_CODE:
    *param_longp = data->info.httpcode;
    break;

long* 에 int 형 데이터 주소를 넘기니 내부에서 int* 형 주소에 long 형식의 데이터를 write 하다가 int* 다음 영역의 데이터를 오염시켜서 발생하는 문제였다. 

불행히 curl_easy_getinfo 가 가변 변수 함수라 컴파일 타임에 잡아 낼 수 없었다.

예전엔 short, int 같은 형식을 선호했었는데 이제는 int8, int16, int32 와 같이 비트가 명시된 형식을 선호하게 되었다.

728x90

+ Recent posts