473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Insuring a function is only called from a macro

Hi all,
I have a function

void actualfunction(unsigned int t, unsigned int lineno)
{
....
}

I want that it is called only from a macro I wrote for it (I don't
want to use a wrapper function) like,

#define FUNCTION(a) actualfunction((a), __LINE__)

How to do I insure that actualfunction is only called as FUNCTION?
One thing I could do is, call another function that sets a global
variable and then test that variable in 'actualfunction'.

#define FUNCTION(a) setTest(); \
actualfunction((a), __LINE__); \
void setTest(void)
{
static unsigned char test;
test = 0xFF;
}

void actualfunction(unsigned int t, unsigned int lineno)
{
assert(test == 0xFF);
test = 0x00;

....
}
BTW I'm ok to ignore the case where some calls setTest and then
actualfunction directly but this is a run time check, is a compile
time check posssible?


Would appreciate any pointers. Thanks!
Aug 19 '08 #1
9 1482
In article <07**********************************@n33g2000pri. googlegroups.com>,
holysmoke <su**********@gmail.comwrote:
>I want that it is called only from a macro I wrote for it (I don't
want to use a wrapper function) like,
You can't. Anything the macro expands to, the user could just
write themselves.

The simplest solution is to give the function an obscure name that
makes it clear it shouldn't be used, like xxx_function_internal, or
function_calling_me_directly_invalidates_warranty. And document
the fact that it shouldn't be used.

-- Richard
--
Please remember to mention me / in tapes you leave behind.
Aug 19 '08 #2
"Richard Tobin" <ri*****@cogsci.ed.ac.ukwrote in message
news:g8***********@pc-news.cogsci.ed.ac.uk...
In article
<07**********************************@n33g2000pri. googlegroups.com>,
holysmoke <su**********@gmail.comwrote:
>>I want that it is called only from a macro I wrote for it (I don't
want to use a wrapper function) like,

You can't. Anything the macro expands to, the user could just
write themselves.

The simplest solution is to give the function an obscure name that
makes it clear it shouldn't be used, like xxx_function_internal, or


function_calling_me_directly_invalidates_warranty.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

=^D

And document
the fact that it shouldn't be used.
Aug 19 '08 #3
holysmoke <su**********@gmail.comwrites:
I have a function

void actualfunction(unsigned int t, unsigned int lineno)
{
...
}

I want that it is called only from a macro I wrote for it (I don't
want to use a wrapper function) like,

#define FUNCTION(a) actualfunction((a), __LINE__)

How to do I insure that actualfunction is only called as FUNCTION?
[...]

Any standard library function may be additionally implemented as a
macro with the same name. You could use the same trick:

void actualfunction(unsigned int t, unsigned int lineno) { ... }

#define actualfunction(t) ((actualfunction)((t), __LINE__))

Any ordinary function call to actualfunction will invoke your
macro.

As Richard Tobin pointed out, anything you can do in your macro, a
user can also do. In particular, surrounding the function name with
parentheses or taking the function's address will bypass your macro.

But I presume you're not looking for absolute security in the presence
of hostile users, just a convenient way to make it easy for them to
invoke your macro.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 19 '08 #4
On 19 Aug, 11:31, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <07da609e-11bf-4901-8741-70a54d147...@n33g2000pri.googlegroups.com>,

holysmoke <sukrit.me...@gmail.comwrote:
I want that it is called only from a macro I wrote for it (I don't
want to use a wrapper function) like,

You can't. Anything the macro expands to, the user could just
write themselves.
Is there a way to use the preprocessor to generate a dynamic name for
the function - based on time of day or something random, even the line
number, as long as it's not known in the calling code - and then call
that from within the macro? Assemblers sometimes permit this. I don't
know about the C preprocessor.
Aug 20 '08 #5
James Harris wrote:
On 19 Aug, 11:31, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
>In article <07da609e-11bf-4901-8741-70a54d147...@n33g2000pri.googlegroups.com>,

holysmoke <sukrit.me...@gmail.comwrote:
>>I want that it is called only from a macro I wrote for it (I don't
want to use a wrapper function) like,
You can't. Anything the macro expands to, the user could just
write themselves.

Is there a way to use the preprocessor to generate a dynamic name for
the function - based on time of day or something random, even the line
number, as long as it's not known in the calling code - and then call
that from within the macro? Assemblers sometimes permit this. I don't
know about the C preprocessor.
To do such a thing, you'd need to be able to generate
the same function name at the point of definition and at each
call. Since you might compile the function definition today
and the calls on the second Tuesday of each of the next six
years, I don't see how the name generation could be entirely
impenetrable: All the information needed to come up with the
name is necessarily available at the point of the call.

Macro expansion produces pp-tokens, and once produced there
is nothing that distinguishes pp-tokens generated by macros from
pp-tokens generated directly from the source text.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Aug 20 '08 #6
James Harris <ja************@googlemail.comwrites:
On 19 Aug, 11:31, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
>In article
<07da609e-11bf-4901-8741-70a54d147...@n33g2000pri.googlegroups.com>,

holysmoke <sukrit.me...@gmail.comwrote:
>I want that it is called only from a macro I wrote for it (I don't
want to use a wrapper function) like,

You can't. Anything the macro expands to, the user could just
write themselves.

Is there a way to use the preprocessor to generate a dynamic name for
the function - based on time of day or something random, even the line
number, as long as it's not known in the calling code - and then call
that from within the macro? Assemblers sometimes permit this. I don't
know about the C preprocessor.
I can't think of a way to do this from the C preprocessor, but you
could always pre-filter the source code. Something like this:

void func_$(unsigned int t, unsigned int lineno)
{ ... }

#define func(t) (func_$((t), __LINE__))

where your own pre-filter converts each occurrence of $ to some random
number (the same one for each occurrence in the same file).

But really, what does this buy you? For the macro to be useful, you
need to provide the file that defines it to anyone who's going to use
it. Your users can always just take a look at it and see that
func(42)
expands to
(func_845285045997((42), __LINE))

Anything that you can do in a macro, your users will be able to do
themselves.

Assume for the sake of argument that a solution exists, something that
absolutely prevents users from calling your function other than via
the macro. What real advantage would this have over just (a) making
it convenient for users to use the macro, and (b) telling them they
shouldn't call the function directly? Something like this:

void do_not_call_directly(unsigned int t, unsigned int lineno)
{ ... }

#define func(t) (do_not_call_directly((t), __LINE__))

If you really need to *prevent* hostile users from calling your
function directly, that's a much harder problem, and one that I
suspect can't be solved within standard C. If you can just tell them
"If you call this function directly, all bets are off; I reserve the
right to change the name at the next full moon", that's a lot easier.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 20 '08 #7
In article
<17**********************************@26g2000hsk.g ooglegroups.com>,
James Harris <ja************@googlemail.comwrote:
Is there a way to use the preprocessor to generate a dynamic name for
the function - based on time of day or something random, even the line
number, as long as it's not known in the calling code - and then call
that from within the macro? Assemblers sometimes permit this. I don't
know about the C preprocessor.
Doesn't C++ mangle function names sometimes? Maybe you could do
something similar, although I'm not sure how.
** Posted from http://www.teranews.com **
Aug 20 '08 #8
In article <17**********************************@26g2000hsk.g ooglegroups.com>,
James Harris <ja************@googlemail.comwrote:
>Is there a way to use the preprocessor to generate a dynamic name for
the function - based on time of day or something random, even the line
number, as long as it's not known in the calling code - and then call
that from within the macro?
Even if you could - which you can't - then what's to stop the user
from do the same thing?

Why are you (or the OP) trying to do this? If you want to protect
against mistakes, then the solutions already proposed should be
adequate. If you want to protect against malicious programmers, the
whole thing is futile.

-- Richard
--
Please remember to mention me / in tapes you leave behind.
Aug 20 '08 #9
On 20 Aug, 08:58, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <1742b74a-b106-4b26-ad0b-9472e80ca...@26g2000hsk.googlegroups.com>,
James Harris <james.harri...@googlemail.comwrote:
Is there a way to use the preprocessor to generate a dynamic name for
the function - based on time of day or something random, even the line
number, as long as it's not known in the calling code - and then call
that from within the macro?

Even if you could - which you can't - then what's to stop the user
from do the same thing?

Why are you (or the OP) trying to do this? If you want to protect
against mistakes, then the solutions already proposed should be
adequate. If you want to protect against malicious programmers, the
whole thing is futile.
No reason. Just my devious mind wondering if there's an irregular way
to do what the OP wanted. Assemblers sometimes have a facility for
defining unique labels, something along the lines of this pseudo-
assembler

.define uniq @<next_id ;Set up a varying number 0 upwards

func'uniq': ;Label for function proper
... (function code)
ret

.macro KNOWN_FUNCTION_NAME ;Define the name known outside this
file
call func'uniq' ;Call the function proper
.endm ;End of macro
.undef uniq ;Remove this definition

Where @<next_idgenerates a different integer each time it appears.
Then the user code would call KNOWN_FUNCTION_NAME and could not know
in advance except by guessing what the real function name would be.
The real function name would be known only within the separate file
where it is defined.

Sounds like this is not possible with the C preprocessor, though.
Shame.....;-)
Aug 20 '08 #10

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

Similar topics

13
by: Shi Jin | last post by:
Hi there, While I was trying to disassemble a simple C program for my OS course, I found that the call to function longjmp is actually called to siglongjmp under linux. And similarly, under...
33
by: Pushkar Pradhan | last post by:
I'm using clock() to time parts of my code e.g. clk1 = clock(); /* code */ clk2 = clock(); /* calculate time in secs */ ...... clk1 = clock(); /* code */ clk2 = clock();
1
by: Capstar | last post by:
Hi NG, I have the following struct, macro and functions prototypes: typedef struct { jmp_buf __env; /*other stuff*/ } SAT; #define smalloc_init(sat_p, size) \
8
by: David | last post by:
Hi, I am using header file for driver so Can I define macro to call Kernel function ? What are the generatl rules for defining macro. - David
6
by: Johs32 | last post by:
I get this warning: warning: incompatible implicit declaration of built-in function 'printf' because I use printf in a function that I include in a .h file that is included by other files...Do...
3
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'...
24
by: junky_fellow | last post by:
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...
45
by: madhawi | last post by:
Is it better to use a macro or a function?
13
by: mohi | last post by:
hello everyone, i use a function called minor to find the minor of an element of a matrix , i declare it as : float minor(float A,int m,int n,int i,int j);//find the minor in A and define...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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...
0
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...
0
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...
0
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,...

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.