-2

There is a main.c file, whose content is:

#include "memory.h"
#include "stdint.h"

#define MOLECULE_API_DECORATOR
MOLECULE_API_DECORATOR const uint8_t MolDefault_ByteOpt[0]      = {};

int main(int argc, char *argv[]) {
    uint32_t size = sizeof(MolDefault_ByteOpt);
    const uint8_t *expected = MolDefault_ByteOpt;
    const uint8_t *actual = NULL;
    if (memcmp(actual, expected, size) != 0){
        return 1;
    }
    return 0;
}

Then I compile main.c with gcc 12.2.1 by gcc -Werror -Wall main.c , will got:

main.c: In function ‘main’:
main.c:8:5: error: offset ‘0’ outside bounds of constant string [-Werror=array-bounds]
    8 | int main(int argc, char *argv[]) {
      |     ^~~~
main.c:6:38: note: ‘MolDefault_ByteOpt’ declared here
    6 | MOLECULE_API_DECORATOR const uint8_t MolDefault_ByteOpt[0]      = {};
      |                                      ^~~~~~~~~~~~~~~~~~
main.c:10:31: error: offset ‘0’ outside bounds of constant string [-Werror=array-bounds]
   10 |     const uint8_t *expected = MolDefault_ByteOpt;
      |                               ^~~~~~~~~~~~~~~~~~
main.c:6:38: note: ‘MolDefault_ByteOpt’ declared here
    6 | MOLECULE_API_DECORATOR const uint8_t MolDefault_ByteOpt[0]      = {};
      |                                      ^~~~~~~~~~~~~~~~~~
main.c:8:5: error: offset ‘0’ outside bounds of constant string [-Werror=array-bounds]
    8 | int main(int argc, char *argv[]) {
      |     ^~~~
main.c:6:38: note: ‘MolDefault_ByteOpt’ declared here
    6 | MOLECULE_API_DECORATOR const uint8_t MolDefault_ByteOpt[0]      = {};
      |                                      ^~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors

How can I change the code to avoid this error?

I expected to compare actual and expected and compile this code without any warnings or errors.

Eval EXEC
  • 1
  • 1

1 Answers1

0

C 2018 6.2.5 20 says (bold added):

  • An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type

and C 2018 6.7.6.2 1 says, if the expression inside [ and ] of an array declarator:

  • … If the expression is a constant expression, it shall have a value greater than zero…

Since the declarator MolDefault_ByteOpt[0] does not conform to these rules, the behavior is not defined by the C standard.

C 2018 7.1.4 1 says:

  • … If an argument to a function has an invalid value (such as a value outside the domain of the function, or a pointer outside the address space of the program, or a null pointer, or a pointer to non-modifiable storage when the corresponding parameter is not const-qualified) or a type (after default argument promotion) not expected by a function with a variable number of arguments, the behavior is undefined…

Since actual is a null pointer when memcmp(actual, expected, size) is evaluated, the argument has an invalid value, and the behavior is not defined by the C standard.

How can I change the code to avoid this error?

Make the array size positive and pass a valid pointer to memcmp. (You could pass the address of any const or unqualified object.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312