C/Code snippet
[C/코드해석] pop_string
2022. 5. 26. 01:34반응형
코드 원문
/**
* Helper function which corrects the string pointers
* in a stroke_msg_t. Strings in a stroke_msg sent over "wire"
* contains RELATIVE addresses (relative to the beginning of the
* stroke_msg). They must be corrected if they reach our address
* space...
*/
static void pop_string(stroke_msg_t *msg, char **string)
{
if (*string == NULL)
{
return;
}
/* check for sanity of string pointer and string */
if (string < (char**)msg ||
string > (char**)((char*)msg + sizeof(stroke_msg_t)) ||
(unsigned long)*string < (unsigned long)((char*)msg->buffer - (char*)msg) ||
(unsigned long)*string > msg->length)
{
*string = "(invalid pointer in stroke msg)";
}
else
{
*string = (char*)msg + (unsigned long)*string;
}
}
사용 예제
struct stroke_end_t {
char *auth;
char *auth2;
char *id;
char *id2;
char *eap_id;
char *rsakey;
char *cert;
char *cert2;
char *ca;
char *ca2;
char *groups;
char *groups2;
char *cert_policy;
char *updown;
char *address
static void pop_string(stroke_msg_t *msg, char **string)
* Pop the strings of a stroke_end_t struct and log them for debugging purposes
*/
static void pop_end(stroke_msg_t *msg, const char* label, stroke_end_t *end)
{
pop_string(msg, &end->address);
코드 해석
//
static void pop_string(stroke_msg_t *msg, char **string)
{
if (*string == NULL)
{
return;
}
/* check for sanity of string pointer and string */
if (string < (char**)msg ||
string > (char**)((char*)msg + sizeof(stroke_msg_t)) ||
(unsigned long)*string < (unsigned long)((char*)msg->buffer - (char*)msg) ||
(unsigned long)*string > msg->length)
{
*string = "(invalid pointer in stroke msg)";
}
else
{
*string = (char*)msg + (unsigned long)*string;
}
}
반응형
'C > Code snippet' 카테고리의 다른 글
[C] 문자열 파싱 (strtok없이), strcpy, strdup, strlen (0) | 2022.07.10 |
---|---|
[C] exec - execl 예제, c언어 내부에서 ls 같은 시스템 명령어 실행하기 (0) | 2022.06.03 |
[C][Linux] realloc, malloc 그리고 malloc 사이즈 구하기 (0) | 2022.05.22 |
[C] 가변 리스트 (pack된 속성)에 대한 고찰, Variable list of packed attribute (0) | 2022.05.22 |
[C] 버블정렬 (0) | 2022.05.19 |