Why does free() keep triggering a breakpoint - c

Can someone please tell me why my free() function keeps triggering a breakpoint? I then get a Microsoft Visual C++ Runtime Library error: Debug Assertion failed! Program: ... .exe, File: minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp ... Expression: _CrtlsValidHeapPointer(block).
#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "IPHLPAPI.lib")
int main()
{
IP_ADAPTER_INFO *pAdapterInfo = malloc(sizeof(IP_ADAPTER_INFO));
if (pAdapterInfo == NULL)
{
return 1;
}
ULONG size = sizeof(*pAdapterInfo);
ULONG * pOutBufLen = &size;
if(GetAdaptersInfo(pAdapterInfo, pOutBufLen) == ERROR_BUFFER_OVERFLOW) //When this error code is returned, the pOutBufLen parameter points to the required buffer size.
{
pAdapterInfo = realloc(pAdapterInfo, *pOutBufLen);
}
else
{
return 1;
}
if(GetAdaptersInfo(pAdapterInfo, pOutBufLen) == NO_ERROR)
{
while (pAdapterInfo->Next != NULL)
{
printf("%s\n", pAdapterInfo->Description);
pAdapterInfo = pAdapterInfo->Next;
}
}
free(pAdapterInfo);
getchar();
return 0;
}
SOLUTION: Ok, thanks for everyone for your responses. I fixed my problem with another pointer that I've called pAdapter:
int main()
{
IP_ADAPTER_INFO *pAdapterInfo = malloc(sizeof(IP_ADAPTER_INFO));
IP_ADAPTER_INFO *pAdapter = NULL; //for memory freeing purposes only
...
if(GetAdaptersInfo(pAdapterInfo, pOutBufLen) == NO_ERROR)
{
pAdapter = pAdapterInfo;
while (pAdapter->Next != NULL)
{
printf("%s\n", pAdapter->Description);
pAdapter = pAdapter->Next;
}
}
free(pAdapterInfo);

Related

mach_vm_protect unable to make memory executable

I'm trying to allocate memory dynamically and have it be executable through mach_vm_protect; however any time I try to execute the code the application crashes. But mach_vm_protect succeeds which is what I don't understand.
#include <stdio.h>
#include <unistd.h>
#include <mach/mach_init.h>
#include <mach/vm_map.h>
#include <mach/mach_vm.h>
int test(int x, int y){
return x+y;
}
typedef int (*test_mach_copy)(int,int);
#define CODE_SIZE 0x17
int main()
{
mach_vm_address_t remoteCode64 = (vm_address_t) NULL;
mach_vm_address_t testvmaddr = (vm_address_t)&test;
task_t remotetask;
task_for_pid(mach_task_self(), getpid(), &remotetask);
if (mach_vm_protect(remotetask, testvmaddr, CODE_SIZE, 1, VM_PROT_READ|VM_PROT_EXECUTE)!=KERN_SUCCESS) {
return 1;
}
if(mach_vm_allocate(remotetask,&remoteCode64,CODE_SIZE,VM_FLAGS_ANYWHERE)!=KERN_SUCCESS){
return 1;
}
if (mach_vm_protect(remotetask, remoteCode64, CODE_SIZE, 1, VM_PROT_READ|VM_PROT_EXECUTE|VM_PROT_WRITE|VM_PROT_COPY)!=KERN_SUCCESS) {
return 1;
}
mach_vm_copy(remotetask, testvmaddr, CODE_SIZE, remoteCode64);
test_mach_copy tmc = (test_mach_copy)remoteCode64;
int x = tmc(10,20);
printf("%d\n",x);
return 0;
}
x017 Is size the correct sizeof(test())
The issue is probably your use of VM_PROT_READ|VM_PROT_EXECUTE|VM_PROT_WRITE|VM_PROT_COPY. Modern operating systems, and architectures, try to enforce W^X permissions. That is, either a memory range is executable or writeable, but never both.
There might be a bug in the kernel since your call to mach_vm_protect is returning KERN_SUCCESS.
I was able to get your code working by simply making 2 calls to mach_vm_protect in succession:
int main()
{
mach_vm_address_t remoteCode64 = (vm_address_t) NULL;
mach_vm_address_t testvmaddr = (vm_address_t)&test;
task_t remotetask;
task_for_pid(mach_task_self(), getpid(), &remotetask);
if (mach_vm_protect(remotetask, testvmaddr, CODE_SIZE, 1, VM_PROT_READ|VM_PROT_EXECUTE)!=KERN_SUCCESS) {
return 1;
}
if(mach_vm_allocate(remotetask,&remoteCode64,CODE_SIZE,VM_FLAGS_ANYWHERE)!=KERN_SUCCESS){
return 1;
}
if (mach_vm_protect(remotetask, remoteCode64, CODE_SIZE, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_COPY)!=KERN_SUCCESS) {
return 1;
}
if (mach_vm_copy(remotetask, testvmaddr, CODE_SIZE, remoteCode64) != KERN_SUCCESS) {
return 1;
}
if (mach_vm_protect(remotetask, remoteCode64, CODE_SIZE, 0, VM_PROT_READ|VM_PROT_EXECUTE)!=KERN_SUCCESS) {
return 1;
}
test_mach_copy tmc = (test_mach_copy)remoteCode64;
int x = tmc(10,20);
printf("%d\n",x);
return 0;
}

Trying to write a program that ask the user to input parentheses and brackets. The program will then tell the users whether they are properly nested

As the title states I am trying to write a program that will tell the user if there inputted brackets and or parentheses are properly nested. Here is my code.
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define STACK_SIZE 100
char contents[STACK_SIZE];
int top = 0;
void stack_overflow();
void stack_underflow();
void make_empty(void)
{
top = 0;
}
bool is_empty(void)
{
return top == 0;
}
bool is_full (void)
{
return top == STACK_SIZE;
}
void push(char i)
{
if (is_full())
stack_overflow();
else
contents[top++] = i;
}
char pop(void)
{
if (is_empty())
stack_underflow();
else
return contents[--top];
}
int main(void)
{
char input;
bool is_nested=true;
printf("Enter parentheses and/or braces: ");
while ((input = getchar()) != '\n')
{
if(input =='(' || input == '{')
push(input);
if(input ==')' && pop() != '(')
is_nested = false;
if(input =='}' && pop() != '{')
is_nested = false;
}
if (is_empty() == false) is_nested = false;
if (is_nested)
printf("Parentheses/braces are nested properly");
else
printf("Parentheses/braces are not nested properly");
return 0;
}
When I compile the code I keep getting the error:
C:\Users\g\AppData\Local\Temp\ccy6YhqI.o:brackets.c:(.text+0x45): undefined reference to `stack_overflow'
C:\Users\g\AppData\Local\Temp\ccy6YhqI.o:brackets.c:(.text+0x76): undefined reference to `stack_underflow'
collect2.exe: error: ld returned 1 exit status
I cannot seem to find why I am getting this error. Any help would be appreciated.
clearly you haven't implemented stack_overflow() nor stack_underflow().
void foo(); is not a sufficient function definition.

C segmentation fault prog

I have a problem with segmentation fault. And I am not sure about the arrays.
I need to get output like this
Here is part of my code. (structure and enumeration must stay unchanged)
EDIT: second malloc fix, This is only part of my code where I get segmentation fault, warning while compiling: unused variable ‘title’ and segmentation fault while running
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
typedef enum {
GAME1,
GAME2,
GAME3,
} TITLE;
typedef struct abx {
TITLE **title;
} ABX;
string[]="ABCACC|ACCBAA|BBCABC"
ABX *abx = (ABX*)malloc(sizeof(ABX));
TITLE **title = (TITLE**)malloc(sizeof(TITLE*));
width = height = 0;
while (string[i] !='\0') {
abx->title[height][width]=(TITLE)malloc(sizeof(TITLE*));
if(string[i]=='A'){
abx->title[height][width] = GAME1;
break;
}
if(string[i]=='B'){
abx->title[height][width] = GAME2;
break;
}
if(string[i]=='C'){
abx->title[height][width] = GAME3;
break;
}
if (string[i]=='|'){
height++;
width = 0;
}
else{
width++;
}
i++;
}
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
typedef enum {
GAME1,
GAME2,
GAME3,
} TITLE;
typedef struct abx {
TITLE **title;
} ABX;
int main(){
char string[]="ABCACC|ACCBAA|BBCABC";
ABX *abx = malloc(sizeof(ABX));
int i, len, w, h, width, height;
char ch;
len = width = height = 0;
for(i=0;(ch=string[i]) != '\0';++i){
if(ch == '|'){
++height;
if(width<len)
width = len;
len = 0;
} else
++len;
}
++height;
if(width<len)
width = len;
abx->title =malloc(height*sizeof(TITLE*));
for(h=0;h<height;++h)
abx->title[h]=malloc(width*sizeof(TITLE));
for(h=w=i=0;string[i]!='\0';++i){
if(string[i]=='A'){
abx->title[h][w++] = GAME1;
continue;
}
if(string[i]=='B'){
abx->title[h][w++] = GAME2;
continue;
}
if(string[i]=='C'){
abx->title[h][w++] = GAME3;
continue;
}
if (string[i]=='|'){
++h;
w = 0;
}
}
...
}

How to use fgetpwent()?

I am trying to get a list of all the users in the system (linux, fedora).
and i've heard that the function:fgetpwent is the one that i need to that mission.
the sad part is that i didnt find any documentation or example of how to use this function.
if someone would give me an example, that would be great, thanks in advance.
No idea why I ever could have used it:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <crypt.h>
#include <pwd.h>
#include <sys/types.h>
char *testentry = "testread";
static void read_etc_passwd (void) {
struct passwd *pwd_entry = NULL;
int found = 0;
setpwent(); // go to the top of /etc/passwd
while (!found && (pwd_entry = getpwent())){
if (0 == strcmp (testentry, pwd_entry->pw_name)){
found = 1;
}
}
if (found) {
printf ("name = %s\nhome = %s\n", pwd_entry->pw_name,
pwd_entry->pw_dir);
} else {
puts("could not find the entry you were looking for, or"
"some error occurred");
}
}
void change_etc_passwd (void){
struct passwd *pwd = NULL;
FILE *pwd_fd = NULL;
FILE *pwd_new = NULL;
int result = 0;
pwd_fd = fopen ("/etc/passwd", "r");
pwd_new = fopen ("/tmp/passwd.neu", "a");
// assuming everthing went fine (bad idea)
while (pwd = fgetpwent (pwd_fd)){
if (0 == strcmp (pwd->pw_name, testentry)){
pwd->pw_passwd = crypt ("new_pwd", "aa");
}
result = putpwent(pwd, pwd_new);
if (result < 0){
fprintf (stderr, "Failed to write entry, giving up\n");
exit (EXIT_FAILURE);
}
}
}
int main (void) {
/* handling of /etc/passwd */
read_etc_passwd ();
change_etc_passwd();
return 0;
}
Add error handling and it may even work without breaking ;-)

HarfBuzz - hb_shape() leads to access violation

Based on this example, I try to implement font rendering in my SDL application. When calling hb_shape(), the application is halted because of an access-violation.
DLL-download-link (win32): here {harfbuzz-0.9.26-win32.zip}
ErrorMsg (VC2012): Unhandled exception at 0x6160B7F0 (libharfbuzz-0.dll)in ConsoleApplication2.exe: 0xC0000005: Access violation while reading at position 0x00000068
EDIT: I changed the example to a console application, for simplicity.
Edit2: Now linking statically, .lib-file created with VC++'s LIB.exe.
#include <Windows.h>
#include <iostream>
#include <hb.h>
#include <hb-ft.h>
#pragma comment(lib,"lib/x86/freetype253ST.lib") // freetype single-thread
#pragma comment (lib,"libharfbuzz-0.lib") // linked to libharfbuzz.dll, created by LIB.exe
int main()
{
hb_font_t* font = NULL;
hb_buffer_t* buffer = NULL;
FT_Library flib;
FT_Face face;
bool found = false;
const char text[] = {"Write something...."};
if (FT_Init_FreeType(&flib) == 0)
{
if (FT_New_Face(flib,"arial.ttf",0,&face) == 0)
{
for (int i = 0; i < face->num_charmaps; i++)
{ // search for UNICODE 2.0 charmap
if ((face->charmaps[i]->encoding_id == 3 && face->charmaps[i]->platform_id == 0) ||
(face->charmaps[i]->encoding_id == 1 && face->charmaps[i]->platform_id == 3) )
{
FT_Set_Charmap(face,face->charmaps[i]);
found = true;
break;
}
}
if (found && FT_Set_Char_Size(face,0,50*64,72,72) == 0)
{
font = hb_ft_font_create(face,NULL);
buffer = hb_buffer_create();
if (hb_buffer_allocation_successful(buffer))
{
hb_buffer_set_script(buffer,HB_SCRIPT_LATIN);
hb_buffer_set_language(buffer, hb_language_from_string("en",strlen("en"))); // strlen("en") is awful but good enough here
hb_buffer_set_direction(buffer,HB_DIRECTION_LTR);
hb_buffer_add_utf8(buffer,text,strlen(text),0,strlen(text));
hb_shape(font,buffer,NULL,0); // Access violation
std::cout << "There\n";
hb_buffer_destroy(buffer);
}
hb_font_destroy(font);
}
FT_Done_Face(face);
}
FT_Done_FreeType(flib);
}
Sleep(3000);
return 0;
}

Resources