Writing to file in C overwrites variables - arrays

Description:
I have created a small program that stores the name and checksum of a file in a struct, for each file in a directory. When output is written to stdout with printf, everything seems fine, but if we write to a file with either fputs or fprintf, values get overwritten, perhaps because of some buffer overflow?
Output from main with print.
Name: 2.txt. Checksum: fc769d448ed4e08bd855927bad2c8e43efdf5315a6daa9f28577758786d52eaf
Name: 1.txt. Checksum: 2d46cffd0302c5537ddb4952a9cca7d66060dafecd56fe3a7fe8e5e5cabbbbf9
Name: 3.txt. Checksum: 37bb2e5563e94eee68fac6b07501c44f018599482e897a626a94dd88053b4b7e
However, if we print the values of checksumMaps[0] to a file,
the value checksumMaps[0].filename gets overwritten (with the last 2 bytes of the checksum string) as seen by:
FILE *fp = fopen("mychecksums.txt", "w");
char formatted_bytes[32*2+1];
char *filename = checksumMaps[0].filename;
format_bytes(formatted_bytes, checksumMaps[0].checksum);
fputs(filename, fp);
fputs(formatted_bytes, fp);
// We print the value of `filename` again in order to see that it has been overwritten.
printf("%s \n", filename);
fclose(fp);
The program writes aftxt to stdout instead of 2.txt.
Using gdb, I can see that the value of filename changes from 2.txt to aftxt after the line fputs(formatted_bytes, fp);. What could be the reason for this?
Minimal Reproducible Example
ArchiveFile.h
typedef struct ArchiveFile{
char *uuid;
char *checksum;
char *relative_path;
int is_binary;
} ArchiveFile;
typedef struct file_content{
unsigned char* bytes;
unsigned long file_size;
} file_content;
void set_uuid(ArchiveFile *file, char* uuid);
char* get_absolute_path(ArchiveFile *file, char* root);
char* get_file_text(ArchiveFile *file, char* root);
void get_bytes(ArchiveFile *file, char* root, unsigned char *buffer, size_t fsize);
long get_file_size(ArchiveFile *file, char *root);
ArchiveFile.c
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include "ArchiveFile.h"
#include <string.h>
void set_uuid(ArchiveFile* file, char* uuid){
file->uuid = uuid;
}
char* get_absolute_path(ArchiveFile *file, char* root){
/* Allocate space according to the relative path +
the root path + null terminating byte.*/
char* absolute_path = malloc(strlen(file->relative_path) + strlen(root) + 1);
// Add the root path.
strcpy(absolute_path, root);
// Concatonate the root with the rest of the path.
strcat(absolute_path, file->relative_path);
return absolute_path;
}
char* get_file_text(ArchiveFile *file, char* root){
char* absolute_path = get_absolute_path(file, root);
FILE *fp = fopen(absolute_path, "r");
if(fp == NULL)
printf("Could not open file %s \n", absolute_path);
// Platform independent way of getting the file size in bytes.
fseek(fp, 0, SEEK_END);
long fsize = ftell(fp);
fseek(fp, 0, SEEK_SET); /* same as rewind(f); */
char *buffer = malloc(fsize);
if(fp){
fread(buffer, sizeof(char), fsize, fp);
}
fclose(fp);
free(absolute_path);
return buffer;
}
void print_bytes2(unsigned char* md, size_t size){
for (size_t i = 0; i < size; i++) {
printf("%02x ", md[i]);
}
printf("\n");
}
void get_bytes(ArchiveFile *file, char *root, unsigned char *buffer, size_t fsize){
char* absolute_path = get_absolute_path(file, root);
FILE *fp = fopen(absolute_path, "rb");
if(fp){
fread(buffer, 1, fsize, fp);
}
free(absolute_path);
fclose(fp);
}
long get_file_size(ArchiveFile *file, char *root){
char* filepath = get_absolute_path(file, root);
FILE *fp = fopen(filepath, "rb");
fseek(fp, 0, SEEK_END);
long fsize = ftell(fp);
fseek(fp, 0, SEEK_SET); /* same as rewind(f); */
free(filepath);
fclose(fp);
return fsize;
}
checksum/checksum.h
// Used to store information about filename and checksum.
typedef struct ChecksumMap{
char* filename;
unsigned char checksum [32];
} ChecksumMap;
int calculate_checksum(void* input, unsigned long length, unsigned char* md);
checksum/checksum.h
#include <stdio.h>
#include <openssl/sha.h>
#include "checksum.h"
int calculate_checksum(void* input, unsigned long length, unsigned char* md){
SHA256_CTX context;
if(!SHA256_Init(&context))
return 0;
if(!SHA256_Update(&context, (unsigned char*)input, length))
return 0;
if(!SHA256_Final(md, &context))
return 0;
return 1;
}
main.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/types.h>
#include "ArchiveFile.h"
#include "checksum/checksum.h"
void format_bytes(char* buffer, unsigned char* md){
for (int i = 0; i < 32; i++) {
sprintf(&buffer[i*2], "%02x", md[i]);
}
buffer[32*2] = '\0';
}
void *listdir(char *name, int count, ChecksumMap *checksumMaps)
{
DIR *dir;
struct dirent *direntry;
if (!(dir = opendir(name)))
return NULL;
while ((direntry = readdir(dir)) != NULL) {
// If we reach a directory (that is not . or ..) then recursive step.
if (direntry->d_type == DT_DIR) {
char path[1024];
if (strcmp(direntry->d_name, ".") == 0 || strcmp(direntry->d_name, "..") == 0)
continue;
snprintf(path, sizeof(path), "%s/%s", name, direntry->d_name);
listdir(path, count, checksumMaps);
} else {
unsigned char md[32];
ArchiveFile file;
file.relative_path = direntry->d_name;
// Get the full path of the file:
char parent_name[strlen(name)+1];
memset(&parent_name[0], 0, sizeof(parent_name));
strcat(parent_name, name);
strcat(parent_name, "/");
size_t fsize = get_file_size(&file, parent_name);
unsigned char *bytes = malloc(sizeof(char) * fsize);
get_bytes(&file, parent_name, bytes, fsize);
calculate_checksum((void*) bytes, fsize, md);
ChecksumMap checksumMap = {.filename=file.relative_path};
memcpy(checksumMap.checksum, md,
sizeof(checksumMap.checksum));
free(bytes);
}
}
closedir(dir);
return NULL;
}
int main(int argc, char const *argv[]) {
FILE *fp = fopen("mychecksums.txt", "w");
char formatted_bytes[32*2+1];
char *filename = checksumMaps[0].filename;
format_bytes(formatted_bytes, checksumMaps[0].checksum);
fputs(filename, fp);
fputs(formatted_bytes, fp);
// We print the value of `filename` again in order to see that it has been overwritten.
printf("%s \n", filename);
fclose(fp);
}
Compile with gcc:
gcc -Wall -Wextra main.c ArchiveFile.c checksum/checksum.c -lcrypto

The program writes aftxt to stdout instead of 2.txt. Using gdb, I can see that the value of filename changes from 2.txt to aftxt after the line fputs(formatted_bytes, fp);. What could be the reason for this?
Hard to say, because we're in the domain of UB (undefined behavior). But there are two obvious candidates here.
formatted_bytes is not properly terminated, causing fputs to read past the array, invoking UB.
fp is not a valid stream. The reason could be that it's not initialized, or changed, or the stream is closed or something.
Enable -Wall -Wextra -fsanitize=address. You could also try -fsanitize=undefined.
Check all return values. malloc, fopen and fputs returns a value that can be used for error checking.
Replace formatted_bytes with a hardcoded string that have the value you think it has.
Learn how to create a Minimal, Reproducible Example and how to debug small c programs. It's a guide I wrote a while ago.

Update
It seems that there was some different problems with the code.
First thing to notice is file.relative_path = direntry->d_name;, however the value that direntry points to changes in each iteration, thus the value file.relative_path points to, also changes. Furthermore, the size of the string stored in file.relative_path has never been specified, which would be a problem, if we use strcpy.
The solution is to specify a size for file.relative_path and use strcpy to copy the value of direntry->d_name. Also, no need for the checksumMap struct, since ArchiveFile already can store the same information (again, specify a size for the checksum).
Thing to keep in mind when you work with strings, buffers, arrays in C:
Remember that strings in C are based on char arrays, themselves based on a pointer to the first element. Assigning the value of one string to another might return in unexpected behavior when you actually want to copy the value of the string, not the address to the first element.

One bug here:
char parent_name[strlen(name)+1];
memset(&parent_name[0], 0, sizeof(parent_name)); // could have been parent_name[0]='\0'; instead
strcat(parent_name, name); // Now the parent_name buffer is full and null terminated.
strcat(parent_name, "/"); // this overwrites the null terminator and writes a new one out-of-bounds
You should have done something like this:
size_t length = strlen(name);
char parent_name[length+1+1];
memcpy(parent_name, name, length); // copies characters only (fast) but not the null term
parent_name[length] = '/'; // append this single character 1 symbol past "name" string
parent_name[length+1] = '\0'; // add manual null termination

Related

Deleting a redundant parameter results in a segmentation fault, seemingly for no reason

The following program finds and deletes words that begin and end with the same character. It works just fine, except I decided to take the code for printing result text in from deleteWords() and put it inside of main(). Therefore, the *fpOut parameter in became redundant in deleteWords(). Deleting the parameter results in
/bin/sh: line 1: 1371 Segmentation fault: 11 ./main duom.txt rez.txt make: *** [main] Error 139
However if I compile it and run it any third parameter (e.g. int useless argument instead of FILE *fpOut), it works without errors.
Has anybody have a clue what could be causing this problem?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int checker (char zodis[]) {
size_t last = strlen(zodis);
if (zodis[0] == zodis[last-1])
return 0;
return 1;
}
void memAlloc (char **text, char **buffer, FILE **fp, char *fileName) {
int fileLength;
*fp = fopen(fileName, "r");
fseek(*fp, 0L, SEEK_END);
fileLength = fseek(*fp, 0L, SEEK_SET);
*text = malloc(fileLength * sizeof(char));
*buffer = malloc(fileLength * sizeof(char));
}
void deleteWords (FILE *fp, int anyUselessParameter, char *buffer) {
char *text;
while (fscanf(fp, "%s", text) == 1) {
if (checker(text)) {
printf("%s ", text);
sprintf(buffer + strlen(buffer), "%s ", text);
}
}
printf("\n");
}
int main(int argc, char *argv[]) {
FILE *fp, *fpOut;
int anyUselessParameter;
char *text, *buffer, *inputFileName = argv[1], *outputFileName = argv[2];
if (argc < 2)
return 0;
fpOut = fopen(outputFileName, "w");
memAlloc(&text, &buffer, &fp, inputFileName);
deleteWords(fp, anyUselessParameter, buffer);
fputs(buffer, fpOut);
fclose(fp);
fclose(fpOut);
free(text);
return 0;
}
char *text;
while (fscanf(fp, "%s", text) == 1) {
scanf needs the buffer to be allocated. Here it dereferences an uninitialized pointer text and writes to it. scanf tries to write to text[0], text[1].. and so on, so accesses text out of bounds and undefined behavior happen.
*buffer = malloc(fileLength * sizeof(char));
...
sprintf(buffer + strlen(buffer), "%s ", text);
buffer is uninitialized, so strlen(buffer) will result in some undefined value. Explicitly initialize buffer[0] = '\0' if you wish to use strlen later. Also you don't include memory for terminating '\0' character inside your buffer.
As you are trying to read the file into a buffer, that is allocated using the file size
if (fread(buffer, fileLenght, 1, fp) != fileLength) { /* handle error */ }
If you have to, use snprintf instead of sprintf just to be safe. snprinttf(buffer+strlen(buffer), fileLength - strlen(buffer), ...);
Also, try to never use scanf without specifing field length inside %s modifier. You can try:
char text[256]; // or other maximum word length
while (fscanf(fp, "%255s", text) == 1) {
As you already have allocated memory for the file, you can use it as a parameter to scanf, if you have to. One would need to prepare the format string for scanf as argument - it is a bit hard. See below:
for (;;) {
// prepare scanf %s format modifier to use with printf to write to buffer end
char fmt[20];
size_t buffer_size = fileLenght;
size_t free_in_buffer = buffer_size - strlen(buffer);
snprintf(fmt, 20, "%%%ds", free_in_buffer);
// we will write here: up to free_in_buffer
char *in = buffer + strlen(buffer);
if (fscanf(fp, fmt, in) != 1) break;
// we now check the last readed word form the file
if (!checker(in)) {
// if the last readed word is bad, we can revert it
in[0] = '\0'
}
}
This is wrong:
fileLength = fseek(*fp, 0L, SEEK_SET);
Per POSIX:
RETURN VALUE
The fseek() and fseeko() functions shall return 0 if they
succeed.
Otherwise, they shall return -1 and set errno to indicate the error.

Error using fprintf and fscanf

I have an archive results.csv and I need to read the first line of this archive and print it out on output.txt. Somehow it's printing random characters after everything and I couldn't figure out what is wrong.
Command: a.c results.csv
First line:
date,home_team,away_team,home_score,away_score,tournament,city,country,neutral
output.txt: date,home_team,away_team,home_score,away_score,tournament,city,country,neutral,(!£,(!£,(!£,(!£,(!£,#,£,(!£,(!£
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
typedef struct
{
char *line1;
char *line1a;
char *line1b;
char *team1;
char *team2;
char *reason;
char *city;
char *country;
char *neutral_field;
}data;
void open_input(char *argv[], FILE **input)
{
if((*input=fopen(argv[1], "r")) == NULL)
{
printf("%s not found\n", argv[1]);
exit(1);
}
}
void open_output(char *string, FILE **output)
{
if((*output=fopen(string, "w")) == NULL)
{
printf("%s not found\n", string);
exit(1);
}
}
void alloc_data(data *d, int size)
{
d->line1 = (char*)malloc(4*sizeof(char));
d->team1 = (char*)malloc(9*sizeof(char));
d->team2 = (char*)malloc(9*sizeof(char));
d->line1a = (char*)malloc(10*sizeof(char));
d->line1b = (char*)malloc(10*sizeof(char));
d->reason = (char*)malloc(10*sizeof(char));
d->city = (char*)malloc(4*sizeof(char));
d->country = (char*)malloc(7*sizeof(char));
d->neutral_field = (char*)malloc(7*sizeof(char));
}
void store(data *d, FILE *input, FILE **output)
{
fscanf(input, "%s,%s,%s,%s,%s,%s,%s,%s,%s", d[0].line1, d[0].team1, d[0].team2, d[0].line1a, d[0].line1b, d[0].reason, d[0].city, d[0].country, d[0].neutral_field );
fprintf(*output, "%s,%s,%s,%s,%s,%s,%s,%s,%s\n", d[0].line1, d[0].team1, d[0].team2, d[0].line1a, d[0].line1b, d[0].reason, d[0].city, d[0].country, d[0].neutral_field );
}
int main(int argc, char *argv[])
{
FILE *input;
FILE *output;
char *string = "output.txt";
int size = 1000;
open_input(argv, &input);
open_output(string, &output);
data *d;
d = (data*)malloc(size*sizeof(data));
alloc_data(d, size);
store(d, input, &output);
free(d);
return 0;
}
fscanf(input, "%s,%s,%s,%s,%s,%s,%s,%s,%s", d[0].line1, d[0].team1,...
The above code tries to read the whole line in to d[0].line1 which causes buffer overflow. team1 and the rest will contain uninitialized data.
You have to change fscanf as follows:
fscanf(input, "%3[^ ,\n\t],%9[^ ,\n\t],...
Where 3 is 4 - 1, and 4 is the size of d[0].line1
Alternatively you can use strtok
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void store(FILE *input, FILE *output)
{
char buf[500];
while(fgets(buf, sizeof(buf), input))
{
//strip end-of-line from `buf`
if(strlen(buf))
if(buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = 0;
//tokenize with strtok
char *token = strtok(buf, ",");
while(token)
{
fprintf(output, "%s", token);
token = strtok(NULL, ",");
}
fprintf(output, "\n");
}
}
int main(int argc, char *argv[])
{
FILE *input = fopen("input.txt", "r");
FILE *output = fopen("output.txt", "w");
store(input, output);
return 0;
}
With above code you don't need an additional structure.
If you do use a structure for data, you have to be more careful. It seems you are trying to create an array of 1000 data, but the following only creates one oversized pointer, not an array of data
int size = 1000;
data *d;
d = (data*)malloc(size*sizeof(data));
alloc_data(d, size);
Additionally, for each malloc there should be a corresponding free.
Your buffers aren't big enough to hold the terminating NUL byte. scanf stores that NUL byte (overrunning the buffer), but then the object that really owns that byte may overwrite it, so when printf looks for the NUL it doesn't find it until much later in memory.
The buffer overruns are a bigger problem than what you've seen, who knows what objects those NUL bytes you didn't make space for are smashing? And what happens when you read a data file with slightly different header spelling? Suddenly your hard-coded allocations sizes will be even more wrong than they are already.

C program to reverse content of a file and write that to another file

I'm having issues with an assignment where I have to take the contents of one file into a buffer, reverse those contents, and write them to another file. This program NEEDS to utilize two functions that look like this:
int read_file( char* filename, char **buffer );
int write_file( char* filename, char *buffer, int size);
so far my files look like this:
file_utils.h
#ifndef UTILS_H
#define UTILS_H
int read_file(char* filename, char **buffer);
int write_file(char* filename, char *buffer, int size);
#endif
file_utils.c
#include "file_utils.h"
#include <stdlib.h>
#include <stdio.h>
#include <font1.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
int read_file(char* filename, char **buffer) {
FILE* file1;
file1 = fopen(filename, "r");
//gets the size of the file
struct stat st;
stat(filename, &st);
int size = st.st_size;
buffer = malloc(size);
read(file1, &buffer, 1);
return size;
}
int write_file(char* filename, char*buffer, int size) {
FILE* file2;
file2 = fopen(filename, 'w');
for (int k = size - 1; k >= 0; k--) {
char* x = &buffer + k;
fprintf(file2, "%s", x);
}
printf(filename, '\O');
return 1;
}
reverse.c
#include "file_utils.h"
#include <stdlib.h>
#include <stdio.h>
#include <font1.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
char* buffer;
char* filename1;
char* filename2;
int filesize;
filename1 = argv[1];
filename2 = argv[2];
filesize = read_file(filename1, &buffer);
write_file(filename2, buffer, filesize);
return 0;
}
and that's all there is. I run it using "clang file_utils.c reverse.c" and I get warnings for file_utils.c like
incompatible integer to pointer conversion passing 'int" to parameter of type 'const char *' (for the line file1 = fopen(filename, 'r')
incompatible pointer to integer conversion passing 'FILE *' (aka 'struct_IO_FILE*') to parameter of type 'int' (for the line read(file1, &buffer, 1);)
the same warning as the first one but for the line file2 = fopen(filename, 'w');
and incompatible pointer types initializing 'char *' with an expression of type 'char **'; dereferences with * (for the line char* x = &buffer + k;)
on top of all this when I continue on to run the executable as such
./a.out file1 file2
where file 1 has text that should be reversed into file 2, I get a segmentation fault.
Any insight into things I can fix will be much appreciated.
Just off the top of my head, without testing, I see these bugs:
buffer = malloc(size); should be *buffer = malloc(size);
... because buffer is a pointer to pointer to char, you need to
dereference it once.
read(file1, &buffer, 1); should be fread(*buffer, 1, size, file1);
... because you opened file1 with fopen, so it's FILE *. read is
Unix I/O, not stream I/O, and doesn't use FILE *.
file2 = fopen(filename, 'w'); should be file2 = fopen(filename, "w");
The second argument should be a "string" (pointer to char or array of
char). 'w' is a single char.
char* x = &buffer + k; should be char *x = buffer + k;
buffer is a pointer to char, so you want to use it directly, not
take its address. Also note the style of putting * next to the
variable instead of the type. This is a good habit, because these
do not mean the same thing:
char *a, *b, *c; /* three pointers */
char* a, b, c; /* one pointer, two chars */
fprintf(file2, "%s", x); should be fprintf(file2, "%c", *x);
The first form treats x as the beginning of a string and will output
everything from that point onward until it hits a NUL terminator. You
want to output only one char, so use the %c specifier, and
dereference x to get a char.
A better way would be fwrite(x, 1, 1, file2);
printf(filename, '\O'); is not needed and doesn't do what you think.
It looks like you intended to write a NUL at the end. That would be
'\0' (zero), not '\O' (letter O). In any case, it's not needed or
wanted. NUL is used to terminate a string in C, not a file. Your output
file will be one character longer than it should be if you do this.
The most important issue with your code is here
char* x = &buffer + k;
fprintf(file2, "%s", x);
perhaps you mean
char *x = buffer + k;
fprintf(file2, "%c", *x);
You also, are mixing IO functions. For a FILE * object you should use fread() instead of read(), there should be an incompatible arguments warning from the compiler.
If there are no warnings (BTW char *x = &buffer + k should trigger another warning), then you should probably enable them explicitly so that your compiler can help you figure out other problems.
Also, check that file1 is not NULL after fopen(), check that fread() did read the requested amount, in general check for every possible error which you can easily infer from the return value of the implied function, if you don't know the meaning of such value then READ THE DOCUMENTATION before using such function.
And finally all together:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
int read_file(char* filename, char **buffer) {
FILE* file1;
file1 = fopen(filename, "r");
//gets the size of the file
struct stat st;
stat(filename, &st);
int size = st.st_size;
*buffer = malloc(size);
fread(*buffer, size, 1, file1);
fclose(file1);
return size;
}
void write_file(char* filename, char*buffer, int size) {
FILE* file2 = fopen(filename, "w"); int k;
for (k = size - 1; k >= 0; k--) {
fwrite(buffer + k, 1, 1, file2);
}
fclose(file2);
}
int main(int argc, char *argv[]) {
char* buffer;
char* filename1;
char* filename2;
int filesize;
filename1 = "input.txt";
filename2 = "reverse.txt";
filesize = read_file(filename1, &buffer);
write_file(filename2, buffer, filesize);
free(buffer);
return 0;
}
with a live demo. Please add checks for all return values, like malloc() does not return NULL.

Writing, reading, and splitting files in C

I'm really new at C programing, so I need to read a big file, split it every point, and write in a new file what I got when splitting, so I have to write several files, the problem is when I'm naming the new files.
I've been working in this proyect like for a week and I can't fix this issue.
Here the code I have so far:
#include <stdio.h>
#include <string.h>
char *myRead(char file_name[]){
char *output;
long size;
FILE *myfile = fopen(file_name,"rb");
fseek(myfile, 0, SEEK_END);
size = ftell(myfile);
rewind(myfile);
output = (char*) malloc(sizeof(char) * size);
fread(output,1,size,myfile);
fclose(myfile);
return output;
}
void myWrite(char content[], int i){
FILE *myfile;
myfile = fopen(i,"w");
fprintf(myfile,"%s",content);
fclose(myfile);
}
void split(char *content){
int word_length = strlen(content);
int i = 0;
char *output = strtok (content,".");
while (output != NULL){
myWrite(output,i);
printf("%s\n", output);
output = strtok (NULL, ".");
i++;
}
}
int main(){
char file_name[] = "hourglass.txt";
char *content = myRead(file_name);
split(content);
return 0;
}
What I want to know it's how can I do several files with just a number for the name?
Change
myfile = fopen(i,"w");
to
char file_name[100];
sprintf(filename, "%d", i);
myfile = fopen(file_name, "w");
That should fix it for you
C's fopen has signature:
FILE* fopen(const char *filename, const char *mode)
when you call with i in myWrite you are telling it a string is located at that address, where likely there is garbage. If this isn't homework explain and I can elaborate but in case it is I'll just point you to itoa.

Segmentation Fault when accessing char array outside function but noth within

so basically, thats the code producing my segmentation fault
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CORRUPT_KEY_FILE "s64581-source-key-corrupt.bin" /* ContainsKey and IV */
#define DEBUG 1
/**
* Reads content of a given file and writes it into a buffer
*
* #param filename Path of the file to be read
* #param buffer Pointer to the buffer to write into
* #param length Variable to store the filelength into
*
* #return EXIT_FAILURE if an error occurred opening th File, otherwise EXIT_SUCCESS
*/
int readFiletoBuffer(const char* filename, unsigned char* buffer, int* length) {
int i;
if(DEBUG) printf("\n\nFunction readFileToBuffer(..) called\n");
FILE *file = fopen(filename, "rb");
if(!file) {
printf("Error opening file %s", filename);
return EXIT_FAILURE;
}
fseek(file, 0 , SEEK_END);
*length = ftell(file);
rewind(file);
if(DEBUG) printf("\tLength of File %s: %d\n",filename ,*length);
buffer = (unsigned char*) malloc(*length+1);
fread(buffer, *length, 1, file);
fclose(file);
for(i=0; i<*length; i++) printf("%d:\t%X\n",14, buffer[14]);
return EXIT_SUCCESS;
}
int main(int argc, char **argv)
{
unsigned char *tmp_char;
int tmp_length, i;
tmp_char=NULL;
readFiletoBuffer(CORRUPT_KEY_FILE, tmp_char, &tmp_length);
for(i=0; i<tmp_length; i++) printf("%d:\t%X\n",i, tmp_char[i]);
return 0;
}
Notice the two for loops printing the content of the buffer. The one inside the function works fine, the one in the main-function produces a segmentations fault. Why is that, and how could I fix this while allocating the needed memory within the function readFiletoBuffer ?
Any help is appreciated! :D
To take the char array you want from the function you have to pass a char** array as argument.
In that way your char* pointer will be returned.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CORRUPT_KEY_FILE "s64581-source-key-corrupt.bin" /* ContainsKey and IV */
#define DEBUG 1
/**
* Reads content of a given file and writes it into a buffer
*
* #param filename Path of the file to be read
* #param buffer Pointer to the buffer to write into
* #param length Variable to store the filelength into
*
* #return EXIT_FAILURE if an error occurred opening th File, otherwise EXIT_SUCCESS
*/
int readFiletoBuffer(const char* filename, unsigned char** buffer, int* length) {
int i;
if(DEBUG) printf("\n\nFunction readFileToBuffer(..) called\n");
FILE *file = fopen(filename, "rb");
if(!file) {
printf("Error opening file %s", filename);
return EXIT_FAILURE;
}
fseek(file, 0 , SEEK_END);
*length = ftell(file);
rewind(file);
if(DEBUG) printf("\tLength of File %s: %d\n",filename ,*length);
*buffer = (unsigned char*) malloc(*length+1);
fread(*buffer, *length, 1, file);
fclose(file);
for(i=0; i<*length; i++) printf("%d:\t%X\n",14, buffer[14]);
return EXIT_SUCCESS;
}
int main(int argc, char **argv)
{
unsigned char *tmp_char;
int tmp_length, i;
tmp_char=NULL;
readFiletoBuffer(CORRUPT_KEY_FILE, &tmp_char, &tmp_length);
for(i=0; i<tmp_length; i++) printf("%d:\t%X\n",i, tmp_char[i]);
return 0;
}
Here is my suggestion.
The problem is, when you call your function readFiletoBuffer with parameter tmp_char, this parameter will be copied! Your tmp_char is NULL after call this function!
You need to work with the pointer to this parameter to change it like this:
tmp_char=NULL;
readFiletoBuffer(CORRUPT_KEY_FILE, &tmp_char, &tmp_length);
So your function will look like this:
int readFiletoBuffer(const char* filename, unsigned char** buffer, int* length)

Resources