C++ EVP_EncriptUpdate rewriting stack? - c++

I have this code
.....
const EVP_CIPHER * cipher = EVP_des_ecb();
uint8_t ot_byte,st_byte;
EVP_CIPHER_CTX ctx;
int trash;
EVP_EncryptInit(&ctx,cipher, key, iv);
cout << size - offset << endl;
int i=0;
for (; i < size - offset ;i++){
check = read(input_fd,&ot_byte,1);
cout << (i < size - offset) << " " << i << endl;
EVP_EncryptUpdate(&ctx, &st_byte, &trash, &ot_byte, 1);
check = write(output_fd,&st_byte,1);
}
cout << (i < size - offset) << " " << i << endl;
close(output_fd);
close(output_fd);
the output is
702000
1 0
1 1
1 2
1 3
1 4
1 5
1 6
1 7
0 5019693
When I "comment off" the EVP update function, the loop goes through all 702000 iterations. Where is the mistake? Is there a possibility, that EVP somehow goes behind its buffer and corrupts stack data?

uint8_t type will be small, these functions return at least 8 bytes

Related

C++ array with random counts

I am stuck somewhere(have some problem with the random number) and I know it, but can't find where to fix...
Two void functions must be used; void randomNumbers(int numbers[][3], int rowSize), void randomCounts(int numbers[][3], int size, int counts[])
I can't put images to show how it should and does look like in .exe files as I just signed up today ...... Hope this works ;(
Expected Result:
//========================
// 7 6 5
// 2 1 1
// 6 7 2
// 9 3 3
// 8 1 1
//========================
//Ran. Number: 0 1 2 3 4 5 6 7 8 9
//Frequency(Counts): 0 4 2 2 0 1 2 2 1 1
What I DID:
//========================
// 0 0 0
// 0 0 0
// 0 0 0
// 0 0 0
// 0 0 0
// ========================
// Ran. Number: 0 1 2 3 4 5 6 7 8 9
// Frequency(Counts): 001A148D
Code:
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
const int COL = 3;
const int SIZE = 5;
void randomNumbers(int inumbers[][3], int rowSize) {
int num = 0;
for (int i = 0; i < 10; i++) {
num = rand() % 10;
}
}
void randomCounts(int inumbers[][3], int size, int counts[]) {
for (int i = 0; i < 10; i++) {
counts[i]++;
cout << setw(5) << counts[i];
}
}
int main(){
int random[SIZE][COL] = {};
srand((unsigned)time(NULL));
cout << endl;
cout << "==================" << endl;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < COL; j++) {
cout << setw(5) << random[i][j];
if (j == COL - 1) {
cout << endl;
}
}
}
cout << "==================" << endl;
cout << endl;
cout << "Ran. Number: " << setw(5) << "0" << setw(5) << "1" << setw(5) << "2" << setw(5) << "3" << setw(5) << "4" << setw(5) << "5" << setw(5) << "6" << setw(5) << "7" << setw(5) << "8" << setw(5) << "9" << endl;
cout << "Frequency(Counts): " << randomCounts << endl;
return 0;
}
Ok, so why are you getting 0, 0, 0.... Because you never actually call your functions. You initialize your array:
int random[SIZE][COL] = {};
Then you print it here:
cout << setw(5) << random[i][j];
And nowhere in between do you set anything into this array. When you do start calling your functions you will find they don't work, due to copying the input and doing some undefined behaviour. When you have debugged this a bit more, ask a new question.

C++ array value printing inconsistent

When I print the value of the 2D array "need", I'm getting two different results. While in the initialization loop, I print out all the array elements, as shown in the output section below. At need[0][0], the output is 7 (top right of output section, "need[i][j]:00 7").
Then outside the loop, I try to directly call this element, but this time need[0][0] returns 0.
This is for a Banker's algorithm assignment, where the resources are specified in a text file that is then parsed by my program. I think this is the only relevant code section. I'm sure this is some sort of pointers problem, but I have never been instructed on C/C++ programming and I'm just sort of figuring it out on my own.
// Initialize the need matrix
need = new int*[numProc];
for (int i = 0; i < numProc; i++){
for (int j = 0; j < numResources; j++){
need[i] = new int[numResources];
cout << " max[i][j]:" << max[i][j];
cout << " allocation[i][j]:" << allocation[i][j];
need[i][j] = max[i][j] - allocation[i][j];
cout << " need[i][j]:" << i << j << " " << need[i][j] << endl;
}
}
cout << "need[0][0]" << need[0][0] << endl;
This is the output:
max[i][j]:7 allocation[i][j]:0 need[i][j]:00 7
max[i][j]:5 allocation[i][j]:1 need[i][j]:01 4
max[i][j]:3 allocation[i][j]:0 need[i][j]:02 3
max[i][j]:3 allocation[i][j]:2 need[i][j]:10 1
max[i][j]:2 allocation[i][j]:0 need[i][j]:11 2
max[i][j]:2 allocation[i][j]:0 need[i][j]:12 2
max[i][j]:9 allocation[i][j]:3 need[i][j]:20 6
max[i][j]:0 allocation[i][j]:0 need[i][j]:21 0
max[i][j]:2 allocation[i][j]:2 need[i][j]:22 0
max[i][j]:2 allocation[i][j]:2 need[i][j]:30 0
max[i][j]:2 allocation[i][j]:1 need[i][j]:31 1
max[i][j]:2 allocation[i][j]:1 need[i][j]:32 1
max[i][j]:4 allocation[i][j]:0 need[i][j]:40 4
max[i][j]:3 allocation[i][j]:0 need[i][j]:41 3
max[i][j]:3 allocation[i][j]:2 need[i][j]:42 1
need[0][0]0
need[i] = new int[numResources]; should be before for (int j...
You could have avoided this by not using manual memory management, e.g.
std::vector< std::vector<int> > need(numProc, numResources);
i didn't read that carefully, but i saw your allocation
need[i] = new int[numResources];
it runs numResources*numProc times.
// Initialize the need matrix
need = new int*[numProc];
for (int i = 0; i < numProc; i++) {
for (int j = 0; j < numResources; j++) {
need[i] = new int[numResources];
The previous line is executed numResources times for each cell need[i]. Profligate leakage of memory.
Also, it gives you zeroed memory, so of course it prints 0.!!!!
cout << " max[i][j]:" << max[i][j];
cout << " allocation[i][j]:" << allocation[i][j];
need[i][j] = max[i][j] - allocation[i][j];
cout << " need[i][j]:" << i << j << " " << need[i][j] << endl;
Where are max and allocation defined?
}
}
cout << "need[0][0]" << need[0][0] << endl;

best fit algorithm inplementation in overloaded new operator

I'm practising on how to implement the best fit algorithm by overloading the operator new.
#include<iostream>
#include<malloc.h>
using namespace std;
#define SUCCESS 0
#define FAILURE -1
#define MAX_PARTITION_SIZE 1000
int *avail_space,*needed_space,*used_space,*done_space,*free_space;
int index,j,k,partitions,nop,flag,total_avail;
int *ptr[10];
int no_obj;
class memory{
private:
public:
void set_data();
void create_mem();
int create_partitions();
int alloc_space();
int showdata();
int bestfit_search(int* index);
void* operator new(size_t size);
void operator delete(void* p);
};
void* memory :: operator new(size_t size){
void* p;
int index_no;
memory obj;
int i = 0;
cout << "In Overloaded new" << endl;
cout << "Size : - " << size << endl;
for(i = 0; i < no_obj; i++){
cout << "address :" << ptr[i] << endl;
}
cout << "Size : " << sizeof(memory) << endl;
needed_space[index] = sizeof(memory);
obj.bestfit_search(&index_no); // I guess the problem is in this function where I need to call a non-static member function.
cout << "Index_no - " << index_no << endl;
return ptr[index_no];
}
void memory :: operator delete(void* p){
cout << "In Overloaded delete" << endl;
free(p);
}
void memory :: set_data(){
total_avail = MAX_PARTITION_SIZE;
index = partitions = nop = flag = j = k = 0;
}
void memory :: create_mem(){
memory* obj[10];
int index;
cout << "Enter the no of objects : " << endl;
cin >> no_obj;
for(index = 0; index < no_obj; index++){
cout << "Creating obj : - " << index << endl;
obj[index] = new memory();
}
}
/* Allocate memory to all the pointer variables based on the no of partitions */
int memory :: alloc_space(){
avail_space = new int[partitions];
needed_space = new int[partitions];
used_space = new int[partitions];
done_space = new int[partitions];
free_space = new int[partitions];
if((NULL == avail_space) || (NULL == needed_space) || (NULL == used_space) || (NULL == done_space) || (NULL == free_space)){
return FAILURE;
}
return SUCCESS;
}
/* Get the no of partitions and the space needed for each of the partition */
int memory :: create_partitions(){
cout << "Enter the No of partitions to be created : " << endl;
cin >> partitions;
cout << endl;
/* allocate the space for the pointers */
if(FAILURE == alloc_space()){
return FAILURE;
}
/* Get the size of each partition and update the freespace*/
cout << "Enter the space for each of the partitions : " << endl;
for(index = 0; index < partitions; index++){
cout << "Partition : " << index << "\t";
cin >> avail_space[index];
free_space[index] = avail_space[index];
ptr[index] =(int*)malloc(avail_space[index]);
if(ptr[index] == NULL){
cout << "Memory allocation failed!" << endl;
return FAILURE;
}
cout << "ptr : [" << index << "] - " << ptr[index] << endl;
}
cout << endl;
return SUCCESS;
}
/* Display the available, used and free space*/
int memory :: showdata(){
cout << "Available space "<< "Used space "<<"Free space " <<"Done space "<<endl;
for(index = 0; index < partitions; index++){
cout << avail_space[index]<<"\t\t"<<used_space[index]<<"\t\t"<<free_space[index]<<"\t\t"<<done_space[index]<<endl;
}
return SUCCESS;
}
/* Algorithm to search for the best fit memory */
int memory :: bestfit_search(int* index_no){
if(NULL == index_no){
cout << "Invalid parameter!" << endl;
return FAILURE;
}
int space = 0;
for(index = 0; index < nop; index++){
space = needed_space[index];
flag = 0;
for(k = space; k < MAX_PARTITION_SIZE; k++){ // MAX_PARTITION_SIZE - 1000
for(j = 0; j < partitions; j++){
if((space == avail_space[j]) && (done_space[j] != 1) && (flag != 1)){
used_space[j] = needed_space[index];
free_space[j] = avail_space[j] - needed_space[index];
done_space[j] = 1;
cout << "Used space" <<used_space[j] << " freespace" << free_space[index] <<"done space " << done_space[index] <<endl;
flag = 1;
*index_no = j;
break;
}
}
if(flag == 1){
break;
}
space++;
}
}
showdata();
return SUCCESS;
}
/* Main */
int main(){
memory obj;
obj.set_data();
obj.create_partitions();
obj.create_mem();
return SUCCESS;
}
Program run:
Enter the No of partitions to be created :
5
Enter the space for each of the partitions :
Partition : 0 2
ptr : [0] - 0x9e62080
Partition : 1 7
ptr : [1] - 0x9e62090
Partition : 2 3
ptr : [2] - 0x9e620a0
Partition : 3 10
ptr : [3] - 0x9e620b0
Partition : 4 25
ptr : [4] - 0x9e620c0
Enter the no of objects :
2
Creating obj :
In Overloaded new
Size : 1
address :0x9e62080
address :0x9e62090
Size : 1
Available space Used space Free space Done space
2 0 2 0
7 0 7 0
3 0 3 0
10 0 10 0
25 0 25 0
Index_no - 0
Creating obj :
In Overloaded new
Size : 1
address :0x9e62080
address :0x9e62090
Size : 1
Available space Used space Free space Done space
2 0 2 0
7 0 7 0
3 0 3 0
10 0 10 0
25 0 25 0
Index_no - 1
I'm getting an wrong output as the available space 2 and 3 has to be occupied with used space as 1.

Printing list backwards

I am having a problem with this program which is to print out a list forwards and backwards, however When I print out the list backwards the first number in the list is a random massive number rather than the right number. e.g.
0 1 2 3 4 5 6 7 8 0
4286398 8 7 6 5 4 3 2 1 0
can anyone explain what is wrong with my code please.
Also Can anyone tell me how I could pass the counter from the printList function to a new function called checkList() so that the counter has the same value in checkList() as what it is at the end of printList().
code:
void printList(int array1[]){
int counter = 0;
int x;
ifstream theFile("list.txt");
while(theFile >> x){
array1[x] = x;
cout << array1[x] << " ";
counter = counter + 1;
}
cout << endl << counter << endl;;
int n = counter;
for(int i = n -1; i >= 0; i--){
cout << array1[i] << " ";
}
Here's the culprit:
array1[x] = x;
If your array input values are 0 1 2 3 4 5 6 7 8 0, then at the last iteration of your loop you're doing array1[0] = 0. That overwrites the first item in your array, while incrementing the counter. Then, when you reverse it array[9] contains garbage value because you never set it.
You have a problem because of the line array1[x]=x;. Your code would actually work if the numbers in your file were 0..9, but the final number is another 0, so you don't set array1[9] to anything.
You should have some variable for indexing the array, something like:
int counter = 0;
while(theFile >> x){
array1[counter] = x;
cout << array1[counter] << " ";
counter = counter + 1;
}
you are doing
array1[0] = 0;
array1[1] = 1;
array1[2] = 2;
array1[3] = 3;
array1[4] = 4;
array1[5] = 5;
array1[6] = 6;
array1[7] = 7;
array1[8] = 8;
array1[0] = 0; // here
array1[9] is uninitialized
You have some serious issues in the code:
ifstream theFile("list.txt");
while(theFile >> x){
array1[x] = x;//^^this is evil
cout << array1[x] << " ";
counter = counter + 1;
}
cout << endl << counter << endl;;
//^^extra colon, though not wrong here but not good practice
You read from file and fill the array, in your special case, you have:
0 1 2 3 4 5 6 7 8 0
You have 10 elements, but your array1 will end up with 9 since the last read was 0 and array1[0] was written as 0 again. So when you output your array1, you will never get 10 numbers since your array actually stores 9 numbers. that's why you saw garbage value if you try to access array1[9], which value has not been filled, some garbage raw memory value.
Instead, you can try to do the following:
int counter = 0;
int x;
ifstream theFile("list.txt");
while(theFile >> x){
array1[counter] = x;
cout << array1[counter] << " ";
counter = counter + 1;
}
cout << endl << counter << endl;;
You are counting wrong upwards and eventually hit uninitialized memory AFTER your array. You should pass the length of your array as a parameter to your function.
As arrays decay to pointers you won't be able to recover its length.
void printList(int array1[], into size){ }
Then thou don't need to figure out its length so complicated.

How to access each byte in an integer pointer?

I know that if the integer is dissected rather than being computed as a whole, the total of individual bytes will yield incorrect result. However, for curiosity, I want to examine individual byte and make.I'm not sure if this is correct to inspect each byte in an integer pointer:
#include <iostream>
int main(int argc, char *argv[])
{
using namespace std;
int *num = new int;
*num = 123456789;
cout << "Num: " << *num << '\n';
char* numchar_ptr = reinterpret_cast<char*> (num);
for (int i = 0; i < 4; ++i)
{
cout << "number char: " << i << ' ' << (short) *(numchar_ptr+i) << '\n';
*(numchar_ptr+i) = i
}
cout << "New num: " << *num << '\n';
delete num;
return 0;
}
According to the loop, the bytes in the integer will be: 0 1 2 3
which is equal to 00000000 00000001 00000010 00000011 in binary and 66051 in decimal
But I got the result "New num" is 50462976. Why?
Read carefully the wikipedia page on endianness
You need to take endianness into account. On your system, numbers are stored in little-endian representation, which means that the lowest-addressed byte is the least-significant.
Therefore, your number is:
0 * (1 << 0)
+ 1 * (1 << 8)
+ 2 * (1 << 16)
+ 3 * (1 << 24)
which is 50462976.