1

I was studying some C code from a college book I have (the subject of the book is Linux programming), and I came across some lines of code I don't understand. I have done some research but I haven't find anything on Internet. Can someone explain this code to me?

I know what environment variables are, the part I don't understand is how this int (*det)(), det() function works. Sorry if my title is not correct, I just didn't know how to describe the subject better.

#include<stdio.h>

int main(){
    int (*det)();
    det = getenv("foo");      
    det();
    return 0;
}

*** Thank you for your replies.is there any way to make this program functional, for example print "hallo"?

John Vn
  • 81
  • 2
  • 10
  • 1
    Best troll I've read here, this week. – Sam Varshavchik Apr 16 '16 at 14:18
  • `stidio.h`, seriously? – nalzok Apr 16 '16 at 14:21
  • The keyword to search for is "Function Pointers" – Spikatrix Apr 16 '16 at 14:26
  • 1
    `int (*det)()` is a pointer to a function that takes no parameters and returns a int. It is then assigned the result of `getenv("foo")`, presumably a memory address where such a function resides and then called with `det();`. Note that getting and calling a memory address from a enviroment string is unsafe and will probably fail if the memory does not belong to you (if it did, why get it trough such dubious means?). – Unimportant Apr 16 '16 at 14:28
  • 2
    @user1320881 Actually it's a (pointer to a) function that accepts any number of parameters. – Daniel Jour Apr 16 '16 at 14:30
  • @Daniel Jour: True, thanks, was thinking of C++. – Unimportant Apr 16 '16 at 14:32
  • @user1320881 That's a reasonable answer, but comments are not for that. – edmz Apr 16 '16 at 14:42
  • @RaymondChen: good find, but I think the point is here that it declares a regular local variable, right? – Jongware Apr 16 '16 at 14:49
  • @RadLexus And indeed that's what the answers to the possible duplicate say: It's a variable declaration. (SO search ignores punctuation, which makes searching for dups of questions about operators and punctuation really hard.) – Raymond Chen Apr 16 '16 at 14:51

2 Answers2

5

In general, it does not work. You are trying to assign a char* (string) to a function pointer. A reasonable compiler will reject this.

However, not all compilers are reasonable. If you do manage to compile and execute this code, you have created a program vulnerable to attack via the environment variable foo. This is because if you do manage to execute the contents of foo, and they are indeed executable on your machine, you will have shifted control of the process out of your C program and into whatever code exists in foo. This is nonportable, probably nonworking, definitely unsafe, and should never be attempted.

Also, the book you got this from is, at a guess, something about information security. If not, burn it.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

As somebody already pointed out

det = getenv("foo"); 

doesn't make much sense and i hope your compiler is smart enough so that you don't get away with it.Below might be what you were expecting to see in that book :

#include <stdio.h>
#include <stdlib.h>

extern char* getenv(const char*);
/* The above statement is not mandatory but would make the code more 
 * readable. The keyword "extern" here implies that the function is 
 * is defined elsewhere.
 */

int main(void)
{
    char* (*det)(const char*) = getenv;
    printf("PATH env variable contains : %s\n", (*det)("PATH"));
    /* Replace path with your own environment variable */
    printf("Press any key to continue..\n");
    getchar();
    return 0;
} 

I am just printing some stuff here, change the usage to satisfy your needs.

References

getenv() prototype here.

sjsam
  • 21,411
  • 5
  • 55
  • 102