Exemple #1
0
/* int insensitiveStrCmp(char * str1, char * str2) summary
* Compare 2 strings without taking the letters state (uppercase/lowercase) in account, return 0 if same, <0 if str1 smaller, otherwise >0
* param: char * str1 | first string to compare
* param: char * str2 | second string to compare
*/
int insensitiveStrCmp(char * str1, char * str2) // T: you didnt use this function at all
{
	int ret_val;
	char * _str1;
	char * _str2;
	_str1 = stringMalloc(strlen(str1)); // T: check parameters are not null pointers and check for stringMalloc failure
	_str2 = stringMalloc(strlen(str2)); // T: dont assume str1 and str2 are null terminated
	strcpy(_str1, str1);
	strcpy(_str2, str2);
	ret_val = strcmp(stoLower(_str1), stoLower(_str2));
	free(_str1);
	free(_str2);
	return ret_val;
}
int main(void)
{
	printf("%s\n", stringMalloc());
	printf("%s\n", stringLiteral());
	printf("%s\n", stringLocal());
	return 0;
}
Exemple #3
0
/* char * getWord(char * content, unsigned int start_index, unsigned int contentSize, unsigned int * wordSize_p) summary
* Get a word from a string of content and return it as a string
* param: char * content | content to find a word in
* param: unsigned int start_index | index to start search word from
* param: unsigned int contentSize | content size
* param: unsigned int * wordSize_p | pointer of unsigned int to save word size in
*/
char * getWord(char * content, unsigned int start_index, unsigned int contentSize, unsigned int * wordSize_p)
{
	char * ret_val;
	*wordSize_p = 0;

	while (start_index + *wordSize_p < contentSize && isalpha(content[start_index + *wordSize_p]) != 0)
	{
		++(*wordSize_p);
	}

	ret_val = stringMalloc(*wordSize_p); // T: chaeck ret_val is not null

	for (unsigned int i = 0; i < *wordSize_p; ++i)
	{
		ret_val[i] = content[start_index + i];
	}

	return ret_val; // T: you count on stringMalloc to put \0 at the end of string - a bit weird.. 
}
Exemple #4
0
/* char * getFileContent(FILE * f, unsigned int * fsize_p) summary
* Get the content of file and return it as a string
* param: FILE * f | file to get content of
* param: unsigned int * fsize_p | pointer of unsigned int to save file size in
*/
char * getFileContent(FILE * f, unsigned int * fsize_p)
{
	int resize_content = 0;
	char * ret_val;
	*fsize_p = getFileSize(f); // T: check parameters are not null pointers
	ret_val = stringMalloc(*fsize_p); // T: what happend on stringMalloc failure?
	fread(ret_val, sizeof(char), *fsize_p, f); // T: check failure of fread, why you read the file as binary and not as text?
	for (unsigned int i = 0; i < *fsize_p; i++) // T: what will happen if *fsize is -1 ? how this vulnerability called? how you can fix it?
	{
		if (ret_val[i] == '\n')
		{
			resize_content++;
		}
	}
	*fsize_p -= resize_content; // T: why you sub every \n from the string size?
	ret_val[*fsize_p] = '\0';

	return ret_val;
}