November 20, 2008, 04:47:23 pm *
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: InputStreamReader and OutputStreamWriter without encoding  (Read 1053 times)
Johan Stuyts
Newbie
*
Posts: 5


« on: October 02, 2007, 01:53:30 pm »

In my previous post I gave two scripts for finding encoding problems when converting from String to byte array and vice versa. The same problem with encoding can occur when handling streams, especially if the streams are sent through a network.

This script finds uses of InputStreamReader without an encoding:
from Callable m
where m.fromSource()
and exists(Constructor c |
    c.hasName("InputStreamReader")
    and c.getNumberOfParameters() = 1
    and exists(Class s |
        s.hasQualifiedName("java.io", "InputStream")
        and c.getAParameter().getType() = s)
    and exists(Class s |
        s.hasQualifiedName("java.io", "InputStreamReader")
        and c.getDeclaringType() = s)
    and m.calls(c))
select m.getDeclaringType(), m, m.getAParamType()

And here is the script for OutputStreamWriter:
from Callable m
where m.fromSource()
and exists(Constructor c |
    c.hasName("OutputStreamWriter")
    and c.getNumberOfParameters() = 1
    and exists(Class s |
        s.hasQualifiedName("java.io", "OutputStream")
        and c.getAParameter().getType() = s)
    and exists(Class s |
        s.hasQualifiedName("java.io", "OutputStreamWriter")
        and c.getDeclaringType() = s)
    and m.calls(c))
select m.getDeclaringType(), m, m.getAParamType()

Johan Stuyts
Logged
elnar
Administrator
Newbie
*****
Posts: 17


« Reply #1 on: October 03, 2007, 04:05:07 pm »

Again great queries. Here is another view of the same queries:

Query 1:
Code:
class InputStreamReaderRefType extends RefType {
   InputStreamReaderRefType() {
                this.getASupertype*().hasQualifiedName("java.io","InputStreamReader")
                }
   
   Constructor getInputStreamReaderConstructor() {
                result.getDeclaringType() = this and
                result.getNumberOfParameters() = 1 and
                ((Class)result.getAParameter().getType()).
                        hasQualifiedName("java.io", "InputStream")
                }
}

from Callable m
where m.fromSource() and
      exists(InputStreamReaderRefType t | m.calls(t.getInputStreamReaderConstructor()))
select m.getDeclaringType(), m

Query 2:
Code:
class OutputStreamWriterRefType extends RefType {
   OutputStreamWriterRefType() {
               this.getASupertype*().hasQualifiedName("java.io","OutputStreamWriter")
               }
   
   Constructor getOutputStreamWriterConstructor() {
                result.getDeclaringType() = this and
                result.getNumberOfParameters() = 1 and
                ((Class)result.getAParameter().getType()).
                        hasQualifiedName("java.io", "OutputStream")
                }
}

from Callable m
where m.fromSource() and
      exists(OutputStreamWriterRefType t | m.calls(t.getOutputStreamWriterConstructor()))
select m.getDeclaringType(), m

If you are using an introduced class just for one query, there is no need to put it into the default.ql library. You can define and use new .QL classes as well directly in the quick query editor.

Looking forward to some more cool queries! Smiley
Logged
Pages: [1]
  Print  
 
Jump to: