setbuf function in the following seems ok - linux

This is from man page. Please explain why it is invalid! Thanks!
You must make sure that the space that buf points to still exists by the time stream is closed, which also
happens at program termination. For example, the following is invalid:
#include <stdio.h>
int
main(void)
{
char buf[BUFSIZ];
setbuf(stdin, buf);
printf("Hello, world!\n");
return 0;
}

Related

I am unable to find out the memcheck error when i using valgrind on the given code

I am unable to detect the memory error(memcheck error) . when i run the code i see some unexpected output came. so please describe what is happening in thes code.
#include <stdio.h>
#include <stdlib.h>
char *getString()
{
char message[100]="Hello World";
char *ret = message;
return ret;
}
void test4()
{
printf("String: %s",getString());
}
int main()
{
test4();
return 0;
}
Your variable message is local to the function getString and is on the stack. After getString returns, message no longer exists, meaning that the pointer returned by getString, which was set to point to message is no longer valid.

Linux Pipes and Redirecting output

I'm trying to write a linux program using pipes, but so far I've encountered a major problem.
When I try to run this, it seems it either duplicates the answers, or doesnt give me an answer at all.
So far I'm trying to use a pipe, the parent gets a string from the keyboard, and compares it to see if matches any other commands, momentarily its only the "login" command.
But it doesnt work as it doesnt show me a fail or success message. I've been fiddeling with the code, but sometimes it's repeating the answer several time, like it's executing the child several times. Can someone explain me why its happening?
Thx
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
int fd[2], nbytes;
pid_t childpid;
char input[12];
char readbuffer[80];
int log_variable;
int pid;
int compare(char str1[], char str2[]){
if(strlen(str1) == strlen(str2))
{int i;
for( i=0; i<strlen(str1); i++){
if(str1[i] != str2[i])
return 0;
return 1;
}
}
}
int test(char argument[]){//test function
pipe(fd);
switch(childpid=fork()){
case -1:
perror("fork -1\n");
exit(1);
case 0://child
close (fd[1]);
int nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
if(compare(readbuffer, "login") == 1){
return 1;
}
else if(compare(readbuffer, "login") == 0){
return 0;
}
exit(1);
default:
//parent
close(fd[0]);
write(fd[1], argument, sizeof(argument));
while(wait(NULL)!=-1);
}
}
main(){
while(1){
printf("Insert command: \n");
scanf("%s", input);
logs=(test(input));
if(logs == 1) {printf("success\n"); break;}
else if(logs == 0) {printf("fail\n"); continue;}
}
return 0;
}
a couple of problems for your code with a quick look:
compare function doesn't return value if length not equal.
test() function may get called twice in one process, which means fork more times.
the test() internally for the child will return to the main, also parent will return to main ... get things more complicated here (the child may fork a third time ...)
Use "strace -F" can give you a much better view what things happened behind.

Can FD_ISSET be called with a descriptor that was not added to the select set?

I'm debugging a select loop that normally works OK but dies with segmentation fault under heavy load. I've figured out that the program is sometimes invoking FD_ISSET() for a (correct) descriptor that was not added to the select set. Like in a following snippet:
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
void die(const char* msg)
{
fprintf(stderr, "fatal %s", msg);
exit(1);
}
int main(void)
{
FILE* file = fopen("/tmp/test", "r");
if (file == NULL)
die("fopen");
int file_fd = fileno(file);
fd_set read_fds;
int max_fd = 0;
FD_ZERO(&read_fds);
// Only stdin is added to read_fds.
FD_SET(0, &read_fds);
if (select(max_fd + 1, &read_fds, NULL, NULL, NULL) < 0)
die("select");
if (FD_ISSET(0, &read_fds))
printf("Can read from 0");
// !!! Here FD_ISSET is called with a valid descriptor that was
// not added to read_fds.
if (FD_ISSET(file_fd, &read_fds))
printf("Can read from file_fd");
return 0;
}
It is obvious that the check marked with !!! should never return true, but is it possible that it can be the cause of the SEGFAULT? When I run this snippet under valgrind, no errors are reported, but when I run my load test under valgrind I'm ocasionnaly seing errors like:
==25513== Syscall param select(writefds) points to uninitialised byte(s)
==25513== at 0x435DD2D: ___newselect_nocancel (syscall-template.S:82)
FD_ISSET() tests to see if a file descriptor is a part of the set read_fds. This means that FD_ISSET should not cause the segmentation fault.
Try checking for errno value set prior to calling the FD_ISSET. The select should be causing the segfault.
Also check that the file_fd value isn't greater than FD_MAX.

linux ptrace() get function information

i want to catch information from user defined function using ptrace() calls.
but function address is not stable(because ASLR).
how can i get another program's function information like gdb programmatically?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
#include <dlfcn.h>
#include <errno.h>
void error(char *msg)
{
perror(msg);
exit(-1);
}
int main(int argc, char **argv)
{
long ret = 0;
void *handle;
pid_t pid = 0;
struct user_regs_struct regs;
int *hackme_addr = 0;
pid = atoi(argv[1]);
ret = ptrace(PTRACE_ATTACH, pid, NULL, NULL);
if(ret<0)
{
error("ptrace() error");
}
ret = waitpid(pid, NULL, WUNTRACED);
if(ret<0)
{
error("waitpid ()");
}
ret = ptrace(PTRACE_GETREGS, pid, NULL, &regs);
if(ret<0)
{
error("GETREGS error");
}
printf("EIP : 0x%x\n", (int)regs.eip);
ptrace(PTRACE_DETACH, pid, NULL, NULL);
return 0;
}
ptrace is a bit ugly, but it can be useful.
Here's a ptrace example program; it's used to make I/O-related system calls pause.
http://stromberg.dnsalias.org/~strombrg/slowdown/
You could of course also study gdb, but ISTR it's pretty huge.
You might also check out strace and ltrace, perhaps especially ltrace since it lists symbols.
HTH
You probably want to call a function that resides in a specific executable (probably, a shared object). So, first, you will have to find the base address this executable is mapped on using
/proc/pid/maps
After that, you need to find the local offset of the function you are interested in, and you can do this in two ways:
Understand the ELF file format (Linux native executable format), and searching the desired function using the mapped file (This requires some specialty)
Using a ready to use elfparser (probably readelf tool) to get the function offset under the executable. Note that you will have to figure out the real local offset since this tool usually gives you the address as if the executable was mapped to a specific address

strange behaviour in visual c

this program hangs after taking first argument:-
#include <stdio.h>
#include <conio.h>
void ellip(char*,...);
int main(int argc,char* argv[]){
printf("a");
ellip("first argument",99,"second arg","thirdarg");
_getch();
return 0;
}
void ellip(char* m,...)
{ char com='c';
for(;;)
{
auto g=0;
while(com=='c')
{
printf("%d\nMatched Continue:-",g++);
scanf("%c",&com);
}
}
}
while the same program with a subtle modification(Addition of space)
scanf("%c ",&com);
Works Fine!
Is this some sort of bug in vc or a problem in my computer?
When the new line is read from stdin and placed into com, then com is now '\n' and the for(;;) loop will loop forever while the while(com=='c') will never be entered.
scanf("%c ",&com); fixes the problem because the space character will cause scanf to skip over all white space.
See the MSDN article. FIrst bullet point explains what the space charcter is doing.

Resources