Sign in to follow this  
programering

Array memory scope violation

Recommended Posts

I'm working on my basecode in C and have a problem with my filepath directory code. I've debugged my code and came to that it is the unsigned char array 'add_pre_dir_length' which is addressed within the memory scope of the char array 'filedirpath'. With the code:
#include "filepath.h"
#include "file.h"
#include <string.h>
#include <stdio.h>

/*
char *get_data_directory(void)
{
	long size;
	char *data_directory = 0;
	FILE *file_ptr = fopen("Data.dir","r");

	if (file_ptr)
	{
		size = get_file_size(file_ptr);
		data_directory = (char *) malloc(size);
		fread(data_directory,size,1,file_ptr);
		fclose(file_ptr);
	}

	return data_directory;
}



char *get_data_directory(void)
{
	FILE *file_ptr = fopen("Data.dir","r");

	if (file_ptr)
	{
		data_directory = get_file_char("Data.dir");
	}
}
*/


enum
{
	_base_file_directory_app,
	_base_file_directory_data
};


unsigned char current_directory;

struct file_directory *file_directory_ptr;
unsigned char file_directory_count;


unsigned char change_after_set_def_dir;
unsigned char add_directory_index;


void set_file_directories(struct file_directory *dirs, unsigned int count)
{
	file_directory_ptr = dirs;

	if (file_directory_ptr)
	{
		file_directory_count = count;
		file_directory_ptr[_base_file_directory_data].name = get_file_char("Data.dir");

		current_directory = _base_file_directory_app;
		change_after_set_def_dir = 0;
		add_directory_index = 0;
	}
}




#define MAX_NUM_ADD_DIRS 16


void set_defined_directory(unsigned int directory)
{
	struct file_directory *file_dir_ptr;
	struct file_directory *next_dir_ptr;
	static char *temp[MAX_NUM_ADD_DIRS];
	int i;

	if (file_directory_ptr
	&&	(current_directory != directory
	||  !change_after_set_def_dir)
	&&	_base_file_directory_app < directory
	&&	directory < file_directory_count)
	{
		file_dir_ptr = file_directory_ptr + directory;

		for (i = 0; i < MAX_NUM_ADD_DIRS; i++)
		{
			unsigned int parent = file_dir_ptr->parent;

			temp[i] = file_dir_ptr->name;
			if (parent == 0)
				break;
			else
			{	// To prevent an endless loop:
				next_dir_ptr = file_directory_ptr + parent;

				if (next_dir_ptr == file_dir_ptr)
					break;
				else
					file_dir_ptr = next_dir_ptr;
			}
		}

		if (0 < i)
		{
			set_file_directory(temp[i]);
			while (i > 0)
				add_file_directory(temp[--i]);
		}

		current_directory = directory;
		change_after_set_def_dir = 0;
	//	add_directory_index = 0;
	}
	else
		set_file_directory(0);
}




char filedirpath[64];  // <---- Here.



void ensure_end_slash(void)
{
	char last_filedirpath_char = filedirpath[strlen(filedirpath)-1];

	switch (last_filedirpath_char)
	{
	case '/':
	case '\\':
		break;
	default:
		strcat(filedirpath,"/");
		break;
	}
}


void set_file_directory(char *name)
{
	if (name)
	{
		strcpy(filedirpath,name);
		ensure_end_slash();
		change_after_set_def_dir = 1;
	}
}



unsigned char add_pre_dir_length[MAX_NUM_ADD_DIRS];  // <---- Here.


void add_file_directory(char *name)
{
	unsigned char length;

	fprintf(stdout,"add_file_directory(\"%s\")-------------\n",name);

//	fprintf(stdout,"if (name:%p && add_directory_index:%d < MAX_NUM_ADD_DIRS:%d)\n{\n",name,add_directory_index,MAX_NUM_ADD_DIRS);
	if (name && add_directory_index < MAX_NUM_ADD_DIRS)
	{
		length = strlen(filedirpath);
		fprintf(stdout,"	length:%d = strlen(filedirpath\"%s\");\n",length,filedirpath);

		add_pre_dir_length[add_directory_index] = length;
	//	fprintf(stdout,"	add_pre_dir_length[add_directory_index:%d] = length:%d;\n",add_directory_index,length);
		
	//	fprintf(stdout,"	strcat(filedirpath,name);\n");
		strcat(filedirpath,name);
	//	fprintf(stdout,"	filedirpath:%d\n",filedirpath);
		
	//	fprintf(stdout,"	ensure_end_slash();		");
		ensure_end_slash();
	//	fprintf(stdout,"	filedirpath:%d\n",filedirpath);

	//	fprintf(stdout,"	change_after_set_def_dir = 1;		");
		change_after_set_def_dir = 1;
	//	fprintf(stdout,"	change_after_set_def_dir:%d\n",change_after_set_def_dir);

	//	fprintf(stdout,"	add_directory_index++;		");
		add_directory_index++;
	//	fprintf(stdout,"	add_directory_index:%d\n",add_directory_index);
	}
//	else
	//	fprintf(stdout,"Not entered.\n");

//	fprintf(stdout,"}\n---------------------------------------\n\n");
	fprintf(stdout,"---------------------------------------\n\n");
}


void sub_last_directory(void)
{
	unsigned char length;

	if (0 < add_directory_index)
	{
		length = add_pre_dir_length[--add_directory_index];
		filedirpath[length] = 0;
	}
}


char filepath[64];

char *get_filepath(char *filename)
{
	if (filename)
	{
		strcpy(filepath,filedirpath);
		strcat(filepath,filename);
	}

	return filepath;
}





How can this be? And how do I get them not to violate?

Share this post


Link to post
Share on other sites

size = get_file_size(file_ptr);
data_directory = (char *) malloc(size);
fread(data_directory,size,1,file_ptr);




Is it possible that you are not taking into account the null-terminator somewhere (for example here)?

Share this post


Link to post
Share on other sites
Quote:
Original post by visitor
*** Source Snippet Removed ***

Is it possible that you are not taking into account the null-terminator somewhere (for example here)?


Isn't that code commented:

/*
char *get_data_directory(void)
{
long size;
char *data_directory = 0;
FILE *file_ptr = fopen("Data.dir","r");

if (file_ptr)
{
size = get_file_size(file_ptr);
data_directory = (char *) malloc(size);
fread(data_directory,size,1,file_ptr);
fclose(file_ptr);
}

return data_directory;
}



char *get_data_directory(void)
{
FILE *file_ptr = fopen("Data.dir","r");

if (file_ptr)
{
data_directory = get_file_char("Data.dir");
}
}
*/



Share this post


Link to post
Share on other sites
Without reading all the lots of code: You declare filedirpath before add_pre_dir_length and you seem to add chars to filedirpath. You only reserved 64 chars for this (you must include the terminanting NULL character \0 in those 64 chars). So once you write chars to filedirpath beyond those 64 chars you start corrupting add_pre_dir_length.

Make sure your arrays are big enough and also make sure that you do not append more chars to the arrays than you have room for.

Share this post


Link to post
Share on other sites
Quote:
Original post by Waterwalker
Without reading all the lots of code: You declare filedirpath before add_pre_dir_length and you seem to add chars to filedirpath. You only reserved 64 chars for this (you must include the terminanting NULL character \0 in those 64 chars). So once you write chars to filedirpath beyond those 64 chars you start corrupting add_pre_dir_length.

Make sure your arrays are big enough and also make sure that you do not append more chars to the arrays than you have room for.

Ok. I got it now. Thanks++.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this