Question

In: Computer Science

The following code takes in a command line argument and copies it into a buffer before...

The following code takes in a command line argument and copies it into a buffer before working on it. Explain in detail any problems that you see in the following C code and how you would fix it. Note only describe how you would fix it, do not actually rewrite or give me another version of the code.

void bad_function(char *input)
{
char dest_buffer[32];
char input_len = strlen(input);
if (input_len < 32) {
strcpy(dest_buffer, input_len);
printf(“Command line argument is %s.\n”,dest_buffer);
}
else {
printf(“Error - input is too long for buffer.\n”);
}
// Works with the dest_buffer here
}

Solutions

Expert Solution

Please up vote ,comment if any query . Thanks for Question . Be safe .

Answer :

strcpy(dest_buffer, input_len); //error line

strcpy(dest_buffer, input);    //this will fix program

Explanation :

strcpy function copy data of source char array to destination array .

strcpy(destination array ,source array)

both argument should be array not some not variable

but in above program we are passing a char variable instead of char array .

strcpy(dest_buffer,input) //source and destination argument are char array .

now code will copy input array or command line array to dest_buffer array if length of input array is less than 32 .

Fixed Code :


void bad_function(char *input)
{
char dest_buffer[32];
char input_len = strlen(input);
if (input_len < 32) {
strcpy(dest_buffer, input);
printf("Command line argument is %s.\n",dest_buffer);
}
else {
printf("Error - input is too long for buffer.\n");
}
// Works with the dest_buffer here
}

Please up vote ,comment if any query .


Related Solutions

Python Write a program that takes a text filename as command line argument, and prints number...
Python Write a program that takes a text filename as command line argument, and prints number of times each letter occurred in this file.
Write a C program called test that takes one command line argument, an integer N. When...
Write a C program called test that takes one command line argument, an integer N. When we run test: ./test N the program will do this: the parent process forks N child processes each child process prints its process ID, exits the parent process waits for all child processes to exit, then exits
A C program that accepts a single command line argument and converts it in to binary...
A C program that accepts a single command line argument and converts it in to binary with array length of 16 bits. The array should contain the binary of the int argument. the program should also convert negative numbers. Side note the command line arg is a valid signed int.
UNIX ONLY Write a bash script that will accept a filename as a command line argument....
UNIX ONLY Write a bash script that will accept a filename as a command line argument. Your script should first check whether the filename has been passed as argument or not (hint: at least one argument has to be provided). If no argument has been provided your script should display appropriate message and exit. If a file name has been provided, your script should check if the file exists in the current directory and display the contents of the file....
Write a C program that accepts a port number as a command line argument, and starts...
Write a C program that accepts a port number as a command line argument, and starts an HTTP server. This server should constantly accept() connections, read requests of the form: GET /path HTTP/1.1\r\n\r\n read the file indicated by /path, and send it over the "connect" file descriptor returned by the call to accept().
Please look at the following code. When I run it from the command line, I am...
Please look at the following code. When I run it from the command line, I am supposed to get the following results: 1: I am 1: I am I need to fix it so that the functions 'print_i' and 'print_j' print all of their lines. What do I need to add? Thank you. C source code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <unistd.h> // These two functions will run concurrently void* print_i(void *ptr) { printf("1: I am...
Write a program that takes two command line arguments at the time the program is executed....
Write a program that takes two command line arguments at the time the program is executed. You may assume the user enters only decimal numeric characters. The input must be fully qualified, and the user should be notified of any value out of range for a 23-bit unsigned integer. The first argument is to be considered a data field. This data field is to be is operated upon by a mask defined by the second argument. The program should display...
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to...
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to generate a random sequence of integers be- tween 0 and N – 1. Run experiments to validate the hypothesis that the number of integers generated before the first repeated value is found is ~√?N/2.
IN PYTHON: Compose a recursive program to draw Sierpinski triangles. Use a command-line argument to control...
IN PYTHON: Compose a recursive program to draw Sierpinski triangles. Use a command-line argument to control the depth of the recursion.
Variation on the ubiquitous "hello, world!" program. One argument from the command line may be passed...
Variation on the ubiquitous "hello, world!" program. One argument from the command line may be passed to the main method. If an argument is to be used, it must have a value of "-h" or "-w". If "-h" is passed to the main method as an argmument, the output of the program is"Hello". If "-w" is passed as an argument to the main method, the out of the program is "World". If no arguments are passed to the main method,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT