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.
1. Write a queue client, "LineNum," that takes an integer command line argument “n” and prints...
1. Write a queue client, "LineNum," that takes an integer command line argument “n” and prints the nth string from the first string found on standard input. [MO6.2] Please note that you would need to use a queue to implement it for full credit. You should add the strings inputted by the user to a queue using the enqueue method. Then you should remove and return "n" strings from the queue using the dequeue method. The nth string that is...
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.
The following .py script takes a users input of search parameters on command line, creates a...
The following .py script takes a users input of search parameters on command line, creates a custom search URL, then returns top 5 result links from Google Scholar. Alter the below code so that the script opens up each found link in a separate browser window (or tabbed windows in a single browser). ——.py import requests, sys, webbrowser from bs4 import BeautifulSoup print("Searching for " + "+".join(sys.argv[1:])) myres = requests.get("https://scholar.google.com/scholar? hl=en&q=" + "+".join(sys.argv[1:]) + "&*") soup = BeautifulSoup(myres.text, "html.parser") elems...
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().
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.
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. Both arguments are to be numerical values restricted to 22-bit unsigned integers. The input must be fully qualified, and the user should be notified of any errors with the input provided by the user. 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT