I have a function that walks through a message string. The message string has a begin indicator, and an END indicator en a Separator to separate the key=value pairs in the string. It should return a linked list to struct's that each contain a key value pairs so it can be processes elsewhere in the program.
I will describe briefly what happens. If I call the function with a teststring, it processes a few key value pairs, then after processing two key value pairs it does not seem to return from the malloc call, that I have marked red and bold.
I am aware that this may not be detailed enough, but I can post additional code/information if needed. Maybe one of the giants here spots the mistake in this code already.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "messaging.h"
struct keyValuePair {
int key;
char *value;
struct keyValuePair *next;
struct keyValuePair *parseMessageString(char *aMessageString) {
//set walker to the first position of the first key of the message
char *walker = aMessageString + strlen(BEGIN);
char *end = strstr(aMessageString, END); //this ptr is set at the end of the messg
char *sep; //
char *key = malloc(MAXFIELD * sizeof(*key + 1));
char *value;
int i = 0; //index for key and value pointers
int type; //value for fieldtype
struct keyValuePair *firstPair = NULL;
struct keyValuePair *prevPair;
struct keyValuePair *currPair;
//walk through message string, convert keys into integer
//and store key and value in the linked list firstPair
while (walker != end) {
//get key digits untill you encounter '='
i = 0;
while ((*walker != '=')) {
key[i] = *walker;
++walker;
++i;
}
key[i] = '\0'; //key[] contains key now
type = atoi(key);
printf("key= %i \n", type);
//set sep to next separator or the end for next while loop
sep = ((strstr(walker, SEP) == NULL) ? end : strstr(walker, SEP));
//store the value
walker++; //was on the = sign
i = 0;
value = malloc(MAXFIELD * sizeof(*value + 1));
if (value == NULL) printf("\nMalloc issue\n");
while (walker != sep)
value[i++] = *(walker++);
value[i] = '\0';//value[] contains now the value
printf("VAL value= %s\n", value);
currPair = malloc(sizeof(currPair));
printf("namallod\n");
if (currPair == NULL) printf("\nMalloc Error\n");
currPair->key = type;
currPair->value = value;
currPair->next = NULL;
printf("CURR key= %i value= %s\n", type, value);
//add cuurPair to the linked list
if (firstPair == NULL) {
firstPair = currPair;
prevPair = currPair;
}
else {
prevPair->next = currPair;
prevPair = currPair;
}
if (sep != end)
walker += strlen(SEP);
}
return firstPair;
}