fix: null termnination on file buffer

This commit is contained in:
jonathan santis
2025-05-19 14:56:34 +02:00
parent ff1b01e4be
commit d3e0c75f6c
4 changed files with 104 additions and 12 deletions

View File

@@ -112,7 +112,35 @@ int getStrAtPos(char *str,int fromPos,int toPos,char **name,int sizeName)
//printf("*name:%s\n",ptr_name); //printf("*name:%s\n",ptr_name);
return NO_ERROR; return NO_ERROR;
} }
/*
*Here we get a "pair" of data, parsed from *str, witch consists of a keyname and a keyvalue.
*These are written to the pointers at char **name and char **value.
*These pair can be in the form of:
* ex1:
* NAME=VALUE
* ^ ^
* | Here we have no delimiter this means rightDelimiterPos must be NULL
* This is the left delimiter
* ex2:
* name(value)
* ^ ^
* | This is the rightDelimiterPos and must be ')'
* This is leftDelimiterPos which must be '('
*
* Input:
* char *str: the line in the form of a string where a key value pair is stored
* char leftDelimiterPos: the left delimiter for example '='
* char rightDelimiterPos: the right delimiter can be NULL in most cases, if NULL we assume that there is only one delimiter, which is in this case the leftDelimiterPos
* Output:
* char **name: The address where we store the name of the Key
* char **value: The address where we store the key value
* int sizeName: for size checking against memory allocated at **name
* int sizeValue: for size checking against memory allocated at **value
*
* Return:
* will return NO_ERROR (0) if successfull.
* If not it will return the error of the subroutine
*/
int getNameValuePair(char *str,char leftDelimiterPos,char rightDelimiterPos,char **name,char **value,int sizeName,int sizeValue) int getNameValuePair(char *str,char leftDelimiterPos,char rightDelimiterPos,char **name,char **value,int sizeName,int sizeValue)
{ {
@@ -132,7 +160,7 @@ int getNameValuePair(char *str,char leftDelimiterPos,char rightDelimiterPos,char
} }
posDelimiter = (ptrDelimiter - str); posDelimiter = (ptrDelimiter - str);
printf("LenUntilDelimiter: %d",posDelimiter); printf("LenUntilDelimiter: %d",posDelimiter);
if((ret=getStrAtPos(str,0,posDelimiter-1,&ptr_name,MAX_LEN_SECTIONNAME)) == NO_ERROR) if((ret=getStrAtPos(str,0,posDelimiter-1,&ptr_name,sizeName)) == NO_ERROR)
{ {
printf("ptr_name:%s",ptr_name); printf("ptr_name:%s",ptr_name);
}else { }else {
@@ -145,7 +173,7 @@ int getNameValuePair(char *str,char leftDelimiterPos,char rightDelimiterPos,char
posEnd = rightDelimiterPos; posEnd = rightDelimiterPos;
} }
if((ret=getStrAtPos(str,posDelimiter+1,posEnd,&ptr_value,MAX_LEN_SECTIONNAME)) == NO_ERROR) if((ret=getStrAtPos(str,posDelimiter+1,posEnd,&ptr_value,sizeValue)) == NO_ERROR)
{ {
printf("ptr_name:%s\n",ptr_value); printf("ptr_name:%s\n",ptr_value);
}else { }else {
@@ -155,6 +183,8 @@ int getNameValuePair(char *str,char leftDelimiterPos,char rightDelimiterPos,char
return NO_ERROR; return NO_ERROR;
} }
int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount,int *returnedCount) int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount,int *returnedCount)
{ {
int state=ST_INIT; int state=ST_INIT;

View File

@@ -1,10 +1,66 @@
[PRINTER] [PRINTER]
ip=192.168.209.173 ip=192.168.209.173
[dööner] a=193
[oasdf]
df=123
[anothersection]
a=192
b=111 b=111
c=213 1234567891
ip=192.168.209.173
[PRINTER]
[PRINTER]
[PRINTER]
[PRINTER]
[PRINTER]
[PRINTER]
[PRINTER]
[PRINTER]
[PRINTER]
[PRINTER]
ip=192.168.209.173
a=193
b=111
1234567891
ip=192.168.209.173
ip=192.168.209.173
a=193
b=111
1234567891
ip=192.168.209.173
ip=192.168.209.173
a=193
b=111
1234567891
ip=192.168.209.173
ip=192.168.209.173
a=193
b=111
1234567891
ip=192.168.209.173
ip=192.168.209.173
a=193
b=111
1234567891
ip=192.168.209.173
ip=192.168.209.173
a=193
b=111
1234567891
ip=192.168.209.173
ip=192.168.209.173
a=193
b=111
1234567891
ip=192.168.209.173
ip=192.168.209.173
a=193
b=111
1234567891
ip=192.168.209.173
ip=192.168.209.173
a=193
b=111
1234567891
ip=192.168.209.173
ip=192.168.209.173
a=193
b=111
1234567891
ip=192.168.209.173

8
file.c
View File

@@ -48,7 +48,13 @@ int getFile(char *fname, char **strContent,int cbSize,long *neededSize)
fclose(hfile); fclose(hfile);
return FILE_ERROR_READ_MISMATCH; return FILE_ERROR_READ_MISMATCH;
} }
strContent[*neededSize]='\0'; //buffer end-1 = \0
//buffer end-2 = \n
//buffer end-3 = last character
printf("strcontent: %ld, [%c]\n",*neededSize,*(*strContent+ *neededSize-3));
*(*strContent + *neededSize -1) ='\0';
printf("after zero assign: %ld, %s\n",*neededSize,*(strContent+ *neededSize-1));
printf("content:%s",*strContent);
fclose(hfile); fclose(hfile);
return NO_ERROR; return NO_ERROR;
} }

2
test.c
View File

@@ -60,7 +60,7 @@ int main(void)
memset(content,0,neededSize); memset(content,0,neededSize);
if((ret=getFile("config.cfg",&content,neededSize,&neededSize))==NO_ERROR) if((ret=getFile("config.cfg",&content,neededSize,&neededSize))==NO_ERROR)
{ {
printf("Sucessfull read file into buffer:%s\n---\n",content); printf("Sucessfull read file into buffer:%s|\n---\n",content);
} }
else { else {
printf("Error on getFile:%d\n",ret); printf("Error on getFile:%d\n",ret);