Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Potentially undefined behavior): strncmp with N greater than zero for an array of size zero. #136

Open
safocl opened this issue Jan 28, 2025 · 2 comments

Comments

@safocl
Copy link

safocl commented Jan 28, 2025

!strncmp((const char*)"SETFILEBITS",

strncmp((const char*)"SETFILEBITS",
				(const char*)ea->name, 11)

but

u8 name[0]; /* Name of the EA. */

typedef struct {
	le32 next_entry_offset;	/* Offset to the next EA_ATTR. */
	EA_FLAGS flags;		/* Flags describing the EA. */
	u8 name_length;		/* Length of the name of the extended
				   attribute in bytes. */
	le16 value_length;	/* Byte size of the EA's value. */
	u8 name[0];		/* Name of the EA. */
	u8 value[0];		/* The value of the EA. Immediately
				   follows the name. */
} __attribute__((__packed__)) EA_ATTR;

and

The behavior is undefined when access occurs past the end of either array lhs or rhs. The behavior is undefined when either lhs or rhs is the null pointer.

@safocl
Copy link
Author

safocl commented Jan 28, 2025

is this expression ever true?

if (ea->name_length == 11 &&

ea->name_length == 11

@safocl
Copy link
Author

safocl commented Jan 28, 2025

https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html

Declaring zero-length arrays in other contexts, including as interior members of structure objects or as non-member objects, is discouraged. Accessing elements of zero-length arrays declared in such contexts is undefined and may be diagnosed.

A structure containing a C99 flexible array member, or a union containing such a structure, is not the last field of another structure, for example:

struct flex { int length; char data[]; };

struct mid_flex { int m; struct flex flex_data; int n; };

In the above, accessing a member of the array mid_flex.flex_data.data[] might have undefined behavior. Compilers do not handle such a case consistently. Any code relying on this case should be modified to ensure that flexible array members only end up at the ends of structures.

Please use the warning option -Wflex-array-member-not-at-end to identify all such cases in the source code and modify them. This extension is now deprecated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant