Bug 100947 - [BaseScanner] empty string for 91086 fix can create a bad key for the CodeReaderCache
Summary: [BaseScanner] empty string for 91086 fix can create a bad key for the CodeRea...
Status: RESOLVED FIXED
Alias: None
Product: CDT
Classification: Tools
Component: cdt-parser (show other bugs)
Version: 3.0   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.0 RC1   Edit
Assignee: Devin Steffler CLA
QA Contact:
URL:
Whiteboard:
Keywords: contributed
Depends on: 100858
Blocks: 100637
  Show dependency tree
 
Reported: 2005-06-20 15:33 EDT by Devin Steffler CLA
Modified: 2008-06-19 13:00 EDT (History)
1 user (show)

See Also:


Attachments
proposed fix for this PR (2.05 KB, patch)
2005-06-20 16:11 EDT, Devin Steffler CLA
no flags Details | Diff
updated patch for this PR (4.45 KB, patch)
2005-06-23 15:51 EDT, Devin Steffler CLA
no flags Details | Diff
fix for this PR (4.74 KB, patch)
2005-06-24 09:39 EDT, Devin Steffler CLA
no flags Details | Diff
replaced Path#isAbsolute with File#isAbsolute (4.57 KB, patch)
2005-06-24 13:43 EDT, Devin Steffler CLA
bjorn.freeman-benson: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Devin Steffler CLA 2005-06-20 15:33:45 EDT
A bad key can be used to create a CodeReader entry in the CodeReaderCache that 
can not be
modified.

steps to reproduce:
- create a standard make C++ project named "proj"
- create a test.h header file with these contents:
#ifndef _TEST_H_
#define _TEST_H_
int x;
#endif /*_TEST_H_*/
- create a test.cpp source file with these contents:
#include "/proj/test.h"
- create a file named "makefile" with these contents:
test.exe : test.o
	g++ -o test.exe test.o

test.o : test.cpp
	g++ -c test.cpp
	
all : test.exe
clean :
	-rm test.exe test.o
- build the project
- close and reopen the project to pick up the discovered #includes
- add the root project path to the list of project #includes
- open the test.cpp source file in the DOM AST View
- analyze the DOM AST View to ensure the int x declaration is there
- update test.h file so that int x is int y instead
- refresh the DOM AST View by clicking the "Refresh DOM AST" button
- analyze the DOM AST View to see that the int x declaration did not go away

This seems to be caused by the fix for 91086 at BaseScanner:line 2902.  The 
BaseScanner uses an empty string to prefix the filename assuming that the 
filename is an absolute path.

The cache finds the local file to be created and creates a CodeReader 
corresponding to the following bad key:
"\proj\test.h"

The empty string causes a bad key to be used (and continues to be used due to 
the bad #include in test.cpp).
Comment 1 Devin Steffler CLA 2005-06-20 16:11:34 EDT
Created attachment 23571 [details]
proposed fix for this PR
Comment 2 John Camelon CLA 2005-06-21 09:07:31 EDT
I'm not sure if this patch is the completely right answer.
We may need to put some logic in BaseScanner as well.
Talk more when I get in.
Comment 3 John Camelon CLA 2005-06-21 10:52:13 EDT
#include "/proj/test.h" is supposed to not use the search path.  
pls update BaseScanner for this use case.  
Submit a patch for this & 100858 together if you can. 
Thanks
Comment 4 John Camelon CLA 2005-06-21 13:13:58 EDT
Also, we may be able to add a check in somewhere that ensures that a path
provided is a canonical path.  
i.e. instead of using the generated path in the code reader constructor, you can
take that path and do something like 
  path = new File( path ).getCanonicalPath()
Comment 5 John Camelon CLA 2005-06-23 13:10:47 EDT
FYI RC1 starts next Monday.
Pls have these bugs fixed by then or moved off to a later milestone.
Comment 6 Devin Steffler CLA 2005-06-23 15:51:23 EDT
Created attachment 23891 [details]
updated patch for this PR

updated so that the base scanner doesn't use the search path if the #included
file starts with "/" or "\"... can you take a look at this and let me know what
you think?  What do you want to do with the canonical path?  How would it
differ from File.exists()?  I think that the CodeReaderCache.get() should make
sure that the file exists since the path being used as a key should always be
the proper path to a file that exists.
Comment 7 Devin Steffler CLA 2005-06-23 16:02:31 EDT
Comment on attachment 23891 [details]
updated patch for this PR

Invalid patch, fails for windows absolute paths, i.e. C:\temp\file.c... will
update in a bit.
Comment 8 Devin Steffler CLA 2005-06-23 16:03:09 EDT
Comment on attachment 23571 [details]
proposed fix for this PR

old patch
Comment 9 John Camelon CLA 2005-06-23 16:20:35 EDT
Patch is OK ... I would change the following: 

1.  CodeReaderCache shouldn't check File.exists() on get, but only on put() (the
add function).

2.  In ScannerUtility, use File.separatorChar rather than '\\' or '/'
explicitly, it makes sure that on the right file system invalid characters are
not being accepted.

3.  Ditto issue for BaseScanner & File.separatorChar.
Comment 10 Devin Steffler CLA 2005-06-23 16:33:41 EDT
I know what you mean about canonical path checks and File.separatorChar now, 
bad mistakes on my behalf.

Although I disagree with the "only on put instead of get" comment.  If that was 
done then the cache will still always return a valid CodeReader (when it 
shouldn't because the file doesn't exist for the key).  So the problem will 
continue, even though nothing is actually stored in the cache.
Comment 11 John Camelon CLA 2005-06-24 07:25:18 EDT
Q: Is File.exists() an expensive operation?

If so, the code needs to be structured so that it's called less often than at
the begining of every get(). 
Comment 12 Devin Steffler CLA 2005-06-24 09:39:09 EDT
Created attachment 23936 [details]
fix for this PR

This should be it... I can feel it :)
Comment 13 John Camelon CLA 2005-06-24 12:02:30 EDT
Nope.  In BaseScanner you add the following import.
+import org.eclipse.core.runtime.Path;

This will not compile using our loadbuild scripts. The parser/ src folder cannot
depend on anything other than the jre.
Pls retry.
Comment 14 Devin Steffler CLA 2005-06-24 13:43:43 EDT
Created attachment 23956 [details]
replaced Path#isAbsolute with File#isAbsolute
Comment 15 John Camelon CLA 2005-06-24 13:55:41 EDT
Patch applied.
Comment 16 Devin Steffler CLA 2005-06-24 14:14:42 EDT
FIXED in HEAD.