【问题标题】:Improper Resource Shutdown or Release资源关闭或释放不当
【发布时间】:2019-09-07 20:23:19
【问题描述】:

说明: 在下面的代码中我在下面的代码中做错了什么,在我的代码审计中,我得到了不正确的资源关闭或释放。 我尝试从下面的代码中取出关闭和刷新

    File someFile = new File(fileName);

                   fos = new FileOutputStream(someFile);
                   fos.write(data);
                   fos.flush();
                   fos.close();

主要代码:

    FileOutputStream fos=null;
    try {
       Hashtable hash = responseBlob.getAllAttachments();
       Enumeration e = hash.elements();

       while (e.hasMoreElements()) {

     SBADataAttach tmpAttach = (SBADataAttach) e.nextElement();

     String tag = tmpAttach.getTag();
          byte[] data = tmpAttach.getData();
          // encode compressed file
          if (hasTag(tag))
             mimeResponse.addPart(tag, Base64.encodeBytes(data)
                   .getBytes());
          else
             mimeResponse.addPart(tag, data);
          try {

             if (this.configData.getBlobPath() != null) 
    {
                // save compressed file
                String fileName = File.separatorChar + "tmp"
                      + File.separatorChar + tag;
                Log.theLogger.debug("XisServlet.process() ... "
                      + "Save compressed file = " + fileName);


                   File someFile = new File(fileName);
                   fos = new FileOutputStream(
                         someFile);
                   fos.write(data);
                   fos.flush();
                   fos.close();

             }

                   } catch (Exception zipe) {
             Log.theLogger.error(zipe.getMessage(), zipe);
          }
       }
    } catch (SBADataException sde) {
       // cannot detach files from blob
       Log.theLogger.error(sde.getMessage(), sde);
    } 
    finally {
       try {
               if( fos!=null ) {
                  fos.close();
               }
           } catch(IOException e) {
              Log.theLogger.error(e.getMessage(), e);
           }
          }

我没有收到任何错误。但在 Appsec 发现中,我得到了不正确的资源关闭或释放

【问题讨论】:

  • 如果new FileOutputStream(someFile);fos.close() 之间的某些代码引发异常,您的代码可能不会关闭fos
  • 我真的需要 fos.close();正如我在做 fos.close 一样,最后也终于 { try { if( fos!=null ) { fos.close(); } } catch(IOException e) { Log.theLogger.error(e.getMessage(), e); } }
  • 使用 try-with-resource 语句。
  • 我已将代码更改为尝试{ //打开资源 File someFile = new File(fileName); fos = new FileOutputStream(someFile); fos.write(数据); } catch(Exception e1){ //处理异常 }finally{ //关闭资源 fos.flush(); fos.close(); } }

标签: java xml security


【解决方案1】:
try{
                                //open resources
                                File someFile = new File(fileName);
                                fos = new FileOutputStream(someFile);
                                fos.write(data);
                            }
                            catch(Exception e1){
                                //handle exception
                            }finally{
                                //close resources
                                fos.flush();
                                fos.close();
                            }


                        }

【讨论】:

    猜你喜欢
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 2013-04-02
    • 2018-12-08
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    相关资源
    最近更新 更多