allowing a function to be called only from a specific function

C

CBFalconer

Harald said:
CBFalconer wrote:
.... snip ...

No, it cannot. f3 is defined before f1 is declared. You can refer
to a static function or variable with an extern declaration if and
only if the a static declaration is already in scope, and you
cannot use static on a function declaration with block scope.

Counter example. Nothing says you have to heed warnings. Proper
isolation requires a separate source file.

[1] c:\c\junk>cat junk.c
#include <stdio.h>

int main(void) {
void f1(void);

f1();
return 0;
} /* main */

static void f1(void) {
puts("In f1");
}

[1] c:\c\junk>cc junk.c
junk.c:10: warning: `f1' was declared `extern' and later `static'

[1] c:\c\junk>.\a
In f1
 
G

Guest

CBFalconer schreef:
Harald said:
CBFalconer wrote:
... snip ...

No, it cannot. f3 is defined before f1 is declared. You can refer
to a static function or variable with an extern declaration if and
only if the a static declaration is already in scope, and you
cannot use static on a function declaration with block scope.

Counter example. Nothing says you have to heed warnings. Proper
isolation requires a separate source file.

[1] c:\c\junk>cat junk.c
#include <stdio.h>

int main(void) {
void f1(void);

f1();
return 0;
} /* main */

static void f1(void) {
puts("In f1");
}

[1] c:\c\junk>cc junk.c
junk.c:10: warning: `f1' was declared `extern' and later `static'

[1] c:\c\junk>.\a
In f1

That's nice. As far as standard C is concerned, the behaviour is
undefined per 6.2.2p7, and my compiler rejects it. If you count
implementation-specific extensions, then inline assembly may also allow
you to access static functions defined in another source file.
 
C

CBFalconer

Harald said:
CBFalconer schreef:
Harald said:
CBFalconer wrote:
... snip ...

Yes it can. The 'static' only prevents visibility outside the
compilation unit.

No, it cannot. f3 is defined before f1 is declared. You can refer
to a static function or variable with an extern declaration if and
only if the a static declaration is already in scope, and you
cannot use static on a function declaration with block scope.

Counter example. Nothing says you have to heed warnings. Proper
isolation requires a separate source file.

[1] c:\c\junk>cat junk.c
#include <stdio.h>

int main(void) {
void f1(void);

f1();
return 0;
} /* main */

static void f1(void) {
puts("In f1");
}

[1] c:\c\junk>cc junk.c
junk.c:10: warning: `f1' was declared `extern' and later `static'

[1] c:\c\junk>.\a
In f1

That's nice. As far as standard C is concerned, the behaviour is
undefined per 6.2.2p7, and my compiler rejects it. If you count
implementation-specific extensions, then inline assembly may also allow
you to access static functions defined in another source file.

Then try this:

#include <stdio.h>
static void f1(void);

int main(void) {

f1();
return 0;
} /* main */

static void f1(void) {
puts("In f1");
}
 
G

goose

Hi,

Is there any way, by which we can limit a specific function to be
called
only from a specific function ? I dont know the advantage of this.
Someone
asked this question from me in an interview.

thanks for any help ...

Realistically? Not properly in stdc. The following should be
'good enough' for most practical porpoises.

------------protected.h---------------
#define function(x,y) protected_function (__func__, x, y)



------------protected.c---------------
void protected_function (char *caller, int x, int y)
{
char *legal_callers[] = {
"foo",
"bar",
"baz",
};
int allowed = 0;
size_t i;

for (i=0; i<sizeof legal_callers/sizeof legal_callers[0]; i++) {
if (strcmp (caller, legal_callers)==0) {
allowed = 1;
break;
}
}

if (!allowed) {
return;
}

/* Rest of function goes here */
/* Do something with x and y */
}

<OT>
If it is proper security you are looking for, you are out of
luck; there isn't a single programming language that I know
of that has builtin authentication (or identification) for
functions (and objects) and authorisation (or access controls)
for functions (and objects).

There are some experiments with *code-signing* on newer platforms,
but these apply to the entire platform (.Net or, IIRC, Java?) and
probably do not go down to the granularity you want.

The closest you will get is using gcc[1] and the extensions it
provides (builtin_apply_args, builtin_apply and builtin_return[2])
wrapped in a function that does authentication and/or authorisation.

[1] Or any other compiler with the appropriate extensions.
[2] Or the extensions provided that allow this type of thing.

</OT>

The other option is to have only two functions in a
source file. One is the actual function to call and is
static and the other function is merely a wrapper to
authenticate/authorise the credentials supplied and
invoke the static function (or return a pointer to it).


goose,
 
G

Guest

CBFalconer said:
Harald said:
CBFalconer schreef:
Harald van D?k wrote:
CBFalconer wrote:

... snip ...

Yes it can. The 'static' only prevents visibility outside the
compilation unit.

No, it cannot. f3 is defined before f1 is declared. You can refer
to a static function or variable with an extern declaration if and
only if the a static declaration is already in scope, and you
cannot use static on a function declaration with block scope.

Counter example. Nothing says you have to heed warnings. Proper
isolation requires a separate source file.

[1] c:\c\junk>cat junk.c
#include <stdio.h>

int main(void) {
void f1(void);

f1();
return 0;
} /* main */

static void f1(void) {
puts("In f1");
}

[1] c:\c\junk>cc junk.c
junk.c:10: warning: `f1' was declared `extern' and later `static'

[1] c:\c\junk>.\a
In f1

That's nice. As far as standard C is concerned, the behaviour is
undefined per 6.2.2p7, and my compiler rejects it. If you count
implementation-specific extensions, then inline assembly may also allow
you to access static functions defined in another source file.

Then try this:

#include <stdio.h>
static void f1(void);

int main(void) {

f1();
return 0;
} /* main */

static void f1(void) {
puts("In f1");
}

That's of course allowed, but Chris Dollin's original post of this
subthread already stated that there should be no such forward
declaration. The idea is that main() cannot call f1() without outside
help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top