#include #include /**** ** Exercise 1: What happens if we print str2 before assigning? ** ** Exercise 2: What happens if we do (man strcpy): ** str2 = strcpy(str2, str1); ** ** Exercise 3: What happens if we do (man strcpy): ** sPtr = strcpy(str2, str1); ** Is it valid? ** ** Exercise 4: What happens if we do: ** sPtr = str1; ** strcpy(str2,sPtr); ** Is it valid? ** ** Exercise 5: During strcmp, what happens if we enter a long string (>10 ** chars)? ** ** Exercise 6: What kind of error checking do the string functions (strcmp, ** strlen, strcpy) do? ** i) Passing an invalid pointer as input -- You will face this problem often ** ii) Exceeding the bounds of memory allocated to the string -- Occurs ** often ** iii) Exceeding the bounds of string length ** Write programs that demonstrate these vulnerabilities. ** Write preventive mechanisms to make sure that the programs do not crash ** (e.g., checking the length of string, length of memory allocated, etc) ****/ int main(int argc, char* argv[]){ char str1[] = "CS220"; char str2[10]; char *sPtr; int ret; printf("str1 =%s\n", str1); printf("str2 =%s\n", str2); // Whats the output? Ex 1 /** Length of the string **/ printf("Length of str1 =%d\n", strlen(str1)); /** Copy strings **/ strcpy(str2, str1); printf("\nAfter copy\n"); printf("===================\n"); printf("str1 =%s\n", str1); printf("str2 =%s\n", str2); /** strcmp **/ printf("Enter the string to be compared with string %s: ", str1); scanf("%s", str2); printf("\nComparing\n"); printf("===================\n"); ret = strcmp(str1, str2); printf("Comparing strings: %s and %s: ret=%d\n", str1, str2, ret); ret = strcmp(str2, str1); printf("Comparing strings: %s and %s: ret=%d\n", str2, str1, ret); return 0; }