Search code examples
javac++csyntaxsemantics

Error that is neither syntactic nor semantic?


I had this question on a homework assignment (don't worry, already done):

[Using your favorite imperative language, give an example of each of ...] An error that the compiler can neither catch nor easily generate code to catch (this should be a violation of the language definition, not just a program bug)

From "Programming Language Pragmatics" (3rd ed) Michael L. Scott

My answer, call main from main by passing in the same arguments (in C and Java), inspired by this. But I personally felt like that would just be a semantic error.

To me this question's asking how to producing an error that is neither syntactic nor semantic, and frankly, I can't really think of situation where it wouldn't fall in either.

Would it be code that is susceptible to exploitation, like buffer overflows (and maybe other exploitation I've never heard about)? Some sort of pit fall from the structure of the language (IDK, but lazy evaluation/weak type checking)? I'd like a simple example in Java/C++/C, but other examples are welcome.


Solution

  • Undefined behaviour springs to mind. A statement invoking UB is neither syntactically nor semantically incorrect, but rather the result of the code cannot be predicted and is considered erroneous.

    An example of this would be (from the Wikipedia page) an attempt to modify a string-constant:

    char * str = "Hello world!";
    str[0] = 'h'; // undefined-behaviour here
    

    Not all UB-statements are so easily identified though. Consider for example the possibility of signed-integer overflow in this case, if the user enters a number that is too big:

    // get number from user
    char input[100];
    fgets(input, sizeof input, stdin);
    int number = strtol(input, NULL, 10);
    // print its square: possible integer-overflow if number * number > INT_MAX
    printf("%i^2 = %i\n", number, number * number);
    

    Here there may not necessarily be signed-integer overflow. And it is impossible to detect it at compile- or link-time since it involves user-input.