2017-03-09 08:01:01 +08:00
|
|
|
// RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc -analyzer-store=region -verify %s
|
|
|
|
|
|
|
|
#include "Inputs/system-header-simulator.h"
|
|
|
|
|
|
|
|
typedef void* gpointer;
|
|
|
|
typedef const void* gconstpointer;
|
|
|
|
typedef unsigned long gsize;
|
|
|
|
typedef unsigned int guint;
|
|
|
|
|
|
|
|
gpointer g_malloc(gsize n_bytes);
|
|
|
|
gpointer g_malloc0(gsize n_bytes);
|
|
|
|
gpointer g_realloc(gpointer mem, gsize n_bytes);
|
|
|
|
gpointer g_try_malloc(gsize n_bytes);
|
|
|
|
gpointer g_try_malloc0(gsize n_bytes);
|
|
|
|
gpointer g_try_realloc(gpointer mem, gsize n_bytes);
|
2017-04-26 13:33:14 +08:00
|
|
|
gpointer g_malloc_n(gsize n_blocks, gsize n_block_bytes);
|
|
|
|
gpointer g_malloc0_n(gsize n_blocks, gsize n_block_bytes);
|
|
|
|
gpointer g_realloc_n(gpointer mem, gsize n_blocks, gsize n_block_bytes);
|
|
|
|
gpointer g_try_malloc_n(gsize n_blocks, gsize n_block_bytes);
|
|
|
|
gpointer g_try_malloc0_n(gsize n_blocks, gsize n_block_bytes);
|
|
|
|
gpointer g_try_realloc_n(gpointer mem, gsize n_blocks, gsize n_block_bytes);
|
2017-03-09 08:01:01 +08:00
|
|
|
void g_free(gpointer mem);
|
|
|
|
gpointer g_memdup(gconstpointer mem, guint byte_size);
|
|
|
|
|
|
|
|
static const gsize n_bytes = 1024;
|
|
|
|
|
2022-02-13 21:02:46 +08:00
|
|
|
void f1(void) {
|
2017-03-09 08:01:01 +08:00
|
|
|
gpointer g1 = g_malloc(n_bytes);
|
|
|
|
gpointer g2 = g_malloc0(n_bytes);
|
|
|
|
g1 = g_realloc(g1, n_bytes * 2);
|
|
|
|
gpointer g3 = g_try_malloc(n_bytes);
|
|
|
|
gpointer g4 = g_try_malloc0(n_bytes);
|
|
|
|
g3 = g_try_realloc(g3, n_bytes * 2);
|
2017-04-26 13:33:14 +08:00
|
|
|
gpointer g5 = g_malloc_n(n_bytes, sizeof(char));
|
|
|
|
gpointer g6 = g_malloc0_n(n_bytes, sizeof(char));
|
|
|
|
g5 = g_realloc_n(g5, n_bytes * 2, sizeof(char));
|
|
|
|
gpointer g7 = g_try_malloc_n(n_bytes, sizeof(char));
|
|
|
|
gpointer g8 = g_try_malloc0_n(n_bytes, sizeof(char));
|
|
|
|
g7 = g_try_realloc_n(g7, n_bytes * 2, sizeof(char));
|
2017-03-09 08:01:01 +08:00
|
|
|
|
|
|
|
g_free(g1);
|
|
|
|
g_free(g2);
|
|
|
|
g_free(g2); // expected-warning{{Attempt to free released memory}}
|
|
|
|
}
|
|
|
|
|
2022-02-13 21:02:46 +08:00
|
|
|
void f2(void) {
|
2017-03-09 08:01:01 +08:00
|
|
|
gpointer g1 = g_malloc(n_bytes);
|
|
|
|
gpointer g2 = g_malloc0(n_bytes);
|
|
|
|
g1 = g_realloc(g1, n_bytes * 2);
|
|
|
|
gpointer g3 = g_try_malloc(n_bytes);
|
|
|
|
gpointer g4 = g_try_malloc0(n_bytes);
|
|
|
|
g3 = g_try_realloc(g3, n_bytes * 2);
|
2017-04-26 13:33:14 +08:00
|
|
|
gpointer g5 = g_malloc_n(n_bytes, sizeof(char));
|
|
|
|
gpointer g6 = g_malloc0_n(n_bytes, sizeof(char));
|
|
|
|
g5 = g_realloc_n(g5, n_bytes * 2, sizeof(char));
|
|
|
|
gpointer g7 = g_try_malloc_n(n_bytes, sizeof(char));
|
|
|
|
gpointer g8 = g_try_malloc0_n(n_bytes, sizeof(char));
|
|
|
|
g7 = g_try_realloc_n(g7, n_bytes * 2, sizeof(char));
|
2017-03-09 08:01:01 +08:00
|
|
|
|
|
|
|
g_free(g1);
|
|
|
|
g_free(g2);
|
|
|
|
g_free(g3);
|
|
|
|
g3 = g_memdup(g3, n_bytes); // expected-warning{{Use of memory after it is freed}}
|
|
|
|
}
|
|
|
|
|
2022-02-13 21:02:46 +08:00
|
|
|
void f3(void) {
|
2017-03-09 08:01:01 +08:00
|
|
|
gpointer g1 = g_malloc(n_bytes);
|
|
|
|
gpointer g2 = g_malloc0(n_bytes);
|
|
|
|
g1 = g_realloc(g1, n_bytes * 2);
|
|
|
|
gpointer g3 = g_try_malloc(n_bytes);
|
|
|
|
gpointer g4 = g_try_malloc0(n_bytes);
|
|
|
|
g3 = g_try_realloc(g3, n_bytes * 2); // expected-warning{{Potential leak of memory pointed to by 'g4'}}
|
2017-04-26 13:33:14 +08:00
|
|
|
gpointer g5 = g_malloc_n(n_bytes, sizeof(char));
|
|
|
|
gpointer g6 = g_malloc0_n(n_bytes, sizeof(char));
|
|
|
|
g5 = g_realloc_n(g5, n_bytes * 2, sizeof(char)); // expected-warning{{Potential leak of memory pointed to by 'g6'}}
|
|
|
|
gpointer g7 = g_try_malloc_n(n_bytes, sizeof(char)); // expected-warning{{Potential leak of memory pointed to by 'g5'}}
|
|
|
|
gpointer g8 = g_try_malloc0_n(n_bytes, sizeof(char));
|
|
|
|
g7 = g_try_realloc_n(g7, n_bytes * 2, sizeof(char)); // expected-warning{{Potential leak of memory pointed to by 'g8'}}
|
|
|
|
|
|
|
|
g_free(g1); // expected-warning{{Potential leak of memory pointed to by 'g7'}}
|
|
|
|
g_free(g2);
|
|
|
|
g_free(g3);
|
|
|
|
}
|
|
|
|
|
2022-02-13 21:02:46 +08:00
|
|
|
void f4(void) {
|
2017-04-26 13:33:14 +08:00
|
|
|
gpointer g1 = g_malloc(n_bytes);
|
|
|
|
gpointer g2 = g_malloc0(n_bytes);
|
|
|
|
g1 = g_realloc(g1, n_bytes * 2);
|
|
|
|
gpointer g3 = g_try_malloc(n_bytes);
|
|
|
|
gpointer g4 = g_try_malloc0(n_bytes);
|
|
|
|
g3 = g_try_realloc(g3, n_bytes * 2);
|
|
|
|
gpointer g5 = g_malloc_n(n_bytes, sizeof(char));
|
|
|
|
gpointer g6 = g_malloc0_n(n_bytes, sizeof(char));
|
|
|
|
g5 = g_realloc_n(g5, n_bytes * 2, sizeof(char)); // expected-warning{{Potential leak of memory pointed to by 'g6'}}
|
|
|
|
gpointer g7 = g_try_malloc_n(n_bytes, sizeof(char)); // expected-warning{{Potential leak of memory pointed to by 'g5'}}
|
|
|
|
gpointer g8 = g_try_malloc0_n(n_bytes, sizeof(char));
|
|
|
|
g7 = g_try_realloc_n(g7, n_bytes * 2, sizeof(char)); // expected-warning{{Potential leak of memory pointed to by 'g8'}}
|
|
|
|
|
|
|
|
g_free(g1); // expected-warning{{Potential leak of memory pointed to by 'g7'}}
|
|
|
|
g_free(g2);
|
|
|
|
g_free(g3);
|
|
|
|
g_free(g4);
|
|
|
|
}
|
|
|
|
|
2022-02-13 21:02:46 +08:00
|
|
|
void f5(void) {
|
2017-04-26 13:33:14 +08:00
|
|
|
gpointer g1 = g_malloc(n_bytes);
|
|
|
|
gpointer g2 = g_malloc0(n_bytes);
|
|
|
|
g1 = g_realloc(g1, n_bytes * 2);
|
|
|
|
gpointer g3 = g_try_malloc(n_bytes);
|
|
|
|
gpointer g4 = g_try_malloc0(n_bytes);
|
|
|
|
g3 = g_try_realloc(g3, n_bytes * 2);
|
|
|
|
gpointer g5 = g_malloc_n(n_bytes, sizeof(char));
|
|
|
|
gpointer g6 = g_malloc0_n(n_bytes, sizeof(char));
|
|
|
|
g5 = g_realloc_n(g5, n_bytes * 2, sizeof(char)); // expected-warning{{Potential leak of memory pointed to by 'g6'}}
|
|
|
|
gpointer g7 = g_try_malloc_n(n_bytes, sizeof(char));
|
|
|
|
gpointer g8 = g_try_malloc0_n(n_bytes, sizeof(char));
|
|
|
|
g7 = g_try_realloc_n(g7, n_bytes * 2, sizeof(char)); // expected-warning{{Potential leak of memory pointed to by 'g8'}}
|
|
|
|
|
|
|
|
g_free(g1); // expected-warning{{Potential leak of memory pointed to by 'g7'}}
|
|
|
|
g_free(g2);
|
|
|
|
g_free(g3);
|
|
|
|
g_free(g4);
|
|
|
|
g_free(g5);
|
|
|
|
}
|
|
|
|
|
2022-02-13 21:02:46 +08:00
|
|
|
void f6(void) {
|
2017-04-26 13:33:14 +08:00
|
|
|
gpointer g1 = g_malloc(n_bytes);
|
|
|
|
gpointer g2 = g_malloc0(n_bytes);
|
|
|
|
g1 = g_realloc(g1, n_bytes * 2);
|
|
|
|
gpointer g3 = g_try_malloc(n_bytes);
|
|
|
|
gpointer g4 = g_try_malloc0(n_bytes);
|
|
|
|
g3 = g_try_realloc(g3, n_bytes * 2);
|
|
|
|
gpointer g5 = g_malloc_n(n_bytes, sizeof(char));
|
|
|
|
gpointer g6 = g_malloc0_n(n_bytes, sizeof(char));
|
|
|
|
g5 = g_realloc_n(g5, n_bytes * 2, sizeof(char));
|
|
|
|
gpointer g7 = g_try_malloc_n(n_bytes, sizeof(char));
|
|
|
|
gpointer g8 = g_try_malloc0_n(n_bytes, sizeof(char));
|
|
|
|
g7 = g_try_realloc_n(g7, n_bytes * 2, sizeof(char)); // expected-warning{{Potential leak of memory pointed to by 'g8'}}
|
|
|
|
|
|
|
|
g_free(g1); // expected-warning{{Potential leak of memory pointed to by 'g7'}}
|
|
|
|
g_free(g2);
|
|
|
|
g_free(g3);
|
|
|
|
g_free(g4);
|
|
|
|
g_free(g5);
|
|
|
|
g_free(g6);
|
|
|
|
}
|
|
|
|
|
2022-02-13 21:02:46 +08:00
|
|
|
void f7(void) {
|
2017-04-26 13:33:14 +08:00
|
|
|
gpointer g1 = g_malloc(n_bytes);
|
|
|
|
gpointer g2 = g_malloc0(n_bytes);
|
|
|
|
g1 = g_realloc(g1, n_bytes * 2);
|
|
|
|
gpointer g3 = g_try_malloc(n_bytes);
|
|
|
|
gpointer g4 = g_try_malloc0(n_bytes);
|
|
|
|
g3 = g_try_realloc(g3, n_bytes * 2);
|
|
|
|
gpointer g5 = g_malloc_n(n_bytes, sizeof(char));
|
|
|
|
gpointer g6 = g_malloc0_n(n_bytes, sizeof(char));
|
|
|
|
g5 = g_realloc_n(g5, n_bytes * 2, sizeof(char));
|
|
|
|
gpointer g7 = g_try_malloc_n(n_bytes, sizeof(char));
|
|
|
|
gpointer g8 = g_try_malloc0_n(n_bytes, sizeof(char));
|
|
|
|
g7 = g_try_realloc_n(g7, n_bytes * 2, sizeof(char)); // expected-warning{{Potential leak of memory pointed to by 'g8'}}
|
2017-03-09 08:01:01 +08:00
|
|
|
|
|
|
|
g_free(g1);
|
|
|
|
g_free(g2);
|
|
|
|
g_free(g3);
|
2017-04-26 13:33:14 +08:00
|
|
|
g_free(g4);
|
|
|
|
g_free(g5);
|
|
|
|
g_free(g6);
|
|
|
|
g_free(g7);
|
2017-03-09 08:01:01 +08:00
|
|
|
}
|