473,976 Members | 2,696 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

allowing a function to be called only from a specific function

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 ...

Nov 29 '06
24 1368
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

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
Dec 1 '06 #21
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.

Dec 1 '06 #22
Harald van D?k wrote:
>
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");
}

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

Dec 1 '06 #23
ju**********@ya hoo.co.in wrote:
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_funct ion (__func__, x, y)

------------protected.c---------------
void protected_funct ion (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,

Dec 1 '06 #24
CBFalconer wrote:
Harald van D?k wrote:

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.

Dec 1 '06 #25

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2120
by: Dave | last post by:
Hello NG, If a particular dynamic library gets loaded, I need to ensure that a function in that library gets executed on startup (and I want the mechanism to use only Standard C++). Here's what I've got: void func() { // This is the function that must be executed }
11
2311
by: Enquiries, Hopkins Research | last post by:
Hi all I have a conundrum that is puzzling me. I have a large codebase in C that I am converting to C++ as fast as possible (i.e. slowly because I keep learning new idioms and stumbling with C++ 'features'). One part of the C code is some optimisation functions that expect a pointer to a function which is sent an array of doubles and returns a double i.e. simplified..
4
3652
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
7
2931
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the database into classes, which are used throughout the application. I have made class collections which, upon reading from the DB, create an instance of the class and store the DB values in there temporarily. My problem is that if user1 looks at...
14
1862
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ????? OK, this function may return a boolean, and if this is true, then no message back is really required, but if it fails then some supporting message needs to be returned to the calling code. As I see it there are a few options.
3
3673
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules' that takes arguments of type (void *) because the ADT must be able to deal with any type of data. In my actual code, I will code the function to take arguments of their real types, then when I pass this pointer through an interface function, I...
89
6130
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be used." Could anybody tell me why gets() function is dangerous?? Thank you very much. Cuthbert
28
4374
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
6
5950
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with or without parms. I know that any function that is passed to Eval() must be declared Public. It can be a Sub or Function, as long as it's Public. I even have it where the "function" evaluated by Eval can be in a form (class) module or in a standard...
0
10350
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10167
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11406
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11570
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10905
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
10081
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.