Common Errors in C Programming and How to Fix Them
Verified
Added on 2022/11/29
|6
|1243
|342
AI Summary
This document discusses common errors in C programming and provides solutions for fixing them. It covers missing include statements, insufficient array length, unknown type name, missing cast to char pointer, and off by one errors. Each error is explained in detail, along with the steps to resolve it.
Contribute Materials
Your contribution can guide someone’s learning journey. Share your
documents today.
Question 1: The errors are indicated as comments in red below // Error 1 #include <stdio.h> #include <stdlib.h> #include <string.h> int main (void){ char source [11];// Error2: char *dest; size_t i;// Error 3: strcpy(source, "0123456789"); dest = (char*)malloc(strlen(source));//Error4: for (i = 1; i<10; i++){//Error5: dest[i] = source [i]; } dest[i] = '\0'; printf("dest = %s", dest); return 0; } The 5 errors Error 1: Missing include statements Error 2: Insufficient array length, should be 11 instead of 10. If unresolved, results to null termination problem Error 3: Unknown type name ‘size_t’, resolved by importing stdlib.h Error 4: Missing cast to char pointer Error 5: Off by one error, should be i < 10 instead of i<=11 and counter be set to 0 to account for character at the first index (index 0)
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
Question 2: 2.1 Allocation of the 3 chunks of memory The first chunk of memory is allocated using the malloc(660) command which allocates a memory block of 660 bytes and saves the address as pointer named first The second chunk of memory is allocated using the malloc(220) command which allocates a memory block of 220 bytes and saves the address as pointer named second The third chunk of memory is allocated using the malloc(120) command which allocates a memory block of 120 bytes and saves the address as pointer named third The commandstrcpy(second, argv[1])copies the string value entered by the user and saves it in the second memory address referred to by pointer named second Memory chunk (referred by pointer name) ValueSize (in bytes) firstRandom values660 secondString value entered by user, e.g. “hello” 220 thirdRandom values120 2.2 Vulnerability of the program to a buffer overflow attack The 224 bytes payload results in a buffer overflow because the memory block referred to by pointersecondcan only accommodate 220 bytes. This will result in the value in address referred to by pointerthirdbeing overwritten by the value 4. Pointerthirdwill now hold value 4.
Question 3: 3.1 Normal run In a normal run, the input comprises of two values, str1 and s2 where; str1is an unsigned integer value (len) used to specify the number of characters to be copied str2is the original string text whose part/subset is going to be copied to a newly specified address (commentpointer) lenis used to capture the number of characters to be copied sizeis a cleaned version oflensuch that it eliminates the first four characters precompiled by the computer Sample of normal run illustrated above 3.2 Abnormal run (with segmentation fault) The segmentation fault occurs when the user enters an invalid value for the lenth e.g an alphabet instead of an unsigned integer The same error occurs when a value of less than 4 is entered resulting to a negative length. The fault can be prevented by checking the user’s input to ensure it is within the accepted range and only has integers
printf(format,&aaa,&aaa,&bbb); } Question 4 (2): The attacker is trying to deceitfully manipulate the addresses of variables aaa and bbb
Question 5: 5.1 Least Privilege Principles The Lease Priviledge Principle is a crucial concept in computer security involving the limiting of access rights for users to basic minimum permission needed for them to carry out their work. Also known as POLP (Principle of Least Privilege), users are only given read, write and execute permissions for only the resources they interact with during their jobs. In a password management system, the queries to access and or modify passwords should be filtered by the system to try and catch any attempts of reading or modifying passwords for others. All requested should be filtered by a parser program and only validated requests should be allowed through. 5.2 TOCTOU TOCTOU is an acronym standing for Time Of Check to Time Of Use. This is a type of software bug that arises when changes occur in a system between the time a check was conducted and the time the results of that check were used. To avoid TOCTOU race conditions that could occur during I/O operations, the program can be designed such that it makes use of recent security updates that hinder execution of TOCTOU. This includes isolating all updates to the file in process until all the changes are committed. 5.3 Example An attacker can send more data than the system set buffer can accommodate resulting to overwriting of non-buffer application. Such an attack over a network can enable the attacker to run executable code on the server and modify files.