Skip to content

Conversation

@fosslinux
Copy link
Contributor

@fosslinux fosslinux commented Aug 20, 2024

This trivial program:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *thread(void *data) {
	char *s = malloc(8 * sizeof(char));
	s[10] = 0;
	free(s);
	return 0;
}

int main(void) {
	pthread_t thread_data;
	pthread_create(&thread_data, NULL, thread, NULL);
	pthread_join(thread_data, NULL);
}

Previously outputted

=================================================================

Runtime error: malloc buffer overflow
dcc explanation: access past the end of malloc'ed memory.
  Make sure you have allocated enough memory for the size of your struct/array.
  A common error is to use the size of a pointer instead of the size of the struct or array.

  For more information see: https://comp1511unsw.github.io/dcc/malloc_sizeof.html

Execution stopped in main() in a.c at line 15:

int main(void) {
	pthread_t thread_data;
	pthread_create(&thread_data, NULL, thread, NULL);
-->	pthread_join(thread_data, NULL);
}

Values when execution stopped:

thread_data = 127271036778176

Which is clearly incorrect, as the error occurs within the thread.
This set of changes makes DCC correctly trace the error back to the originating thread, for ASAN and signal errors.

Now it outputs:

=================================================================

Runtime error: malloc buffer overflow
dcc explanation: access past the end of malloc'ed memory.
  Make sure you have allocated enough memory for the size of your struct/array.
  A common error is to use the size of a pointer instead of the size of the struct or array.

  For more information see: https://comp1511unsw.github.io/dcc/malloc_sizeof.html

Execution stopped in thread(data=NULL) in a.c at line 7:

void *thread(void *data) {
	char *s = malloc(8 * sizeof(char));
-->	s[10] = 0;
	free(s);
	return 0;
}

Values when execution stopped:

data = NULL
s = "<8 uninitialized values>"
s[10] = 0 = '\0'

Motivation: I found this very very confusing the first few times I ran across this bug in COMP1521 this term, so I fixed it 😄

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

Successfully merging this pull request may close these issues.

1 participant