Created
September 25, 2020 11:05
-
-
Save atamrawi/e5a8170e96f4a2dc71598c80d6be9344 to your computer and use it in GitHub Desktop.
Assignment 2 - Problem 1 Source Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <assert.h> | |
#include <string.h> | |
#include <stdlib.h> | |
// maximum number of integers stored in buffer | |
const int MAX_BUF_SZ = 1024; | |
int srcBuffer[MAX_BUF_SZ]; // source buffer | |
int dstBuffer[MAX_BUF_SZ]; // destination buffer | |
// randomly fill buffer | |
void fillBuffer_srcBuffer() { | |
for(int i = 0; i < MAX_BUF_SZ; ++i) { | |
srcBuffer[i] = rand() * 100; | |
} | |
} | |
int main(int argc, char** argv) { | |
if(argc < 2) { | |
printf("Usage - ./bufferOverflow [Number of Integers to Copy]\n"); | |
return -1; | |
} | |
assert(sizeof(int)==4); // make sure we are working on machine with (sizeof(int) = 4) | |
fillBuffer_srcBuffer(); | |
int copySize = atoi(argv[1]); | |
if(copySize > MAX_BUF_SZ) { | |
printf("You cannot copy more than (%d) integers from our buffer\n", MAX_BUF_SZ); | |
return -1; | |
} | |
printf("About to copy (%u) integers from our buffer\n", copySize); | |
memcpy(dstBuffer, srcBuffer, copySize * sizeof(int)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is based on the examples from https://gist.github.com/nscooling/d99d78a29eecbfba84bd#file-bufferoverflow-c