Skip to content

Instantly share code, notes, and snippets.

@ztane
Created March 20, 2019 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ztane/ed60a0cf8b4fba4a318cabacfae493b6 to your computer and use it in GitHub Desktop.
Save ztane/ed60a0cf8b4fba4a318cabacfae493b6 to your computer and use it in GitHub Desktop.
clang ub stuff
#include <stdio.h>
#define BUGGY
void funcA(int s, int g)
{
int i, j = 0, k;
int grp[4] = {-1, -1};
for (i = 0, k = 0; i < 1; ++i) {
for (j = 0; j < 1; ++j) {
if ( grp[g] > 0 )
{
if ( g > 5 ) {
continue;
}
else {
#ifdef BUGGY
*(long long *)grp = 0;
#else
*(int *)grp = 0;
*(((int *)grp) + 1) = 0;
#endif
}
}
grp[k++] = 0;
}
printf("this should be zero: %d\n", grp[0]);
}
}
int main(void) {
funcA(0, 1);
}
@ensc
Copy link

ensc commented Mar 20, 2019

void
funcA(int g)
{
	int grp[4];
	int k = 0;

	grp[0] = grp[1] = grp[2] = grp[3] = -1;

	printf( "  %s:%s:%d: grp[0]:%d\n", __FUNCTION__, __FILE__, __LINE__, grp[0]);

	for (int j = 0; j < 1; ++j ) {
		if ( grp[g] > 0 )
			*(long long *)grp = 0;

		if ( g < 100 )
			grp[k++] = 0;
	}

	printf( "  %s:%s:%d: grp[0]:%d\n", __FUNCTION__, __FILE__, __LINE__, grp[0]);

	return;
}

int
main( void)
{
	funcA(1);

	return (0);
}

seems to be more mimimal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment