1

Recently I've got a strange bug/feature. Developing C function using C Matrix API in MATLAB I have done a mistake by passing more arguments than was declared by function, and the compiler didn't raise an error about it.

A small example of a problem using Matlab C Matrix API - mex_with_bug.c:

#include "mex.h"

int function_which_declares_one_input(int a) {
   return a; 
}

void mexFunction(int hlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
    
    // expected usage
    function_which_declares_one_input(1);

    // totally not expected use cases
    function_which_declares_one_input(1, 2);
    function_which_declares_one_input(1, 2, 3);
    function_which_declares_one_input(1, 2, 3, 'a');

}

Compilation command in Matlab:

mex -v COMPFLAGS="$COMPFLAGS -Wall" mex_with_bug.c

Only gives a warning and no errors:

...
mex_with_bug.c(13): warning C4020: "function_which_declares_one_input": too many real parameters
mex_with_bug.c(14): warning C4020: "function_which_declares_one_input": too many real parameters
mex_with_bug.c(15): warning C4020: "function_which_declares_one_input": too many real parameters
...

And calling function mex_with_bug works just fine:

>> mex_with_bug()
>>

Why no error was raised? And why this 'feature' is needed by C?

Oleg Kmechak
  • 165
  • 1
  • 14
  • 1
    Definitely a 'mex' thing, because that code will puke all over itself with standard-compliant C. – WhozCraig Jun 09 '21 at 17:19
  • 3
    @WhozCraig that is strange, because "mex" uses gcc, MVS or minGW to compile code, i.e. standard-compliant C. Seems that it appears in other places: https://stackoverflow.com/questions/26570012/visual-c-accepting-wrong-number-of-arguments – Ander Biguri Jun 09 '21 at 17:29
  • 1
    What is *"some_function"*? Do you get the warning message with exactly the code shown in the question? Line 9 of the source file seems to be a comment. If you re-typed or modified the code or error message, please copy&paste matching code and error message to the question. If it is not clear, state which line the error message refers to. The single quotes in `COMPFLAGS='$COMPFLAGS -Wall'` might be wrong unless `mex` emulates the UNIX environment variable syntax. On what OS are you running this? Please [edit] your question, don't use comments to provide requested information. – Bodo Jun 09 '21 at 17:37
  • @Bodo thx fo comments, fixed! – Oleg Kmechak Jun 14 '21 at 10:25

0 Answers0