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 = "...
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
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...
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...
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 following fragment of C code: for (i=0; i<100; i++) { A[i]=B[i]+C; } Assume that...
Consider the following fragment of C code: for (i=0; i<100; i++) { A[i]=B[i]+C; } Assume that A and B are arrays of 64-bit integers, and C and i are 64-bit integers. Assume that all data values and their addresses are kept in memory (at addresses 1000, 3000, 5000, and 7000 for A, B, C, and i, respectively) except when they are operated on. Assume that values in registers are lost between iterations of the loop. Assume all addresses and words...
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...
5.29) Explain what the following fragment of code achieves. Note that the data is signed and...
5.29) Explain what the following fragment of code achieves. Note that the data is signed and that the packed shift right arithmetic instruction operates on word (16-bit) operands. MOVQ MM0, MM1 PSRAW MM0, 15 PXOR MM0, MM1 5.30) Consider the following block of operations that might be found inside a loop. Explainf what the instructions do and what operation is being performed on the data. MOVQ MM1, A ; move 8 pixels of image A MOVQ MM2, B ; move...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT