Can someone tell me why I'm getting this error? How can I fix it? "collect2: error: ld returned 1 exit status" - c++11

This is a header file that I'm getting the error on.
#include <vector>
#include <fstream>
#ifndef CRYPTO_H
#define CRYPTO_H
// given a char c return the encrypted character
char encrypt(char c);
// given a char c retun the decrypted character
char decrypt(char c);
// given a reference to an open file, return a vector with the # of characters, words, lines
std::vector<int> stats(std::ifstream& infile);
#endif
Please let me know what you think.
Thanks!!

Related

Unexpected value appears on stack when attempting buffer overflow

I am trying to learn more about cyber security, in this case about buffer overflows. I have a simple code that I want to change flow of:
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
void win()
{
printf("code flow successfully changed\n");
}
int main(int argc, char **argv)
{
volatile int (*fp)();
char buffer[64];
fp = 0;
gets(buffer);
if(fp) {
printf("calling function pointer, jumping to 0x%08x\n", fp);
fp();
}
}
By using some tools I have determined that function pointer (fp) gets it value updated after 72 characters have entered the buffer. The function win() is located at value 0xe5894855 so after 72 characters I need to provide that value to buffer for it to jump to the desired function.
However I am facing this issue:
By putting Python3's print("A"*18*4 + "UH" + "\x89" + "\xe5") into input of given C code, I should be getting desired value 0xe5894855 in section marked with red. But instead, I am getting highlighted malformed hex from somewhere. (89 is getting extra C2 and incorrect e5 value is overflowing to next part of stack) (value in those parts of stack are zero initially, but changed into that once overflow is attempted).
Why is this happening? Am I putting hex values into C program incorrectly?
Edit: Still have not figured out why passing hex through python did not work, but I found a different method, by using Perl: perl -e 'print "A"x4x18 . "\x55\x48\x89\xe5"', which did work, and address I needed to jump to was also incorrect (which I also fixed)

'USB_Close': identifier not found... 'len': undeclared identifier...

I wrote a code that accesses LEDs on an FPGA. Anyway, I cannot successfully compile the following code in Visual Studio:
#ifdef STATS_LIBRARY_EXPORTS
# define LIBRARY_API __declspec(dllexport)
#else
# define LIBRARY_API __declspec(dllimport)
#endif
#include <windows.h>
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <conio.h>
#include <time.h>
#include <bitset>
#include <stdio.h>
#include "C:\Cypress\Cypress Suite USB 3.4.7\CyAPI\inc\CyAPI.h"
_declspec(dllexport) int excite_LED(bool start, int on) {
int i;
USB_Open();
for(i=0; i<100; i++) // blink the LEDs for a few seconds
{
USB_BulkWrite(2, &i, 1); // send one single byte (= the value of i) to FIFO2
Sleep(50); // and wait 50ms
BulkOutPipe2->XferData((PUCHAR)&i, len); // send one byte (the value of i) to FIFO2
//Send command to FPGA
//status = !BulkOutPipe2->XferData(fpgaCommunicator, fpgaCommunicatorBytes);
}
USB_Close();
}
I am getting the following errors:
left of '->XferData' must point to class/struct/union/generic type
identifier "USB_Open" is undefined
identifier "USB_Close" is undefined
identifier "USB_BulkWrite" is undefined
identifier "len" is undefined
identifier "BulkOutPipe2" is undefined
cannot open source file "stdafx.h"
'USB_Open': identifier not found
'USB_Close': identifier not found
'USB_BulkWrite': identifier not found
'len': undeclared identifier
'BulkOutPipe2': undeclared identifier
How can I fix my code to get rid of these errors?
BulkOutPipe2 is a common define for EndPoint 3 in many samples, but its not set in CyAPI.h, so you need to set it yourself:
#define BulkOutPipe2 USBDevice->EndPoints[3]
Which you need to initialise:
CCyUSBDevice *USBDevice = new CCyUSBDevice(NULL, ...);
Len is also not defined:
LONG len= 512000;
USB_BulkWrite (and other USB_) looks like usb_bulk_write, which is part of the libusb api. But you are trying to use with the CyApi (which is a lot different), so remove those method calls.

char * variable declaration

I just want to verify I got this right.
The copy from sr to ds2 gives an error. Is this because ds2 is considered "const"??
Thanks and hope this isn't a bore.
#include <stdio.h>
#include <string.h>
#include <malloc.h>
int main(void)
{
char *sr = "Hello World";
char *ds1 = (char*)malloc(100 * sizeof(char));
char *ds2 = "12345678901234567890";
// This statement works just fine
printf("%s\n", strcpy(ds1, sr));
// This gives error
strcpy(ds2, sr);
printf("%s\n", ds2);
return 0;
}
Here is a similar post
difference between char* and char[] with strcpy()
When you do this
char *ds2 = "12345678901234567890";
the compiler leaves the pointer pointing to a non-writable memory region.
With this line
// This gives error
strcpy(ds2, sr);
You are trying to do an strcpy into the non-writable memory.
You should also have a free for each malloc as you are allocating memory but not de-allocating it.

"struct has no member named" error with gcc on dev machine

#include <stdio.h>
#include <stdlib.h>
#include "ReadMethods.h"
int main(int argc,char * argv[])
{
DPDA WordChecker;
DPDA * WordCheckerPointer=&WordChecker;
WordChecker.DPDAFilename=(char*)malloc(25*sizeof(char));
WordChecker.DPDAInputFilename=(char*)malloc(25*sizeof(char));
WordChecker.DPDAOutputFilename=(char*)malloc(25*sizeof(char));
strcpy( WordChecker.DPDAFilename,argv[1]);
strcpy( WordChecker.DPDAInputFilename,argv[2]);
strcpy( WordChecker.DPDAOutputFilename,argv[3]);
readDPDA(argv[1],WordCheckerPointer);
readInputLines(argv[2],WordCheckerPointer,argv[3]);
return 0;
}
This is my code that gives error from mallocs until last strcpy() ,total 6 lines.The error is "DPDA has no member named DPDAFilename" and same for other fields for every malloc and strcpy linesthat i work on.Here is the part of header file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct tagRule{
char *startingState;
char symbolToPop;
char expectedInput;
char *endingState;
char symbolToPush;
}Rule;
typedef struct tagStackDPDA{
char * arrayOfSymbols;
int stackElementCount;
char * currentState;
}stackDPDA;
typedef struct tagDPDA{
char * alphabet;
char * stackSymbols;
char ** states;
char *startingState;
char **finalStates;
int finalStatesAmount;
Rule * ruleList;
stackDPDA stackOfDPDA;
int sizeArray[4];//This array holds amount values of states,alphabet symbols,stack symbols and transition rules
char *DPDAFilename;
char *DPDAInputFilename;
char *DPDAOutputFilename;
}DPDA;
The code works fine in codeblocks environment but in gcc (-Wall -ansi).Those filenames come from input text files yet i am not sure it can cause this error.
Edit:By the way I am using this command line to compile;
gcc -Wall -ansi main.c ReadMethods.h -o WordChecker
May be if you compile in C mode, you have to use C-style comments in header?
/**/ instead of //

Using strcmp to compare argv item with string literal isn't working as I was expecting

I'm quite new to Visual C++ so this might be a 'schoolboy' error, but the following code is not executing as I'd expected:
#include "stdafx.h"
#include <string.h>
int _tmain(int argc, _TCHAR* argv[])
{
if (strcmp((char*)argv[1], "--help") == 0)
{
printf("This is the help message."); //Won't execute
}
return 0;
}
The executable, named Test.exe is launched as follows
Test.exe --help
I was expecting the message This is the help message. but I'm not seeing it - debugging reveals that the if condition comes out as -1 and not 0 as I'd expect. What am I doing wrong?
OK, I've figured out what's going on. The argv[] array is declared as TCHAR*, which is a macro that adjust the type based on whether or not Unicode has been enabled for the project (wchat_t if it is or char if it is not). The strcmp function, which I was trying to use, is the non-Unicode string comparison while wcscmp is the Unicode equivalent. The _tcscmp function uses the appropriate string comparison function depending on the Unicode setting. If I replace strcmp with _tcscmp, problem solved!
#include "stdafx.h"
#include <string.h>
int _tmain(int argc, _TCHAR* argv[])
{
if (_tcscmp(argv[1], _T("--help")) == 0)
{
printf("This is the help message."); //Will execute :)
}
return 0;
}
The _T function converts the argument to Unicode, if Unicode is enabled.
See also: Is it advisable to use strcmp or _tcscmp for comparing strings in Unicode versions?

Resources