# HG changeset patch # User dtitov # Date 1532038986 25200 # Node ID c66b192fe3b42ce56821950bbb0944c96b181465 # Parent ee7b0da99262328fdf3e513d3ccbf1927da3a680 8205709: Proper allocation handling Reviewed-by: sspitsyn, mschoene, rhalade diff -r ee7b0da99262 -r c66b192fe3b4 src/java.instrument/unix/native/libinstrument/FileSystemSupport_md.c --- a/src/java.instrument/unix/native/libinstrument/FileSystemSupport_md.c Thu Jul 19 07:02:42 2018 -0700 +++ b/src/java.instrument/unix/native/libinstrument/FileSystemSupport_md.c Thu Jul 19 15:23:06 2018 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2004, 2018 Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,6 +23,7 @@ * questions. */ +#include #include #include @@ -50,6 +51,10 @@ } else { int len = last - path; char* str = (char*)malloc(len+1); + if (str == NULL) { + fprintf(stderr, "OOM error in native tmp buffer allocation"); + return NULL; + } if (len > 0) { memcpy(str, path, len); } @@ -80,6 +85,10 @@ if (n == 0) return strdup("/"); sb = (char*)malloc(strlen(pathname)+1); + if (sb == NULL) { + fprintf(stderr, "OOM error in native tmp buffer allocation"); + return NULL; + } sbLen = 0; if (off > 0) { @@ -128,6 +137,10 @@ len = parentEnd + cn - childStart; if (child[0] == slash) { theChars = (char*)malloc(len+1); + if (theChars == NULL) { + fprintf(stderr, "OOM error in native tmp buffer allocation"); + return NULL; + } if (parentEnd > 0) memcpy(theChars, parent, parentEnd); if (cn > 0) @@ -135,6 +148,10 @@ theChars[len] = '\0'; } else { theChars = (char*)malloc(len+2); + if (theChars == NULL) { + fprintf(stderr, "OOM error in native tmp buffer allocation"); + return NULL; + } if (parentEnd > 0) memcpy(theChars, parent, parentEnd); theChars[parentEnd] = slash; @@ -150,10 +167,13 @@ if (len > 1 && path[len-1] == slash) { // "/foo/" --> "/foo", but "/" --> "/" char* str = (char*)malloc(len); - if (str != NULL) { - memcpy(str, path, len-1); - str[len-1] = '\0'; + if (str == NULL) + { + fprintf(stderr, "OOM error in native tmp buffer allocation"); + return NULL; } + memcpy(str, path, len-1); + str[len-1] = '\0'; return str; } else { return (char*)path; diff -r ee7b0da99262 -r c66b192fe3b4 src/java.instrument/windows/native/libinstrument/FileSystemSupport_md.c --- a/src/java.instrument/windows/native/libinstrument/FileSystemSupport_md.c Thu Jul 19 07:02:42 2018 -0700 +++ b/src/java.instrument/windows/native/libinstrument/FileSystemSupport_md.c Thu Jul 19 15:23:06 2018 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2004, 2018 Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,6 +23,7 @@ * questions. */ +#include #include #include #include @@ -66,6 +67,10 @@ } else { int len = (int)(last - path); char* str = (char*)malloc(len+1); + if (str == NULL) { + fprintf(stderr, "OOM error in native tmp buffer allocation"); + return NULL; + } if (len > 0) { memcpy(str, path, len); } @@ -135,6 +140,10 @@ if (off < 3) off = 0; /* Avoid fencepost cases with UNC pathnames */ sb = (char*)malloc(len+1); + if (sb == NULL) { + fprintf(stderr, "OOM error in native tmp buffer allocation"); + return NULL; + } sbLen = 0; if (off == 0) { @@ -261,11 +270,19 @@ if (child[childStart] == slash) { theChars = (char*)malloc(len+1); + if (theChars == NULL) { + fprintf(stderr, "OOM error in native tmp buffer allocation"); + return NULL; + } memcpy(theChars, parent, parentEnd); memcpy(theChars+parentEnd, child+childStart, (cn-childStart)); theChars[len] = '\0'; } else { theChars = (char*)malloc(len+2); + if (theChars == NULL) { + fprintf(stderr, "OOM error in native tmp buffer allocation"); + return NULL; + } memcpy(theChars, parent, parentEnd); theChars[parentEnd] = slash; memcpy(theChars+parentEnd+1, child+childStart, (cn-childStart)); @@ -320,10 +337,12 @@ return (char*)path; } else { char* p = (char*)malloc(len+1); - if (p != NULL) { - memcpy(p, path+start, len); - p[len] = '\0'; + if (p == NULL) { + fprintf(stderr, "OOM error in native tmp buffer allocation"); + return NULL; } + memcpy(p, path+start, len); + p[len] = '\0'; return p; } }