• Welcome to Theos PowerBasic Museum 2017.

The compiler advantage

Started by John Spikowski, August 11, 2013, 07:20:11 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca

#120
Many C programmers use variadic functions just because that language allows to do it, but it is no the best way. With languages like PB, it is much easier (and safe) to use an array of variants.

The lack of safety of variadic functions is not something that I believe, but a fact:

Quote
A variable argument function (variadic function) is a function that can accept an undefined number of arguments. In many programming languages, formatted output functions are defined as variadic functions.

In C++, variable argument functions are declared with the ellipsis (...) in the argument list field. The compiler checks types only for explicitly defined arguments. To get access to the variable argument list macros va_arg, va_end and va_start from the STDARG.H header file are used. The printf function family is one of the well-known examples of variable argument functions.

In some languages using variable argument functions might cause type safety troubles. For example, C/C++ functions of the printf family may create uncontrolled string format vulnerabilities when used carelessly. Mismatch of the format string and passed arguments may cause unpredictable behavior, buffer overflows, stack smashing and execution of unsafe code and lead to destruction of dynamic memory areas.

Although these troubles related to the use of variadic functions are known for a long time, they were identified as serious security vulnerabilities only in 1999. Considering that such vulnerabilities were thought to be rather harmless for many years, this is one of the most widespread classes of errors today.

Charles Pegge


There are quite a few unsafe constructs in PB, which prove to be very useful, especially for low-level coding, so I think ellipsis is a useful construct to have in the toolbox, if only to access libraries which deploy variadic functions.

Patrice Terrier

What about using a pointer to a structure, only one 32/64-bit LONG_PTR parameter, but plainty of options to customize it the way you want :)
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Charles Pegge

#123
This is the obvious alternative, using some form of variant structure. In fact ScriptBasic has such a function called scriba_SbCallArgEx which takes arguments byref as an array of sbData, which contains type, length and value. This takes more coding to setup a call  but is slightly more efficient than interpreting a format-string, and an indeterminate bunch of args.