linkisted have a pointer for required data - c

I have a linked list , which has full information
typdef struct details
{
struct details *head, *next, *curr;
char* values;
}
Details *fyllinfo ; //(this strcuct has full information)
/* fyllinfo this has been filled with many vallues*/
Now I need to copy the pointers to new list which can point only my required or matched data
Details *required;
char* myValue;
details *pstPtr =fyllinfo->head;
details *required = (details*) malloc(
sizeof(details));
required->head = required->next = NULL;
while(pstPtr != NULL)
{
if(NULL == pstPtr->values) goto NEXT;
printf("UUID FOUND IS [%s] and comapre with [%s] ",pstPtr->values,myvalue);
if(strCmp(pstPtr->values,myvalue)==0)
{
if(required->head == NULL)
{
required->head = required->curr = pstPtr;
}
else
{
required->next = pstPtr;
required->curr->next = pstPtr;
}
}
NEXT: pstPtr = pstPtr->next;
}
My Required ptr should have the pointer to only matched values
My code only points the first matched data and it has unmatched data in reuired->next pointer

This code works now... you just need to put some effort to understand it. you should be able to print the values. see how values are stored now.
details *pstPtr = fyllinfo;
details *required = NULL; //(details*)malloc(
details *head=NULL;
while (pstPtr != NULL)
{
if (1 == pstPtr->values)
printf("UUID FOUND IS [%d] and comapre with [%d] \n", pstPtr->values, 1);
if (pstPtr->values==1)
{
if (required == NULL)
{
required = (details*)malloc( sizeof(details));
head = required;
required->head = required->curr = pstPtr;
required->next = NULL;
}
else
{
required->next = (details*)malloc(sizeof(details));
required = required->next;
required->next = NULL;
required->curr = pstPtr;
}
}
pstPtr = pstPtr->next;
}
void display(struct details *r)
{
// r = fyllinfo;
if (r == NULL)
{
return;
}
while (r != NULL)
{
printf("%d ", r->curr->values);
r = r->next;
}
printf("\n");
}

Related

Heap buffer overflow on a getline() - C

I am coding a local server, I need to parse a file to get the config of the server.
Problem : I have a heap buffer overflow indicated at on the while.
This probeme is shown when I run with -fsanitize but I don't have any trouble without.
Here is the code :
struct container *configParse(FILE *file)
{
char *line = NULL;
size_t n;
char *token = NULL;
char *saveptr = NULL;
struct container *head = NULL;
struct container *container = NULL;
int key = 0;
int first = 1;
while ((getline(&line, &n, file)) != -1)
{
token = strtok_r(saveptr, " =\n\r", &line);
while (token != NULL)
{
if (token[0] == '[')
{
if (first)
{
container = container_init();
container->title = token;
head = container;
first = 0;
}
else
{
container = container_add_back(container);
container = container->next;
container->item = NULL;
container->title = token;
}
key = 0;
}
else
{
if (key == 0)
{
if (container->item == NULL)
{
container->item = items_init();
container->item->key = token;
}
else
{
struct item *itemcpy = container->item;
while (itemcpy->next != NULL)
{
itemcpy = itemcpy->next;
}
itemcpy->next = items_init();
itemcpy->next->key = token;
}
key = 1;
}
else
{
struct item *itemcpy = container->item;
while (itemcpy->next != NULL)
{
itemcpy = itemcpy->next;
}
itemcpy->value = token;
key = 0;
}
}
token = strtok_r(NULL, " =\n\r", &line);
}
}
container_print(head);
printf("\n*****Parsing du .conf*****\n\n");
if (isvalid(head))
printf("Parsing OK\n");
else
{
printf("Parsing KO\n");
return NULL;
}
return head;
}
Thanks in advance.
As explained I try to run the program without -fsanitze, and everything was fine

Usage of struct of double pointer

Below is my source code, I am trying to store sensor information on linked list.
Sensor information is received by type of JSON. And, I confirmed that it is well parsed.
However, when i am trying to store received value, then "Segmentation fault
" error is occurred.
I think it is because of usage of double pointer of struct.
How can i solve this?
//sensor
if (!strcmp(key, "sensor") && (num_sen > 0))
{
#if DEBUG
fprintf(stderr, "\n********************SENSORINFO********************\n");
#endif
SENSOR_CONFIG **p_sen = NULL;
p_sen = mcfg->sensor_cfg;
SENSOR_CONFIG ** firstnode = NULL;//첫번째 노드
SENSOR_CONFIG ** lastnode = NULL;//마지막 노드
if (num_sen > 0)
{
for (int i = 1; i < num_sen; i++)
{
json_t * arr_data, *obj2, *arr2;
const char * key2;
int k;
json_array_foreach(obj, i, arr) {
//각 센서 array 시작
json_object_foreach(arr, key2, obj2) {
p_sen = (SENSOR_CONFIG **)malloc(sizeof(SENSOR_CONFIG));//먼저 생성하고 보자
arr_data = json_object_get(arr, key2);
if (_eq("id"))
{
fprintf(stderr, "can read id\n");
(*p_sen)->id = (int)json_integer_value(arr_data);
fprintf(stderr, "id:");
fprintf(stderr, "%d\n", (*p_sen)->id);
}
...
}
(*p_sen)->next = NULL;
if (firstnode == NULL) {//처음으로 실행되면
firstnode = p_sen;
lastnode = p_sen;
}
else {//처음이 아니면 뒤에 계속 추가. lastnode가 계속 바뀐다
(*lastnode)->next = p_sen;//원래 lastnode가 가리키는 곳이 새로 생성된 노드의 값을 가리키게 한 후,
lastnode = p_sen;//방금 생성한 노드가 맨 끝자리에 추가했으므로 방금 생성한 노드가 lastnode가 된다.
}
}//sensor array
}
}
else
{
mcfg->sensor_cfg = NULL;
}
}

circular linked list not working properly in c windows

I tried to implement circular linked list to manage a set of tasks by a server side application. The application is multi-threaded where one thread (updater() ) reads the linked list only for read while another two (push_stream() and delete_stream()) access the linked list to add to and delete from the linked list respectively.
My problem is not all the files to be deleted (after being processed) are deleted.
struct data_stream
{
bool processed;
int count;
char filename[30];
int TYPE_GRP;
int task_type;
struct data_stream *next;
};
struct data_stream *stream_head=NULL; //global variable
main(partial code)
main()
{
_beginthread(updater, 0, NULL);
while ((new_socket = accept(srv_sock, (struct sockaddr *)&client, &c)) != INVALID_SOCKET)
{
_beginthreadex(0, 0, handle_client, &new_socket, 0, 0);
}
}
handle_client function (partial code)
handle_client()
{
//some where in handle_client
EnterCriticalSection(&stream_lock);
stream_head = push_stream(&stream_head, TYPE_GRP, task_type);
LeaveCriticalSection(&stream_lock);
}
updater function (full source code)
void updater(void *data)
{
while (1)
{
struct data_stream*temp = stream_head;
struct data_stream*first = stream_head;
struct data_stream*prev = NULL;
if (stream_head != NULL)
{
struct data_stream*next = NULL;
do
{
next = temp->next;
if (temp->processed == false&&temp->task_type == 2)
{
process_files(temp);
}
else if (temp->processed == false&&temp->task_type == 3)
{
process_others();
}
EnterCriticalSection(&stream_lock);
temp->processed = true;
LeaveCriticalSection(&stream_lock);
temp = next;
} while (temp != first);
}
if (stream_head != NULL)
{
EnterCriticalSection(&stream_lock);
stream_head=delete_stream(&stream_head);
LeaveCriticalSection(&stream_lock);
}
Sleep(6000);
}
}
process_files
void process_files(struct data_stream*temp)
{
int count = 0;
char file_to_update[50] = { NULL };
size_t name_len = strlen(temp->filename);
memcpy_s(file_to_update, name_len, temp->filename, name_len);
file_to_update[name_len] = '\0';
temp->count = 0;
FILE *list_to_update ;
fopen_s(&list_to_update , file_to_update, "r+b");
if (list_to_update != NULL)
{
char readline[100] = { '\0' };
while (fgets(readline, sizeof(readline), list_to_update ) != NULL)
{
//read a line at a time and process the list
count++;
}
temp->count = count;
fclose(list_to_update );
}
else
printf("\nerror opening file\n");
}
process_others()
void process_others(struct data_stream*temp)
{
int count = 0;
char file_to_update[50] = { NULL };
size_t name_len = strlen(temp->filename);
memcpy_s(file_to_update, name_len, temp->filename, name_len);
file_to_update[name_len] = '\0';
temp->count = 0;
FILE *list_to_update ;
fopen_s(&list_to_update , file_to_update, "r+b");
if (list_to_update != NULL)
{
char readline[100] = { '\0' };
while (fgets(readline, sizeof(readline), list_to_update ) != NULL)
{
//read a line at a time and process the list
count++;
}
temp->count = count;
fclose(list_to_update );
}
else
{
printf("\nerror opening file\n");
}
}
}
push_stream (full source code)
struct data_stream* push_stream(struct data_data_stream**head_ref, int typ_grp, int task_type)
{
struct data_stream*new_data_stream=
(struct data_data_stream*)malloc(sizeof(struct data_stream));
new_stream->processed = false;
new_stream->task_type = task_type;
new_stream->TYPE_GRP = typ_grp;
new_stream->filename[0] = NULL;
new_stream->count = 0;
new_stream->next = NULL;
if (task_type == 2)
sprintf_s(new_stream->filename, "%s%03d.txt", "file_md5_list_", stream_count);
else
sprintf_s(new_stream->filename, "%s%03d.txt", "other_list_", stream_count);
if (*head_ref == NULL)
{
*head_ref = new_stream;
new_stream->next = new_stream;
}
else
{
struct data_stream* last = (*head_ref)->next;
struct data_stream* prev = (*head_ref)->next;
while (last != *head_ref)
{
prev = last;
last = last->next;
}
new_stream->next = *head_ref;
last->next = new_stream;
}
if (stream_count > 998)
stream_count = 1;
else
stream_count++;
return new_stream;
}
delete_stream function (full source code)
struct data_stream* delete_stream(struct data_data_stream**head)
{
if (head == NULL)
return NULL;
struct data_stream*prev = NULL;
struct data_stream*temp = *head;
struct data_stream*first = *head;
do
{
struct data_stream*next = temp->next;
if (temp->processed)
{
if (prev == NULL)
*head = temp->next;
else
prev->next = temp->next;
char file_to_delete[50] = { NULL };
memcpy_s(file_to_delete, strlen(temp->filename), temp->filename, strlen(temp->filename));
DeleteFileA(file_to_delete);
free(temp);
}
else
{
prev = temp;
}
temp = next;
} while (temp != first);
if (prev == NULL)
{
return NULL;
}
return *head;
}

List of lists C

I want to make list made of lists and the inner list is made of items.
Union of datatype:
typedef union s_datatype {
int t_int;
char* t_char;
double t_double;
bool t_bool;
} t_datatype;
Structure of item:
typedef struct s_token {
int y;
int type;
t_datatype value;
struct s_token *next;
} t_token;
Structure of inner list:
typedef struct s_line {
int x;
int depth;
int type;
int number_of_tokens;
t_token*head;
struct s_line *next;
} t_line;
Structure of the final list:
typedef struct s_tokenized_code {
int number_of_lines;
t_line*head;
} t_tokenized_code;
Let's say that I want to add new "line" and into that line, I want to insert the "token". But I don't know, how to put this together. Can you help me? I'm not sure how to alloc this and how to work with this list of lists.
EDIT: Structure modified
Solved.
If you want to know, how to (i want to insert items to the end of list, not beginning):
t_token*init_token (int y, int type, t_datatype value) {
t_token*token = malloc(sizeof(struct s_token));
if (token == NULL) {
return NULL;
}
token->y = y;
token->type = type;
token->value = value;
token->next = NULL;
return token;
}
t_line*init_line (int x, int depth, int type) {
t_line*line = malloc(sizeof(struct s_line));
if (line == NULL) {
return NULL;
}
line->x = x;
line->depth = depth;
line->type = type;
line->number_of_tokens = 0;
line->head = NULL;
line->next = NULL;
return line;
}
t_tokenized_code*init_code (void) {
t_tokenized_code*code = malloc(sizeof(struct s_tokenized_code));
if (code == NULL) {
return NULL;
}
code->number_of_lines = 0;
code->head = NULL;
return code;
}
void insert_token (t_line*line, t_token*token) {
if (token != NULL) {
if (line->head == NULL){
line->head = token;
line->number_of_tokens += 1;
return;
}
t_token*tmp = line->head;
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = token;
line->number_of_tokens += 1;
}
}
void insert_line (t_tokenized_code*code, t_line*line) {
if (line != NULL) {
if (code->head == NULL) {
code->head = line;
code->number_of_lines += 1;
return;
}
t_line*tmp = code->head;
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = line;
code->number_of_lines += 1;
}
}
void free_code (t_tokenized_code*source) {
if (source == NULL) {
return;
}
t_line*line;
t_token*token;
while ((line = source->head) != NULL) {
while ((token = line->head) != NULL) {
line->head = token->next;
free(token);
}
source->head = line->next;
free(line);
}
free(source);
}
Let's make a simple test - Adding 2 tokens to the first line and 3 tokens to the second line:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main (void) {
t_tokenized_code*source = init_code();
t_line*line = init_line(0,0,3);
if (line == NULL) {
return 1;
}
insert_line(source, line);
t_datatype tt;
tt.t_char = "first";
t_token*token = init_token(0,6, tt);
if (token == NULL) {
return 1;
}
insert_token(line, token);
tt.t_char = "second";
token = init_token(1,6, tt);
insert_token(line, token);
line = init_line(1,0,3);
insert_line(source, line);
tt.t_char = "third";
token = init_token(0,6, tt);
insert_token(line, token);
tt.t_char = "fourth";
token = init_token(1,6, tt);
insert_token(line, token);
tt.t_char = "fifth";
token = init_token(2,6, tt);
insert_token(line, token);
line = source->head;
while (line != NULL) {
printf("****\n");
token = line->head;
while (token != NULL) {
printf("%s\n", token->value.t_char);
token = token->next;
}
line = line->next;
}
free_code (source);
return 0;
}
The output will be following:
****
first
second
****
third
fourth
fifth

C Custom Database writing errors

I have an assignment for class that I have to write a program to read and write key, value pairs to disk. I am using a linked list to store the keys, and read in values whenever I need to from disk. However, I am having trouble changing and deleting values. I am using this to test it: http://gaming.jhu.edu/~phf/2010/fall/cs120/src/sdbm-examples.tar.gz. Code below. Basically, I need some help figuring out errors, because this is the first assignment we have had to use pointers on, and I am just dying in all the segfaults and everything else. Just some advice would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>
#include "sdbm.h"
FILE *db;
bool opened = false, needNewDB = false;
int err = 0, keyLen = 0;
char *filename;
typedef struct Key_{
char *name;
char *val;
long offset;
struct Key_ *next;
} Key;
Key *head = NULL,*tail = NULL, *lastHas = NULL, *beforeLastHas = NULL;
/**
* Create new database with given name. You still have
* to sdbm_open() the database to access it. Return true
* on success, false on failure.
*/
void listAdd() {
if (tail != NULL) {
tail->next = (Key *) malloc(sizeof(Key));
tail = tail->next;
}
else {
tail = (Key *)malloc(sizeof(Key));
head = tail;
}
tail->next = NULL;
tail->name = NULL;
tail->val = NULL;
}
bool sdbm_create( const char *name ) { //Errors: 1) fopen failed 2) fclose failed on new db
filename = malloc(sizeof(*name));
strcpy(filename,name);
FILE *temp = fopen(name, "w");
if (temp == NULL) {
printf("Couldn't create file %s\n",name);
err = 1;
return false;
}
if (fclose(temp) == EOF) {
printf("Couldn't close created file %s\n",name);
err = 2;
return false;
}
return true;
}
/**
* Open existing database with given name. Return true on
* success, false on failure.
*/
bool sdbm_open( const char *name ) { //Errors: 3) couldn't open database
db = fopen(name,"r+");
if (db == NULL) {
err = 3;
printf("Couldn't open database file %s\n",name);
return false;
}
opened = true;
int c;
bool inKey = true;
char currKey[MAX_KEY_LENGTH];
while ((c = getc(db)) != EOF) {
if (!inKey && c == '\0') {
inKey = true;
}
else if (inKey && c == '\0') {
currKey[keyLen] = '\0';
listAdd();
tail->offset = ftell(db);
tail->name = malloc(sizeof(*currKey));
strcpy(tail->name,currKey);
keyLen = 0;
inKey = false;
}
else if (inKey) {
currKey[keyLen] = c;
keyLen++;
}
}
Key *curr = head;
while (curr != NULL) {
printf("Key: %s\n",curr->name);
curr = curr->next;
}
return true;
}
void readVal(char *value, long offset) {
fseek(db,offset,SEEK_SET);
int c;
for (int i = 0; (c = getc(db)) != '\0'; i++) {
*(value + i) = c;
}
}
/**
* Synchronize all changes in database (if any) to disk.
* Useful if implementation caches intermediate results
* in memory instead of writing them to disk directly.
* Return true on success, false on failure.
*/
bool sdbm_sync() {
if (!needNewDB) {
Key *curr = head;
fseek(db,0,SEEK_END);
while (curr != NULL) {
if (curr->val != NULL) {
fprintf(db,"%s%c%s%c",curr->name,'\0',curr->val,'\0');
}
curr = curr->next;
}
}
else {
FILE *temp;
sdbm_create("tRpdxD.p4ed");
temp = fopen("tRpdxD.p4ed","w");
Key *curr = head;
while (curr != NULL) {
if (curr->val != NULL) {
fprintf(temp,"%s%c%s%c",curr->name, '\0', curr->val, '\0');
}else {
char *tempS = malloc(MAX_VALUE_LENGTH);
readVal(tempS, curr->offset);
fprintf(temp,"%s%c%s%c",curr->name,'\0',tempS,'\0');
free(tempS);
}
fflush(temp);
fflush(db);
curr = curr->next;
}
fclose(db);
remove(filename);
rename("tRpdxD.p4ed",filename);
db = fopen(filename,"r+");
}
fflush(db);
return true;
}
/**
* Close database, synchronizing changes (if any). Return
* true on success, false on failure.
*/
bool sdbm_close() { // Errors: 5) Couldn't close database
sdbm_sync();
Key *tmp = head;
while (head->next != NULL) {
tmp = head;
head = head->next;
free(tmp->name);
if (tmp->val != NULL) {
free(tmp->val);
}
free(tmp);
}
if (fclose(db) == EOF) {
err = 5;
printf("Couldn't close database.\n");
return false;
}
return true;
}
/**
* Return error code for last failed database operation.
*/
int sdbm_error() {
return err;
}
/**
* Is given key in database?
*/
bool sdbm_has( const char *key ) {
if (head == NULL) {
return false;
}
Key *curr = head;
lastHas = NULL;
beforeLastHas = NULL;
while (curr != NULL) {
if (!strcmp(curr->name,key)) {
lastHas = curr;
return true;
}
beforeLastHas = curr;
curr = curr->next;
}
return false;
}
/**
* Get value associated with given key in database.
* Return true on success, false on failure.
*
* Precondition: sdbm_has(key)
*/
bool sdbm_get( const char *key, char *value ) { //Errors: 6)Don't have key
if (!sdbm_has(key)) {
printf("Precondition sdbm_has(%s) failed", key);
err = 6;
return false;
}
readVal(value, lastHas->offset);
return true;
}
/**
* Update value associated with given key in database
* to given value. Return true on success, false on
* failure.
*
* Precondition: sdbm_has(key)
*/
bool sdbm_put( const char *key, const char *value ) {
if (!sdbm_has(key)) {
printf("Precondition !sdbm_has(%s) failed",key);
err = 7;
return false;
}
sdbm_remove(key);
sdbm_insert(key,value);
return true;
}
/**
* Insert given key and value into database as a new
* association. Return true on success, false on
* failure.
*
* Precondition: !sdbm_has(key)
*/
bool sdbm_insert( const char *key, const char *value ) { //Errors: 7)Already have key 8)Invalid key or value length
if (sdbm_has(key)) {
printf("Precondition !sdbm_has(%s) failed",key);
err = 7;
return false;
}
if (strlen(key) < MIN_KEY_LENGTH || strlen(key) > MAX_KEY_LENGTH || strlen(value) < MIN_VALUE_LENGTH || strlen(value) > MAX_VALUE_LENGTH) {
printf("Invalid key or value length");
err = 8;
return false;
}
listAdd();
tail->name = (char *)key;
tail->val = malloc(sizeof(*value));
strcpy(tail->val,value);
return true;
}
/**
* Remove given key and associated value from database.
* Return true on success, false on failure.
*
* Precondition: sdbm_has(key)
*/
bool sdbm_remove( const char *key ) {
if (!sdbm_has(key)) {
printf("Precondition !sdbm_has(%s) failed",key);
err = 7;
return false;
}
needNewDB = true;
if (beforeLastHas == NULL) {
head = lastHas->next;
}
else if (lastHas->next == NULL) {
tail = beforeLastHas;
}
else {
beforeLastHas->next = lastHas->next;
}
if (lastHas->val != NULL) {
free(lastHas->val);
}
free(lastHas->name);
free(lastHas);
return true;
}
There's a lot of errors in this code. To name just one:
filename = malloc(sizeof(*name));
*name is the first element of name, so it's a char, so sizeof(*name) == 1. To get the size of a string, use strlen(name) + 1. Better yet, use strdup if your system has it.
i would advise against global variables. you can not change the program (in the future) to use two of your databases in parallel.
so all your sdbm_xxx functions should get (or infer) all necessary values on their own.

Resources