Posts

Showing posts with the label C Language

C Language – Code Analysis Practice (Intermediate Examples)

1. Intermediate C Code Analysis Examples 1.1. Pointer Size and String Length Understand how a character pointer that points to a string literal behaves. Confirm that sizeof and strlen calculate values using different criteria . #include <stdio.h> #include <string.h> int main ( void ) { char * text = " 0123456789 " ; size_t result = sizeof (text) + strlen (text); printf ( "%zu\n" , result); return 0 ; } char *text is not an array that stores the string contents; it is a pointer that stores the starting address of a string literal. sizeof(text) is the size of the pointer variable itself, which is different from the length of the string it points to. strlen(text) counts the number of characters from the location the pointer references until it reaches '\0' . The size of a pointer can vary depending on the execution environment, and this explanation uses a 64-bit environment as its basis. 1.2. Poi...

C Language – Function Arguments Using Pointers

Image
1. Function Arguments in C Programming languages pass function arguments using either Call by Value or Call by Reference .  However, in C, function arguments are always passed by Call by Value .  In other words, the value passed to a function is a  copy , so the function cannot directly access the original variable. For this reason, C achieves a Call by Reference effect by using pointers to pass a variable's address as a value. 1.1. Function Argument Passing Methods Method What is passed Can change original Call by value The variable's value No Call by reference The variable's  address Yes 1.1.1. Call by Value Call by Value is a method that  passes a copy of the value to the function . Therefore, even if the value is changed inside the function, the original variable is not affected. 1.1.2. Call by Reference Call by Reference is a method that uses a pointer to...

C Language – Understanding Buffer Overflow Attacks and Weaknesses

Image
1. Buffer Overflow 1.1. Security Vulnerabilities 1.1.1. Causes Buffer Overflow is a security vulnerability where data is written beyond the size of a buffer, overwriting adjacent memory. When data is written past the buffer boundary, adjacent memory gets overwritten . 1.1.2. Impact and Risks The program may crash abnormally or produce errors. Critical data may be corrupted, causing unexpected behavior . An attacker may manipulate memory to execute arbitrary code or take control of the system . 1.2. Stack Buffer Overflow Stack Buffer Overflow is a vulnerability in which data is written beyond the size of a buffer allocated in the Stack area, overwriting adjacent memory regions. The main causes are insufficient input length validation and the use of unsafe functions that do not perform bounds checks. # include <stdio.h> # include <string.h> # include <stdlib.h> // Demonstrate stack buffer overflow: // Stac...

C Language – Understanding Pointers

Image
1. C Language Pointers A pointer in C is a variable that stores a memory address. It stores the "location" (address) where data resides . #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { const char *pstr = "Hello World!"; char *ptr = NULL ; const size_t len = strlen (pstr); ptr = malloc (len + 1); memset (ptr, 0x00, len + 1); memcpy (ptr, pstr, len); printf ("%p[%p] → %s\n", &ptr, ptr, ptr); free (ptr); return 0; } The ptr pointer variable resides in the Stack region . Using malloc , a memory space is allocated in the Heap region , and its address is stored in the Stack region ptr variable. Via memcpy , the "Hello World!" string data is copied into the allocated space in the Heap region . 0x7ffffc12fd50[0x5a14a05396b0] → Hello World! &ptr is the address of the pointer varia...