Email: support@prominentessays.com | WhatsApp: +1 (256) 784 58535 | PLACE YOUR FIRST ORDER AND SAVE 15% USING COUPON:

4.8/5

Incorrect Buffer Size Calculation A C Example Miscalculating Buffer Size

/*

* Incorrect Buffer Size Calculation

A C++ example miscalculating buffer size. 

*/

#include <cstdlib>

#include <iostream>

#include <vector>

using namespace std;

string convertInput(string userString){

// i is an iterator, charIndex moves through characters in the buffer

int i, charIndex;

// if string is larger that 10 chars, then do not store this input

const int MAX_SIZE = 15;

if ( MAX_SIZE < userString.length() ){

return “string to large”;

}

// an array to store characters in memory as a buffer

char buffer[(5*sizeof(char) * MAX_SIZE) + 1] = {‘ ‘};

charIndex = 0;

for ( i = 0; i < userString.length(); i++ ){

char theChar = userString[i];

// some secial charcters are changed to html format

if( ‘&’ == theChar ){

buffer[charIndex++] = ‘&’;

buffer[charIndex++] = ‘a’;

buffer[charIndex++] = ‘m’;

buffer[charIndex++] = ‘p’;

buffer[charIndex++] = ‘;’;

} else if (‘<‘ == theChar ){

buffer[charIndex++] = ‘&’;

buffer[charIndex++] = ‘l’;

buffer[charIndex++] = ‘t’;

buffer[charIndex++] = ‘;’;

} else if (‘>’ == theChar ){

buffer[charIndex++] = ‘&’;

buffer[charIndex++] = ‘g’;

buffer[charIndex++] = ‘t’;

buffer[charIndex++] = ‘;’;

} else if (‘”‘ == theChar){

buffer[charIndex++] = ‘&’;

buffer[charIndex++] = ‘q’;

buffer[charIndex++] = ‘u’;

buffer[charIndex++] = ‘o’;

buffer[charIndex++] = ‘t’;

buffer[charIndex++] = ‘;’;

}

else buffer[charIndex++] = theChar;

}

return buffer;

}

// function to show all the stored keywords

void showKeywords(vector<string> keywords) {

int count = keywords.size();

cout << “There are a total of ” << count << ” html keywords.” << endl;

for (int i = 0; i < count; i++) {

cout << keywords[i] << endl;

}

// the program starts here

int main(int argc, char** argv) {

vector<string> keywords;

// put some starting values in the keywords vector

keywords.push_back(“HomeKit”);

keywords.push_back(“Interface”);

keywords.push_back(“Web Forum”);

showKeywords(keywords);

string newKeyword;

// run a loop for user to enter keywords.

while(true) {

cout << “Type a word to include in the html keyword data:” << endl;

getline(cin, newKeyword);

// if user hits enter without any input data, then break the loop

if (newKeyword.empty()) {

cout << “This program would now normally return to a main menu, but”

“since this is just an example, the application will now exit”;

break;

}

// run the convertInput function to check for html special chars

string output = convertInput(newKeyword);

// if the function sees that the string is too large, then inform user

if(output == “string to large”) {

cout << “keyword exceeds maximum length, please try again” << endl;

} else { // otherwise, store the converted string in the keywords vector

cout << endl;

keywords.push_back(output);

showKeywords(keywords);

cout << endl;

}

return 0;

}

Solution:

15% off for this assignment.

Our Prices Start at $11.99. As Our First Client, Use Coupon Code GET15 to claim 15% Discount This Month!!

Why US?

100% Confidentiality

Information about customers is confidential and never disclosed to third parties.

Timely Delivery

No missed deadlines – 97% of assignments are completed in time.

Original Writing

We complete all papers from scratch. You can get a plagiarism report.

Money Back

If you are convinced that our writer has not followed your requirements, feel free to ask for a refund.