< prev index next >

src/java.base/windows/native/libjava/io_util_md.c

Print this page
rev 50965 : imported patch 8207060-Memory-leak-when-malloc-fails-within-WITH_UNICODE_STRING-block


 148 /* If this returns NULL then an exception is pending */
 149 WCHAR*
 150 pathToNTPath(JNIEnv *env, jstring path, jboolean throwFNFE) {
 151     int pathlen = 0;
 152     WCHAR *pathbuf = NULL;
 153     int max_path = 248; /* CreateDirectoryW() has the limit of 248 */
 154 
 155     WITH_UNICODE_STRING(env, path, ps) {
 156         pathlen = (int)wcslen(ps);
 157         if (pathlen != 0) {
 158             if (pathlen > 2 &&
 159                 (ps[0] == L'\\' && ps[1] == L'\\' ||   //UNC
 160                  ps[1] == L':' && ps[2] == L'\\'))     //absolute
 161             {
 162                  if (pathlen > max_path - 1) {
 163                      pathbuf = prefixAbpath(ps, pathlen, pathlen);
 164                  } else {
 165                      pathbuf = (WCHAR*)malloc((pathlen + 6) * sizeof(WCHAR));
 166                      if (pathbuf != 0) {
 167                          wcscpy(pathbuf, ps);
 168                      } else {
 169                          JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
 170                          return NULL;
 171                      }
 172                  }
 173             } else {
 174                 /* If the path came in as a relative path, need to verify if
 175                    its absolute form is bigger than max_path or not, if yes
 176                    need to (1)convert it to absolute and (2)prefix. This is
 177                    obviously a burden to all relative paths (The current dir/len
 178                    for "drive & directory" relative path is cached, so we only
 179                    calculate it once but for "drive-relative path we call
 180                    _wgetdcwd() and wcslen() everytime), but a hit we have
 181                    to take if we want to support relative path beyond max_path.
 182                    There is no way to predict how long the absolute path will be
 183                    (therefor allocate the sufficient memory block) before calling
 184                    _wfullpath(), we have to get the length of "current" dir first.
 185                 */
 186                 WCHAR *abpath = NULL;
 187                 int dirlen = currentDirLength(ps, pathlen);
 188                 if (dirlen + pathlen + 1 > max_path - 1) {
 189                     pathbuf = prefixAbpath(ps, pathlen, dirlen + pathlen);
 190                 } else {
 191                     pathbuf = (WCHAR*)malloc((pathlen + 6) * sizeof(WCHAR));
 192                     if (pathbuf != 0) {
 193                         wcscpy(pathbuf, ps);
 194                     } else {
 195                         JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
 196                         return NULL;
 197                     }
 198                 }
 199             }
 200         }
 201     } END_UNICODE_STRING(env, ps);
 202 
 203     if (pathlen == 0) {
 204         if (throwFNFE == JNI_TRUE) {
 205             if (!(*env)->ExceptionCheck(env)) {
 206                 throwFileNotFoundException(env, path);
 207             }
 208             return NULL;
 209         } else {
 210             pathbuf = (WCHAR*)malloc(sizeof(WCHAR));
 211             if (pathbuf != NULL) {
 212                 pathbuf[0] = L'\0';
 213             } else {
 214                 JNU_ThrowOutOfMemoryError(env, 0);
 215                 return NULL;
 216             }
 217         }
 218     }
 219     if (pathbuf == 0) {
 220         JNU_ThrowOutOfMemoryError(env, 0);
 221         return NULL;
 222     }
 223     return pathbuf;
 224 }
 225 
 226 JNIEXPORT FD JNICALL
 227 winFileHandleOpen(JNIEnv *env, jstring path, int flags)
 228 {
 229     const DWORD access =
 230         (flags & O_WRONLY) ?  GENERIC_WRITE :
 231         (flags & O_RDWR)   ? (GENERIC_READ | GENERIC_WRITE) :
 232         GENERIC_READ;
 233     const DWORD sharing =
 234         FILE_SHARE_READ | FILE_SHARE_WRITE;
 235     const DWORD disposition =
 236         /* Note: O_TRUNC overrides O_CREAT */
 237         (flags & O_TRUNC) ? CREATE_ALWAYS :
 238         (flags & O_CREAT) ? OPEN_ALWAYS   :
 239         OPEN_EXISTING;
 240     const DWORD  maybeWriteThrough =
 241         (flags & (O_SYNC | O_DSYNC)) ?




 148 /* If this returns NULL then an exception is pending */
 149 WCHAR*
 150 pathToNTPath(JNIEnv *env, jstring path, jboolean throwFNFE) {
 151     int pathlen = 0;
 152     WCHAR *pathbuf = NULL;
 153     int max_path = 248; /* CreateDirectoryW() has the limit of 248 */
 154 
 155     WITH_UNICODE_STRING(env, path, ps) {
 156         pathlen = (int)wcslen(ps);
 157         if (pathlen != 0) {
 158             if (pathlen > 2 &&
 159                 (ps[0] == L'\\' && ps[1] == L'\\' ||   //UNC
 160                  ps[1] == L':' && ps[2] == L'\\'))     //absolute
 161             {
 162                  if (pathlen > max_path - 1) {
 163                      pathbuf = prefixAbpath(ps, pathlen, pathlen);
 164                  } else {
 165                      pathbuf = (WCHAR*)malloc((pathlen + 6) * sizeof(WCHAR));
 166                      if (pathbuf != 0) {
 167                          wcscpy(pathbuf, ps);



 168                      }
 169                  }
 170             } else {
 171                 /* If the path came in as a relative path, need to verify if
 172                    its absolute form is bigger than max_path or not, if yes
 173                    need to (1)convert it to absolute and (2)prefix. This is
 174                    obviously a burden to all relative paths (The current dir/len
 175                    for "drive & directory" relative path is cached, so we only
 176                    calculate it once but for "drive-relative path we call
 177                    _wgetdcwd() and wcslen() everytime), but a hit we have
 178                    to take if we want to support relative path beyond max_path.
 179                    There is no way to predict how long the absolute path will be
 180                    (therefor allocate the sufficient memory block) before calling
 181                    _wfullpath(), we have to get the length of "current" dir first.
 182                 */
 183                 WCHAR *abpath = NULL;
 184                 int dirlen = currentDirLength(ps, pathlen);
 185                 if (dirlen + pathlen + 1 > max_path - 1) {
 186                     pathbuf = prefixAbpath(ps, pathlen, dirlen + pathlen);
 187                 } else {
 188                     pathbuf = (WCHAR*)malloc((pathlen + 6) * sizeof(WCHAR));
 189                     if (pathbuf != 0) {
 190                         wcscpy(pathbuf, ps);



 191                     }
 192                 }
 193             }
 194         }
 195     } END_UNICODE_STRING(env, ps);
 196 
 197     if (pathlen == 0) {
 198         if (throwFNFE == JNI_TRUE) {
 199             if (!(*env)->ExceptionCheck(env)) {
 200                 throwFileNotFoundException(env, path);
 201             }
 202             return NULL;
 203         } else {
 204             pathbuf = (WCHAR*)malloc(sizeof(WCHAR));
 205             if (pathbuf != NULL) {
 206                 pathbuf[0] = L'\0';



 207             }
 208         }
 209     }
 210     if (pathbuf == 0) {
 211         JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");

 212     }
 213     return pathbuf;
 214 }
 215 
 216 JNIEXPORT FD JNICALL
 217 winFileHandleOpen(JNIEnv *env, jstring path, int flags)
 218 {
 219     const DWORD access =
 220         (flags & O_WRONLY) ?  GENERIC_WRITE :
 221         (flags & O_RDWR)   ? (GENERIC_READ | GENERIC_WRITE) :
 222         GENERIC_READ;
 223     const DWORD sharing =
 224         FILE_SHARE_READ | FILE_SHARE_WRITE;
 225     const DWORD disposition =
 226         /* Note: O_TRUNC overrides O_CREAT */
 227         (flags & O_TRUNC) ? CREATE_ALWAYS :
 228         (flags & O_CREAT) ? OPEN_ALWAYS   :
 229         OPEN_EXISTING;
 230     const DWORD  maybeWriteThrough =
 231         (flags & (O_SYNC | O_DSYNC)) ?


< prev index next >