Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does this line of code "#define LIBINJECTION_SQLI_TOKEN_SIZE sizeof(((stoken_t*)(0))->val)" do?

In particular I'd like to know what ->val does in the

sizeof(((stoken_t*)(0))->val)

and what stoken_t*(0) pointer do, in particular what the (0) means?

I hope I have formulated my question clearly enough.

like image 908
Houst Avatar asked Sep 07 '20 08:09

Houst


People also ask

What is the meaning of this line of code?

Line of code is a unit used in measuring or estimating the scale of programming or code conversion efforts.

What is LOC and Kloc?

In size-oriented metrics, LOC is considered to be the normalization value. It is an older method that was developed when FORTRAN and COBOL programming were very popular. Productivity is defined as KLOC / EFFORT, where effort is measured in person-months. Size-oriented metrics depend on the programming language used.

How many lines of code is 11?

By comparison, the Microsoft Windows operating system has roughly 50 million lines of code.


2 Answers

This is a way of accessing a member of a structure at compile time, without needing to have a variable defined of that structure type.

The cast (stoken_t*) to a value of 0 emulates a pointer of that structure type, allowing you to make use of the -> operator on that, just like you would use it on a pointer variable of that type.

To add, as sizeof is a compile time operator, the expression is not evaluated at run-time, so unlike other cases, here there is no null-pointer dereference happening.

It is analogous to something like

stoken_t * ptr;
sizeof(ptr->val);
like image 112
Sourav Ghosh Avatar answered Oct 17 '22 11:10

Sourav Ghosh


In detail:

(stoken_t*)(0) simply casts 0 (this could be an arbitrary numeric literal) to a pointer to stoken_t, ((stoken_t*)(0)->val) is then the type of the val member of stoken_t and sizeof returns the number of bytes this type occupies in memory. In short, this expression finds the size of a struct member at compile time without the need for an instance of that struct type.

like image 23
Peter Avatar answered Oct 17 '22 10:10

Peter