Question

In: Computer Science

Consider the following code fragment and answer below 1. Read(fd, user entry, size of (user entry));...

Consider the following code fragment and answer below
1. Read(fd, user entry, size of (user entry));
2. Comp=memcmp(userEntry, correct password, stream( user Entry));
3. If (Comp!=0)
4. Return BAD_PASS

- identify any 3 problems with code 8 for each defect describe:

- A) what is this problem
- B) how it can be found (code, review, Static analysis.....)
- C) how it can be solved

Solutions

Expert Solution

Ans :

The 3 defects are:
1. read function call. The 3rd argument should be the number of bytes to be read from the file into the buffer and not the buffer size.
This problem can be found by static analysis.
It can be solved by replacing sizeof(userEntry) with n_bytes (or number of bytes to read ).

2. Usage of memcmp:
Since the code is just trying to compare user input to the password, it is best to use strcmp (string copare) instead of memcmp because generally passwords dont occupy multiple bytes of memory area.
This defect can be found by code review.
It can be solved by using strcmp function instead of memcmp>

3. The third parameter in the compare function should not be sent. This is because if ur password is say hello123 and ur input is say hello, this will compare only the length of user input which is 5 and will try to match only those many characters in password i.e. hello and not the entire string hello123. This is a defect and bug.
This can be foud only on testing different scenarios.
Solution for this is to have full matches of 2 strings and not provide length parameter

Thank you...


Related Solutions

[after §3.22 − easy] String Processing : Consider the following code fragment: 1 int a =...
[after §3.22 − easy] String Processing : Consider the following code fragment: 1 int a = 20; 2 int b; 3 double x = 3.5; 4 String s = "All"; 5 char ch; 6 7 x += a; 8 x--; 9 a /= 4 - 1; 10 b = s.length(); 11 b += 4; 12 s += "is well"; 13 ch = s.charAt(b); 14 System.out.println("a = " + a + ", b = " + b); 15 System.out.println("x = "...
Javascript. Consider the following code fragment, that is supposed to compute the pixel value let c...
Javascript. Consider the following code fragment, that is supposed to compute the pixel value let c = image.getPixel(x, y); const m1 = (c[0] + c[1] + c[2]) / 3; c = image.getPixel(x + 1, y); const m2 = (c[0] + c[1] + c[2]) / 3; image.setPixel(x, y, [m1 - m2, m1 - m2, m1 - m2]); Give three pairs of pixel values (x, y) = [?, ?, ?] and (x+1, y) = [?, ?, ?] in the input image, for...
3) Consider the following IA32 assembly language code fragment. Assume that a, b and c are...
3) Consider the following IA32 assembly language code fragment. Assume that a, b and c are integer variables declared in the data segment. movl a, %eax movl b, %ebx cmpl %ebx, %eax jge L1 movl %eax, %ecx jmp L2 L1: movl %ebx, %ecx L2: movl %ecx, c Write the C code which is equivalent to the above assembly language code. You don't need to include the variable declarations, a function or anything like that, just show the 1 to 4...
Write code in MIPS ,read tow number from the user that do the following: 1- multiply...
Write code in MIPS ,read tow number from the user that do the following: 1- multiply 2- Dividing 3- sum 4- average 5- minimum 6- maximum 7- print message to thank the user for using my program
Consider the following class and the main method below. Trace the code, then answer the questions...
Consider the following class and the main method below. Trace the code, then answer the questions on the right. public class SomeClass { private String aName; private int aNumber; private boolean amAwesome; public SomeClass(String name, int number){ aName = name; aNumber = number; amAwesome = true; } public SomeClass(String name, int number, boolean awesome){ aName = name; aNumber = number; amAwesome = awesome; } public void methodAwesome(int number){ if(amAwesome) aNumber += number - 5; amAwesome = !amAwesome; } public int...
The following code fragment is expressed in arm assembly code. Fill in the blanks, so that...
The following code fragment is expressed in arm assembly code. Fill in the blanks, so that it is equivalent to the following C code. int counter; int x = 5; int y = 6; for (counter =10; counter >0;counter--) IF(X==Y) Y = Y + 1 ; ELSE Y = Y + 2} Fill in the blanks in the following code: MOV__________ ;loop counter into r0-ten times round the loop MOV__________ ;Value of y loaded into r1 MOV__________ ;Value of x...
Write a program that will read user input, and do the following: 1. The user can...
Write a program that will read user input, and do the following: 1. The user can input letters [A-Z] as much as he wants (ignore case). 2. If the user input other than letters or two characters, stop the input process and start to print unduplicated sorted pairs such as the below examples: User input: A a e b d d D E a B 1 Output: AB AD AE BD BE DE User Input: a q w e dd...
Read the following article in the link below and answer the following questions ARTICLE (BELOW): Changes...
Read the following article in the link below and answer the following questions ARTICLE (BELOW): Changes to the elevational limits and extent of species ranges associated with climate change www.uam.es/personal_pdi/ciencias/jspinill/BIBLIOGRAFIA_CASO/ECOL_2005_8_1138_1146.pdf ========================================================================================= 1. What is the larger ecological issue that the authors would like to address (found in the first paragraph of the introduction)? 2. What is the specific study question and/or hypothesis (or hypotheses) addressed in the paper? 3. What did the authors do? Briefly (in 2 sentences max) describe...
The following code fragment is expressed in arm assembly code.Fill in the blanks, so that...
The following code fragment is expressed in arm assembly code. Fill in the blanks, so that it is equivalent to the following C code.int counter;int x = 5;int y = 6;for (counter =10; counter >0;counter--)IF(X==Y)Y = Y + 1 ;ELSEY = Y + 2}Fill in the blanks in the following code:MOV__________ ;loop counter into r0-ten times round the loopMOV__________ ;Value of y loaded into r1MOV__________ ;Value of x loaded into r2Next CMP ____________ ;assume r1 contains y and r2 contains...
consider the code; typedef struct { int size; int *nums; }Data; Complete the function below that...
consider the code; typedef struct { int size; int *nums; }Data; Complete the function below that creates and returns a new data type that contains a nums array with size randomly chosen integers. Ensure proper error checking. Data *getRandomData(int size) { //PUT CODE HERE //Create random ints for(int i=0; i<size; i++) d->nums[1] = (int)(rand()/(float)RAND_MAX*100); return d;
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT