Question

In: Computer Science

The following functions have zero or more security issues given the way they are called in...

The following functions have zero or more security issues given the way they are called in the main() function. Identity these security issues and propose a compliant fix. You don’t need to consider the issues in the main() function.

1. setValue set the element at index in arr to value.

void setValue ( int * arr , int len , int value , int index ) {

                arr [ index ] = value ;

}

int main ( void ) {

                int arr [10];

                int value ;

                int index ;

                printf (" Enter the value :\ n ");

                scanf ("% d " , & value );

                printf (" Enter the position :\ n ");

                scanf ("% d " , & index );

                setValue ( arr , 10 , value , index );

                return 0;

}

2. createArray creates an int array of size len. It, then, initializes each element in the array with value.

int * createArray ( int len , int value ) {

                int * arr = ( int *) malloc ( sizeof ( int ) * len );

                memset ( arr , value , sizeof ( int ) * len );

                return arr ;

                }

int main ( void ) {

                int value ;

                int len ;

                printf (" Enter the value :\ n ");

                scanf ("% d " , & value );

                printf (" Enter the length :\ n ");

                scanf ("% d " , & len );

                int * arr = createArray ( len , value );

                if ( arr != NULL ) {

                                if ( len > 0) {

                                printf (" The first element is % d \ n " , arr [0]);

                                }

                free ( arr );

                }

return 0;

}

3. writeToFile asks the user to input a string and then write the entered string to “out.txt”.

void writeToFile () {

                char buffer [20];

                printf (" Enter the content :\ n ");

                scanf ("% s " , buffer );

                FILE * f = fopen (" out . txt " , " w ");

                fputs ( buffer );

                fclose ( f );

                }

int main ( void ) {

                writeToFile ();

                return 0;

}

4. sum returns the sum of two integers.

long long sum ( int a , int b ) {

                return a + b ;

                }

int main ( void ) {

                int a = 0;

                int b = 0;

                printf (" Enter a :\ n ");

                scanf ("% d " , & a );

                printf (" Enter b :\ n ");

                scanf ("% d " , & b );

                printf (" sum of a and b is % ld \ n " , sum (a , b ));

                return 0;

}

5. swap swaps the integer value stored in a and b.

void swap ( int * a , int * b ) {

                * a += * b ;

                * b = * a - * b ;

                * a = * a - * b ;

                }

int main ( void ) {

                int a = 0;

                int b = 0;

                printf (" Enter a :\ n ");

                scanf ("% d " , & a );

                printf (" Enter b :\ n ");

                scanf ("% d " , & b );

                swap (& a , & b );

                printf (" a is %d , b is % d \ n " , a , b );

                return 0;

}

Solutions

Expert Solution

1)

void setValue ( int * arr ,const int len ,const int value ,const int index ) {

if(index < len)

{

  arr [ index ] = value ;

}

               

}

The SetValue here sets a given value at the index,The security issue here is that whenever index is greater than length of the array then it can cause the system to crash.To avoid it we can add a check,Also adding the const qualifier to the arguments ensure that these are not modified inside the function.

2)

int * createArray ( const int len , int value ) {

//We can check whether len is greater than 0

if(len > 0)

{

int * arr = ( int *) malloc ( sizeof ( int ) * len );

                memset ( arr , value , sizeof ( int ) * len );

}              

                return arr ;

                }

3)

void writeToFile () {

                char buffer [20];

                printf (" Enter the content :\ n ");

//We can use scanf_s instead of scanf as it is more secured and prevents buffer overflow

                scanf ("% s " , buffer );

                FILE * f = fopen (" out . txt " , " w ");

                fputs ( buffer );

                fclose ( f );

                }

4)

//We can add the const to the parameters to prevent it from changing

long long sum (const int a ,const int b ) {

                return a + b ;

                }

5)

//We can add null check before de-referencing the pointer a and b

void swap ( int * a , int * b ) {

if(a == NULL || b == NULL)

return;

                * a += * b ;

                * b = * a - * b ;

                * a = * a - * b ;

                }


Related Solutions

In C++ Create an abstract class called Shape Shape should have the following pure virtual functions:...
In C++ Create an abstract class called Shape Shape should have the following pure virtual functions: getArea() setArea() printArea() Create classes to inherit from the base class Circle Square Rectangle Both implement the functions derived from the abstract base class AND must have private variables and functions unique to them like double Radius double length calculateArea() Use the spreadsheet info.txt read in information about the circle, rectangle, or square text file: circle   3.5   square   3   rectangle   38   36 circle   23  ...
Vertebrates have a gene called Pax6, and this gene codes for a protein that functions as...
Vertebrates have a gene called Pax6, and this gene codes for a protein that functions as a transcription factor. Where-ever Pax6 protein is produced in young embryos, an eye develops. a. Where would the Pax6 gene be normally expressed in this embryo? b. If one could cause the Pax6 gene to be expressed on the stomach of an embryo, what do you suppose would happen? c. Pax 6 protein is a transcription factor. What does that mean in terms of...
“A low-coupon security is more price-volatile [in response to a given change in yield that a...
“A low-coupon security is more price-volatile [in response to a given change in yield that a high-coupon security.” True or false, or what? Explain carefully.
As we know, dollar-weighted average return is a more accurate way to measure security return performance...
As we know, dollar-weighted average return is a more accurate way to measure security return performance if we buy or sell securities from time to time. Suppose you have the following price information for a risky security for several recent years. Assume that the stock pays no dividends Year Beginning of year price # of share bought or sold 2017 $50 100 bought 2018 $55 50 bought 2019 $51 75 sold 2020 $54 75 sold      What is the dollar-weighted average...
You are consultants who have been called in to address a significant security breach in a...
You are consultants who have been called in to address a significant security breach in a medium sized manufacturing company. Detailed in the following link the organization was forced to halt the production of boots because a dismissed IT employee took steps to sabotage. https://www.bleepingcomputer.com/news/security/sysadmin-gets-18-months-in-prison-for-shutting-down-former-employers-network/ Your goal is to recommend a security policy and associated implementation of this policy (procedures supported by standards and guidelines) that limits or eliminates the security risks associated with departing employees. task is to “slam...
A researcher believes that maximum security inmates have a more lengthy arrest record than minimum security...
A researcher believes that maximum security inmates have a more lengthy arrest record than minimum security inmates. She collects a sample of 25 inmates from the minimum security prison and 32 inmates from the maximum security prison and computes the following data from the samples:                                                 Mean                           Standard Deviation Minimum:                   5.8                                         2.7 Maximum:                  6.2                                         1.9 1.Calculate the 95% CI for each group. Based on these data, is the researcher correct that maximum security inmates have a longer arrest records than...
Given the following functions, can you have the corresponding a) Fourier series, b) Fourier transform and...
Given the following functions, can you have the corresponding a) Fourier series, b) Fourier transform and c) Laplace transform? If yes, find them, if not, explain why you can not. A, x(t) = -1+cos(2t) + sin(pai*t+1)                                               (4-1) B, x(t) = 2d(t) cos(2t) +d(t-1.5p) sin(2t)                                          (4-2) C, x(t) = 1+cos(1.5t) + cos(4t)                                           (4-3)
Given the following functions, can you have the corresponding a) Fourier series, b) Fourier transform and...
Given the following functions, can you have the corresponding a) Fourier series, b) Fourier transform and c) Laplace transform? If yes, find them, if not, explain why you can not. A, x(t) = -1+cos(2t) + sin(pt+1)                                                                                 (4-1) B, x(t) =2d(t) cos(2t) +d(t-1.5p) sin(2t)                                                                    (4-2) C, x(t) = 1+cos(1.5t) + cos(4t)                                                                                    (4-3)
The following returns have been estimated for Security T and Security S: Scenario Security T Security...
The following returns have been estimated for Security T and Security S: Scenario Security T Security S 1 20% 10% 2 13% -6% 3 15% 20% Each scenario is equally likely to occur, and you plan to invest 70% in Security T and 30% in Security S. What is the standard deviation of the rate of return of the portfolio? Round your answer to the nearest tenth of a percent. A) 0.0% B) 4.5% C) 19.9% D) 59.7%
The following returns have been estimated for Security T and Security S: Scenario Security T Security...
The following returns have been estimated for Security T and Security S: Scenario Security T Security S 1 20% 10% 2 13% -6% 3 15% 20% Each scenario is equally likely to occur, and you plan to invest 70% in Security T and 30% in Security S. What is the standard deviation of the rate of return of the portfolio? Round your answer to the nearest tenth of a percent. A) 0.0% B) 4.5% C) 19.9% D) 59.7%
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT