Stack smashing detected during fread on binary file in C - c

UPDATE IN BOTTOM====
So a while ago I made the following function, which I successfully used to get the grey values from images (w x h dimension) that were converted to .bin-files. It just gives an array of all pixel values.
It was, however, not as a function like this but put in the main() immediately.
// read the BIN-file as grayscale image
void decodeBIN(const char* filename, short image[], int w, int h){
int i = 0;
unsigned char buffer[16]; // no specific size attributed
FILE *ptr;
ptr = fopen(filename, "rb");
if (!ptr){
printf("\nUnable to open file!\n"); // error
}
while (!feof(ptr)){
fread(buffer,2,1,ptr); // read w bytes to buffer
image[i] = buffer[1];
//printf("%u ", image[i]); // DEBUG
i++;
}
fclose(ptr);
printf("\nBinary image read (npixels: %i).\n", i-1); // DEBUG
}
I decided to expand the code, so I rewrote it to the previous function and put it in a separate file for functions and also made a header file. The extra file for functions and the header file work 100% so that's not the issue. Now, this code does not work anymore and I get a stack smashing error. Some variables called after this function have also jumped to another value, so I figured the problem was with the buffer (I didn't know about the correct size for the buffer, but it worked...). After some experimentation and testing, I came up with the following function. I replaced the buffer with a char array named image2 to simply try and test it:
void decodeBIN(const char* filename, short image[], int w, int h){
int i = 0, res;
char image2[];
FILE *ptr;
ptr = fopen(filename, "rb"); //"MySnap_20180327-2239-010.bin"
if (!ptr){
printf("\nUnable to open file!\n"); // error
}
res = fread(image2,1,w*h,ptr) // need to read w*h pixels
while (i < w*h){ // DEBUG
printf("%i ", (int)image2[i]); // DEBUG
i++;
}
printf("\nRead %u bytes\n", res); // DEBUG
fclose(ptr);
printf("Binary image read (npixels: %i).\n", i); // DEBUG
}
I'm a bit lost in how it used to work and all of a sudden when I move the code from main() to a function it stops working, so any help is welcome!
Thanks in advance.
Disclaimer: I'm aiming to write this with the help of as few libraries as possible
===== UPDATE:
After the answer of #alainmerigot I got this code, which helped with getting the correct values:
void decodeBIN(const char* filename, unsigned char image[], int w, int h){
int i = 0, res;
FILE *ptr;
res = fread(image,sizeof(char),w*h,ptr) // need to read w*h pixels
fclose(ptr);
}
The segmentation fault and jumped variables are still in place though, so here a more upper-level oversight of what I'm doing:
char filenamePathed["[path of file]/file.bin"];
short img1[npixels]; // npixels = w*h
printf("i_file: %i\n", i_file); // correct value
decodeBIN(filenamePathed, img_curr, w, h); // decode bin
printf("i_file: %i\n", i_file); // value jumped
while (i < npixels){
img1[i] = (short)img_curr[i];
i++;
}
Perhaps it is good to know that I'm doing this iteratively for multiple files (time series)? I also need it to end up in a (short) format (or integer, but eventually needs to be memory-efficient and pixels have a range of 0-255 so int is a bit abundant imo).

The problem with your second function is that you write in array image2 while no space has been reserved for it. Declaring char image2[]; only says that an array exists and that the address of this array can be found in var image2, but no space is associated with it, hence the problem.
You can associate space with this array by several means.
Using permanent storage in the heap
image2 = malloc(x*y); // but do not forget to free(image2) at the end of the function
Using temporary storage in the stack (space is automatically freed when leaving the function).
image2 = alloca(x*y); // slightly faster than malloc and do not require to free() the image
But the best is to use a array with parametrized size (since C99). Your array should be declared as
char image2[w*h]; // will use the value of w and h to define array size
If you want to do other things than printing the image values in your function, you should store the image in permanent memory and have a mean to know the address of the array in your program. This is probably what you intended and is the reason why you have short image[] in your parameter list.
The solution is just simply to use image instead of image2 in fread().
But, the declaration of image should be coherent and image should be an array of char not short.
Beware also of declarations. In your first function, the image is an array of unsigned char and in the second an array of char. While the storage size is identical and fread() will store the same values, they are not equivalent. If used in an arithmetic context, image[i] will be interpreted differently and the results will likely be different. In general, images are unsigned.

Apparently, the problem was with the allocation of image, although I'm not sure why it was wrong.
I used to allocate it with unsigned char image[npixels]; and the solution to the error appeared to be unsigned char image[npixels*7];
Somehow it works, but if anyone has an explanation, please do so :)

Related

C: Saving dynamic array of structs along with string

I am trying to save a struct (listed)
typedef struct tupleStruct{
int element[eMax];
char * id;
int eCount;
}tuple_t;
typedef struct {
tuple_t * array;
int used;
int size;
} DynamicArray;
As part of an assignment I was instructed to save tuples that are stored in a dynamic array in a file. Unfortunately since strings don't exist in c (at least not like they do in other languages). Whenever I try to save an element of the dynamic array in a file, the string is not stored or loaded properly as it's seen as a pointer. I've even tried by initializing it like so in the struct:
char id[256];
Is there any way possible to save the struct and the string in a single file? (Given that I need to store multiple of these)
Edit: Saving and loading code
Loading
DynamicArray loadAllTuples(){
FILE *filePointer;
DynamicArray tempArray;
if((filePointer=fopen("SavedTuples.bin","rb"))==NULL)
{
fputs("Something went wrong while loading!\nA blank Array will be loaded instead\n", stderr);
setbuf(stdout, 0);
//In case of error, blank array is initalised and loaded
fclose(filePointer);
intialiseDynamicArray(&tempArray);
return tempArray;
}
fread(&tempArray, sizeof(DynamicArray),1,filePointer);
//Freeing filePointer memory
free(filePointer);
return tempArray;
}
saving
void saveAllTuples(DynamicArray ToSave){
trimArray(&ToSave,0); //Removing extra space from array
FILE * filePointer;
if((filePointer=fopen("SavedTuples.bin","wb"))==NULL)
{
fputs("Something went wrong while saving!\n", stderr);
setbuf(stdout, 0);
return;
}
fwrite(&ToSave, sizeof(ToSave), 1,filePointer);
fclose(filePointer);
}
called by
saveAllTuples(dynaArray);
and
dynaArray=loadAllTuples();
Instead of writing the whole struct in one go, write out the various parts of it utilising your knowledge of what they contain. So for example, if eCount is the amount of values in element you could write this
fwrite(&ToSave.eCount,sizeof(int), 1, filepointer);
fwrite(ToSave.element,sizeof(int), ToSave.eCount, filepointer);
and then to store the string component
size_t length=strlen(ToSave.id);
fwrite(&length,sizeof(int), 1, filepointer);
fwrite(ToSave.id,sizeof(char), length, filepointer);
Note: sizeof(char) is typically always 1, so you could assume that and put 1 rather than sizeof(char) but I find it makes the code look more uniform to leave it in.
And then you reverse the process when you do the reading
fwrite(&ToLoad.eCount,sizeof(int), 1, filepointer);
fwrite(ToLoad.element,sizeof(int), ToLoad.eCount, filepointer);
// etc...
To whom it may help in the future: So after asking around a bit, some people got it to work by treating the identifier as an array instead of a pointer.
char id[256];
I said this didn't work before, but it was probably due to another mistake which I didn't spot. Saving each tuple will keep the string identifier intact as long as an array is used.

C - fprintf() & printf() deleting array element memory

I fairly new to C Programming, but fprintf() & printf() is behaving strangely and I'm so confused on why--I need some help understanding and diagnosing this issue.
fprintf() Deleting Element of Array
First off, I'm passing in a populated malloc allocated four element char** array into a simple function that will write to a file, everything in the array appears normal and all four elements contain the correct data. The function call in main() looks like this. My array in question is header.
Note: I had to cast this normal (char** array) as a constant in this function parameter, due to the function header parameter. Our professor gave us the header file and we cannot change anything in them.
pgmWrite((const char**) header, (const int**) matrix,
rowPixels, colPixels, outFile);
Next, stopping debugger just before it executes the fprintf() & printf() functions, screenshot showing the array is still populated with my 4 elements.
pgmWrite() - Showing array is still fine
Observe the 4th element of the array after execution of fprintf().
After fprintf() executes, element 3 memory is wiped out.
When run, printf() executes the printing of the array exactly what is shown in the debugger, ending at the 3rd element. Often printing nothing in that spot or in rare cases garbage characters. The behavior of printf() is exactly the same as how fprintf() is working as well.
I'm at a loss here guys, please help me understand what I'm doing wrong. I can only provide these two screenshots, based on me being a new member. I'll try to provide as much information as possible. Thank you. Here is a simplified version of my program. Keep in mind, the professor gave us the function declarations and told us we cannot change them. So, I have to work with what I have here. Also, since this is fileIO, you need to find a *.pgm file to test this.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define rowsInHeader 4
#define maxSizeHeadRow 200
int ** pgmRead( char **header, int *numRows, int *numCols, FILE *in ){
// INITIALIZING
char *headArr[rowsInHeader][maxSizeHeadRow];
char buffer[100];
int r = 0;
fpos_t pos;
// CREATE: Header
while (r < 4){
// IF: Row in pgm file header lists the dimensions of matrix
if (r == 2){
// CURSOR: Saving pointer location in file (see notes in header for method reference)
fgetpos(in, &pos);
// ASSIGN: Dereference column and row pointers from file
fscanf(in, "%d %d", numCols, numRows);
// CURSOR: Moving back to saved pointer location (see notes in header for method reference)
fsetpos(in, &pos);
}
// ASSIGN: Copying header row into array
fgets(buffer, maxSizeHeadRow, in);
strcpy((char*)headArr[r], buffer);
// POINTER: Reference pointer to headArr[]
header[r] = (char*)headArr[r];
// TRAVERSE: To next row in file
r++;
}
// NOTE: Placeholder for return type
return 0;
}
int pgmWrite( const char **header, const int **pixels, int numRows, int numCols, FILE *out ){
// INITIALIZING
int i = 0;
// WRITE: Header
for (i = 0; i < rowsInHeader; i++){
fprintf(out, "%s", header[i]);
printf("%s", header[i]);
}
return 0;
}
int main(int argc, char *argv[]){
char **header = (char**)malloc(rowsInHeader * sizeof(char));
FILE *inFile = fopen("smallFile.pgm", "r");
FILE *outFile = fopen("TestPicture.ascii.pgm", "w");;
int rowPixels = 0;
int colPixels = 0;
int **matrix = NULL;
// READ & WRITE
matrix = pgmRead(header, &rowPixels, &colPixels, inFile);
pgmWrite((const char**)header, (const int**)matrix, rowPixels, colPixels, outFile);
// FINALIZING
fclose(inFile);
free(header);
return 0;
}
You are not allocating your array correctly. This line:
char **header = (char**)malloc(rowsInHeader * sizeof(char));
makes header point to an uninitialized region of memory , size 4 bytes.
Then inside your PGM function you write:
header[r] = (char*)headArr[r];
The code header[r] means to access the r'th pointer stored in the space pointed to by headArr. But since that space is only 4 bytes big, you're actually writing off into the wild blue yonder.
Also, (char *)headArr[r] is a mistake. If you did not use the cast, your compiler would have warned you about this mistake. You should avoid using casts in your code, especially using them to make warnings go away. You're saying to the compiler "Ssh, I know what I'm doing" when in fact you don't know what you are doing.
The entire approach with headArr is flawed from the start: even if you had actually written the right code to implement what you were trying, you'd be returning pointers into space which is deallocated when the function returns.
Basically the whole pgmRead function is a complete mess and it'd be easier to start from scratch. But this time, think carefully about when and where you are allocating memory, and what the types are of your expressions, and don't use casts. Let the pgmRead function do all the allocation.
(Unfortunately, based on your description it looks like you will have to use your casts to call the pgmWrite function since that has a mistake in its signature. const int ** should be const int * const *, and similarly for const char **. I'd recommend to actually change pgmWrite's signature accordingly, get your program working, and then once everything is good, then go back to the broken version that you are forced to use.)
Reading C FAQ - arrays and pointers might be useful too.

C File input & output

I made a simple dictionary code, and surfed for an hour, and I found a file I/O code. But my compiler(I use Microsoft visual C++) says my code(unfortunately, the core part of the code) is wrong. but I can't get it. What is actually wrong and why???
/*
DosDic ver 1.0.0 2015-07-03
*/
#include<stdio.h>
#include<string.h>
char key = 0;
FILE *fp; //set a file pointer var
fp = fopen("dicdata.dat","r"); //open a file
int b = 0;
int trial = 0;
char result[];
char searchfor[] = fp; //save a whole list of dictionary in a var
int i;
char sb[]; //var for search in a list
int getsearchtrial(char sb[]){
for(i=0;i=strlen(sb);i++){ //how much I tried to reach to the word
switch((int)searchfor[b]-(int)sb[i]){ //are two alphabets same?
case 0 :
default : i=0;
}
b++; //keep finding in a list
trial++; //try again
}
return trial;
}
int adress;
int mainpage(){
printf("Type what you want to search : ");
scanf("%c",sb[ ]);
getsearchtrial(sb[ ]) - strlen(sb[ ]) = adress; //where the word is located in the list
for(i = adress;i = adress + 30; i++){ //print
printf("%c",searchfor[i]);
}
printf("Thank you for using DosDic ver.1.0!"); //thank you!
}
void main(){ //call all those functions
mainpage();
fclose(fp); //close list
}
//and whats wrong with this? i cant even get it, but it's not working
Multiple issues.
First of all, you can't assign the result of fopen to fp outside the body of a function; you must move fp = fopen("dicdata.dat", "r"); to within the body of one of your functions (getsearchtrial most likely).
Secondly, you don't read from a file by simply assigning a file pointer to an object; you must use a library function like fscanf or fread or fgets. Assuming your file contains a single string of length 80, you'd need to write something like
char searchfor[81] = {0}; // make sure string is initially empty,
// extra space for string terminator
if ( !fgets( searchfor, sizeof searchfor, fp) )
{
// error reading from file
}
Of course, this depends on how your input file is structured. If it contains a list of strings, then you'll need to use a multidimensional array (or some other structure).
Third, when you declare an array, must specify its size before you can use it. result and searchfor are incomplete array definitions.
Finally, this line
getsearchtrial(sb[ ]) - strlen(sb[ ]) = adress;
needs to be reversed; the target of an assignment must be on the left of the assignment operator.
You need to step back and learn how to write C code from the ground up.
There is so much wrong I'm not even going to itemise it all here - most of it seems to stem from your lack of understanding of arrays in C.
Most notably...
You can't declare an array and not initialise it or specify a size.
You can't assign a FILE * to a char array (and expect decent
results).
You can't execute a statement like fp = fopen at the
global scope like you are.
Try this tutorial and you may fix 95% of your problems, then go from there.

Passing float * and FILE * to/from functions in C

First, I declare variables before the main() function:
// Files
FILE *density_model_file;
char *density_model_filename;
float *density_array;
Next, I open the FILE * for reading and allocate memory for the density array:
density_model_file = open4read(density_model_filename, program_name);
density_array = allocator(density_model_size, sizeof(float));
Up to this point, the debugger shows everything is working fine. Here
is the step that I can't seem to fix, where I am attempting to load
data into the calloc'd array:
density_array = floatfromfile(sizeof(float), density_model_size, density_model_file, density_model_filename);
The density_array has a NULL value after this step for some reason.
Here is the code for this function (contained in a separate .c file).
I have bolded the part where I think the issue exists:
float * floatfromfile(unsigned long int entrysize, int numentries, FILE *inputfile, const char *filename)
{
/* Declaration of density model array size variable */
int numbytes;
**void *temparray = 0;
/* Writes the gravity model to file */
numbytes = (int)fread(temparray, entrysize, numentries, inputfile);**
/* Checks that the forward model file has a sufficient number of entries */
if (numbytes == numentries)
{
printf("loaded %i values from %s using fread()\n", numbytes, filename);
return((float *)temparray);
}
else
{
printf("ERROR: %i data points read from %s of %i needed\n", numbytes, filename, numentries);
return((float *)temparray);
}
}
Any insight would be much appreciated. I think the issue might be that calloc() returns a pointer to a void array. I can provide the other functions if needed.
You seem to have a misunderstanding about how pointers work. What you need to do is pass density_array into floatfromfile as an argument.
What you are doing instead is overwriting the pointer to your allocated memory, with the return value from floatfromfile. That return value is always NULL because that's what you assigned it to (as temparray).
fread expects to be able to write its results into an allocated memory block. But you're giving it temparray which has not been allocated—in fact its value is 0. So you're giving fread the address 0 to write into, which is likely to cause the program to crash. Instead you need to pass your allocated pointer density_array at this point.

Reading file and populating struct

I have a structure with the following definition:
typedef struct myStruct{
int a;
char* c;
int f;
} OBJECT;
I am able to populate this object and write it to a file. However I am not able to read the char* c value in it...while trying to read it, it gives me a segmentation fault error. Is there anything wrong with my code:
//writensave.c
#include "mystruct.h"
#include <stdio.h>
#include <string.h>
#define p(x) printf(x)
int main()
{
p("Creating file to write...\n");
FILE* file = fopen("struct.dat", "w");
if(file == NULL)
{
printf("Error opening file\n");
return -1;
}
p("creating structure\n");
OBJECT* myObj = (OBJECT*)malloc(sizeof(OBJECT));
myObj->a = 20;
myObj->f = 45;
myObj->c = (char*)calloc(30, sizeof(char));
strcpy(myObj->c,
"This is a test");
p("Writing object to file...\n");
fwrite(myObj, sizeof(OBJECT), 1, file);
p("Close file\n");
fclose(file);
p("End of program\n");
return 0;
}
Here is how I am trying to read it:
//readnprint.c
#include "mystruct.h"
#include <stdio.h>
#define p(x) printf(x)
int main()
{
FILE* file = fopen("struct.dat", "r");
char* buffer;
buffer = (char*) malloc(sizeof(OBJECT));
if(file == NULL)
{
p("Error opening file");
return -1;
}
fread((void *)buffer, sizeof(OBJECT), 1, file);
OBJECT* obj = (OBJECT*)buffer;
printf("obj->a = %d\nobj->f = %d \nobj->c = %s",
obj->a,
obj->f,
obj->c);
fclose(file);
return 0;
}
When you write your object, you're writing the pointer value to the file instead of the pointed-to information.
What you need to do is not just fwrite/fread your whole structure, but rather do it a field at a time. fwrite the a and the f as you're doing with the object, but then you need to do something special with the string. Try fwrite/fread of the length (not represented in your data structure, that's fine) and then fwrite/fread the character buffer. On read you'll need to allocate that, of course.
Your first code sample seems to assume that the strings are going to be no larger than 30 characters. If this is the case, then the easiest fix is probably to re-define your structure like this:
typedef struct myStruct{
int a;
char c[30];
int f;
} OBJECT;
Otherwise, you're just storing a pointer to dynamically-allocated memory that will be destroyed when your program exits (so when you retrieve this pointer later, the address is worthless and most likely illegal to access).
You're saving a pointer to a char, not the string itself. When you try to reload the file you're running in a new process with a different address space and that pointer is no longer valid. You need to save the string by value instead.
I would like to add a note about a potential portability issue, which may or may not exist depending upon the planned use of the data file.
If the data file is to be shared between computers of different endian-ness, you will need to configure file-to-host and host-to-file converters for non-char types (int, short, long, long long, ...). Furthermore, it could be prudent to use the types from stdint.h (int16_t, int32_t, ...) instead to guarantee the size you want.
However, if the data file will not be moving around anywhere, then ignore these two points.
The char * field of your structure is known as a variable length field. When you write this field, you will need a method for determining the length of the text. Two popular methods are:
1. Writing Size First
2. Writing terminal character
Writing Size First
In this method, the size of the text data is written first, followed immediately by the data.
Advantages: Text can load quicker by block reads.
Disadvantages: Two reads required, extra space required for the length data.
Example code fragment:
struct My_Struct
{
char * text_field;
};
void Write_Text_Field(struct My_Struct * p_struct, FILE * output)
{
size_t text_length = strlen(p_struct->text_field);
fprintf(output, "%d\n", text_length);
fprintf(output, "%s", p_struct->text_field);
return;
}
void Read_Text_Field(struct My_STruct * p_struct, FILE * input)
{
size_t text_length = 0;
char * p_text = NULL;
fscanf(input, "%d", &text_length);
p_text = (char *) malloc(text_length + sizeof('\0'));
if (p_text)
{
fread(p_text, 1, text_length, input);
p_text[text_length] = '\0';
}
}
Writing terminal character
In this method the text data is written followed by a "terminal" character. Very similar to a C language string.
Advantages: Requires less space than Size First.
Disadvantages: Text must be read one byte at a time so terminal character is not missed.
Fixed size field
Instead of using a char* as a member, use a char [N], where N is the maximum size of the field.
Advantages: Fixed sized records can be read as blocks.
Makes random access in files easier.
Disadvantages: Waste of space if all the field space is not used.
Problems when the field size is too small.
When writing data structures to a file, you should consider using a database. There are small ones such as SQLite and bigger ones such as MySQL. Don't waste time writing and debugging permanent storage routines for your data when they have already been written and tested.

Resources