Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Code The purpose of this program is to allow a user to continually * append text to a string, until CTRL-C is entered. *

C Code

The purpose of this program is to allow a user to continually * append text to a string, until CTRL-C is entered.

* The program needs to protect against integer overflow. * * 1. Detect when an overflow occurred. * 2. If an overflow occurs, discard the current string * and copy (not append) the new string to buffer. */

#include #include #include #include #include

#define MAX_SIZE UCHAR_MAX

typedef unsigned char str_size_t;

str_size_t size;

char *buffer;

/* * The purpose of this program is to allow a user to continually * append text to a string, until CTRL-C is entered. * * Example Output: * * Hit CTRL-C to Exit. * * Input -> abc * Output -> 3 {abc} * Input -> def * Output -> 6 {abcdef} * Input -> ^C * Output -> 6 {abcdef} * * The program needs to protect agains integer overflow. * * 1. Detect when an overflow occurred. * 2. If an overflow occurs, discard the current string * and copy (not append) the new string to buffer. */

void print_output() {

printf ("Output -> %u {%s} ", size, buffer);

}

void signal_handler (int sig) {

printf(" "); print_output(); exit(0); }

int main (int argc, char** argv) { int result; char *tmp;

signal(SIGINT, signal_handler); size = 0; buffer = malloc(MAX_SIZE); tmp = malloc(MAX_SIZE);

memset(buffer, 0, MAX_SIZE);

printf ("Hit CTRL-C to Exit. ");

while (1) { printf ("Input -> "); result = scanf(" %[^ ]", tmp); size = (size + strlen(tmp)); /* TODO - Solution */ buffer = realloc(buffer, size); strcat(buffer, tmp); print_output();

}

/* Any code here is unreachable. */ }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil

1st Edition

1558603921, 978-1558603929

More Books

Students also viewed these Databases questions

Question

Describe Table Structures in RDMSs.

Answered: 1 week ago