Skip to content

Commit 5c4be9c

Browse files
committed
8230466: check malloc/calloc results in jdk.hotspot.agent
Reviewed-by: cjplummer, ysuenaga, sspitsyn
1 parent 4b65e2b commit 5c4be9c

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

src/jdk.hotspot.agent/linux/native/libsaproc/symtab.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -210,6 +210,9 @@ static int open_file_from_debug_link(const char *name,
210210
+ strlen(".debug/")
211211
+ strlen(debug_file_directory)
212212
+ 2);
213+
if (debug_pathname == NULL) {
214+
return -1;
215+
}
213216
strcpy(debug_pathname, name);
214217
char *last_slash = strrchr(debug_pathname, '/');
215218
if (last_slash == NULL) {
@@ -279,6 +282,9 @@ build_id_to_debug_filename (size_t size, unsigned char *data)
279282

280283
filename = malloc(strlen (debug_file_directory) + (sizeof "/.build-id/" - 1) + 1
281284
+ 2 * size + (sizeof ".debug" - 1) + 1);
285+
if (filename == NULL) {
286+
return NULL;
287+
}
282288
s = filename + sprintf (filename, "%s/.build-id/", debug_file_directory);
283289
if (size > 0)
284290
{
@@ -305,7 +311,9 @@ static struct symtab* build_symtab_from_build_id(Elf64_Nhdr *note)
305311
= (unsigned char*)(note+1) + note->n_namesz;
306312
char *filename
307313
= (build_id_to_debug_filename (note->n_descsz, bytes));
308-
314+
if (filename == NULL) {
315+
return NULL;
316+
}
309317
fd = pathmap_open(filename);
310318
if (fd >= 0) {
311319
symtab = build_symtab_internal(fd, NULL, /* try_debuginfo */ false);
@@ -417,6 +425,10 @@ static struct symtab* build_symtab_internal(int fd, const char *filename, bool t
417425
htab_sz = n*1.25;
418426

419427
symtab->hash_table = (struct hsearch_data*) calloc(1, sizeof(struct hsearch_data));
428+
if (symtab->hash_table == NULL) {
429+
goto bad;
430+
}
431+
420432
rslt = hcreate_r(n, symtab->hash_table);
421433
// guarantee(rslt, "unexpected failure: hcreate_r");
422434

@@ -426,11 +438,17 @@ static struct symtab* build_symtab_internal(int fd, const char *filename, bool t
426438
// strings will not be destroyed by elf_end.
427439
size = scn_cache[shdr->sh_link].c_shdr->sh_size;
428440
symtab->strs = (char *)malloc(size);
441+
if (symtab->strs == NULL) {
442+
goto bad;
443+
}
429444
memcpy(symtab->strs, scn_cache[shdr->sh_link].c_data, size);
430445

431446
// allocate memory for storing symbol offset and size;
432447
symtab->num_symbols = n;
433448
symtab->symbols = (struct elf_symbol *)calloc(n , sizeof(struct elf_symbol));
449+
if (symtab->symbols == NULL) {
450+
goto bad;
451+
}
434452

435453
// copy symbols info our symtab and enter them info the hash table
436454
for (j = 0; j < n; j++, syms++) {
@@ -512,6 +530,11 @@ static struct symtab* build_symtab_internal(int fd, const char *filename, bool t
512530
symtab = prev_symtab;
513531
}
514532
}
533+
goto quit;
534+
535+
bad:
536+
destroy_symtab(symtab);
537+
symtab = NULL;
515538

516539
quit:
517540
if (shbuf) free(shbuf);

src/jdk.hotspot.agent/macosx/native/libsaproc/MacosxDebuggerLocal.m

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -373,7 +373,16 @@ jbyteArray readBytesFromCore(
373373

374374
// Allocate storage for pages and flags.
375375
pages = malloc(pageCount * sizeof(vm_offset_t));
376+
if (pages == NULL) {
377+
(*env)->DeleteLocalRef(env, array);
378+
return NULL;
379+
}
376380
mapped = calloc(pageCount, sizeof(int));
381+
if (mapped == NULL) {
382+
(*env)->DeleteLocalRef(env, array);
383+
free(pages);
384+
return NULL;
385+
}
377386

378387
task_t gTask = getTask(env, this_obj);
379388
// Try to read each of the pages.

src/jdk.hotspot.agent/macosx/native/libsaproc/symtab.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -69,18 +69,22 @@ void build_search_table(symtab_t *symtab) {
6969
if (is_debug()) {
7070
DBT rkey, rvalue;
7171
char* tmp = (char *)malloc(strlen(symtab->symbols[i].name) + 1);
72-
strcpy(tmp, symtab->symbols[i].name);
73-
rkey.data = tmp;
74-
rkey.size = strlen(tmp) + 1;
75-
(*symtab->hash_table->get)(symtab->hash_table, &rkey, &rvalue, 0);
76-
// we may get a copy back so compare contents
77-
symtab_symbol *res = (symtab_symbol *)rvalue.data;
78-
if (strcmp(res->name, symtab->symbols[i].name) ||
72+
if (tmp == NULL) {
73+
print_debug("error allocating array in build_search_table\n");
74+
} else {
75+
strcpy(tmp, symtab->symbols[i].name);
76+
rkey.data = tmp;
77+
rkey.size = strlen(tmp) + 1;
78+
(*symtab->hash_table->get)(symtab->hash_table, &rkey, &rvalue, 0);
79+
// we may get a copy back so compare contents
80+
symtab_symbol *res = (symtab_symbol *)rvalue.data;
81+
if (strcmp(res->name, symtab->symbols[i].name) ||
7982
res->offset != symtab->symbols[i].offset ||
8083
res->size != symtab->symbols[i].size) {
81-
print_debug("error to get hash_table value!\n");
84+
print_debug("error to get hash_table value!\n");
85+
}
86+
free(tmp);
8287
}
83-
free(tmp);
8488
}
8589
}
8690
}

0 commit comments

Comments
 (0)