Skip to content

Fix handling of escaped string in tokenizer + PTVS launch hardening #1377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 34 commits into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a764bc9
Undo changes
Feb 13, 2018
9d1b2cc
Test fixes
Feb 13, 2018
a91291a
Increase timeout
Mar 2, 2018
bf266af
Remove double event listening
Mar 7, 2018
7bc6bd6
Remove test
Mar 7, 2018
8ce8b48
Revert "Remove test"
Mar 7, 2018
e3a549e
Revert "Remove double event listening"
Mar 7, 2018
92e8c1e
#1096 The if statement is automatically formatted incorrectly
Mar 27, 2018
b540a1d
Merge fix
Mar 27, 2018
7b0573e
Add more tests
Mar 27, 2018
facb106
More tests
Mar 27, 2018
f113881
Typo
Mar 27, 2018
3e76718
Test
Mar 28, 2018
6e85dc6
Also better handle multiline arguments
Mar 28, 2018
99e037c
Add a couple missing periods
brettcannon Mar 28, 2018
3caeab7
Undo changes
Feb 13, 2018
eeb1f11
Test fixes
Feb 13, 2018
f5f78c7
Increase timeout
Mar 2, 2018
88744da
Remove double event listening
Mar 7, 2018
65dde44
Remove test
Mar 7, 2018
c513f71
Revert "Remove test"
Mar 7, 2018
ccb3886
Revert "Remove double event listening"
Mar 7, 2018
106f4db
Merge fix
Mar 27, 2018
9e5cb43
Merge branch 'master' of https://github.com/MikhailArkhipov/vscode-py…
Apr 5, 2018
e1da6a6
#1257 On type formatting errors for args and kwargs
Apr 5, 2018
e78f0fb
Handle f-strings
Apr 5, 2018
725cf71
Stop importing from test code
Apr 5, 2018
5cd6d45
#1308 Single line statements leading to an indentation on the next line
Apr 5, 2018
27613db
#726 editing python after inline if statement invalid indent
Apr 5, 2018
8061a20
Undo change
Apr 5, 2018
17dc292
Move constant
Apr 5, 2018
65964b9
Harden LS startup error checks
Apr 10, 2018
4bf5a4c
#1364 Intellisense doesn't work after specific const string
Apr 10, 2018
6f7212c
Merge branch 'master' of https://github.com/Microsoft/vscode-python
Apr 12, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions src/client/activation/analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

import * as path from 'path';
import { ExtensionContext, OutputChannel } from 'vscode';
import { Disposable, LanguageClient, LanguageClientOptions, ServerOptions } from 'vscode-languageclient';
import { Message } from 'vscode-jsonrpc';
import { CloseAction, Disposable, ErrorAction, ErrorHandler, LanguageClient, LanguageClientOptions, ServerOptions } from 'vscode-languageclient';
import { IApplicationShell } from '../common/application/types';
import { isTestExecution, STANDARD_OUTPUT_CHANNEL } from '../common/constants';
import { createDeferred, Deferred } from '../common/helpers';
import { IFileSystem, IPlatformService } from '../common/platform/types';
import { IProcessService } from '../common/process/types';
import { StopWatch } from '../common/stopWatch';
Expand All @@ -21,6 +23,18 @@ const dotNetCommand = 'dotnet';
const languageClientName = 'Python Tools';
const analysisEngineFolder = 'analysis';

class LanguageServerStartupErrorHandler implements ErrorHandler {
constructor(private readonly deferred: Deferred<void>) { }
public error(error: Error, message: Message, count: number): ErrorAction {
this.deferred.reject();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.deferred.reject(error)

return ErrorAction.Shutdown;
}
public closed(): CloseAction {
this.deferred.reject();
return CloseAction.DoNotRestart;
}
}

export class AnalysisExtensionActivator implements IExtensionActivator {
private readonly configuration: IConfigurationService;
private readonly appShell: IApplicationShell;
Expand Down Expand Up @@ -92,16 +106,23 @@ export class AnalysisExtensionActivator implements IExtensionActivator {

private async tryStartLanguageClient(context: ExtensionContext, lc: LanguageClient): Promise<void> {
let disposable: Disposable | undefined;
const deferred = createDeferred<void>();
try {
lc.clientOptions.errorHandler = new LanguageServerStartupErrorHandler(deferred);

disposable = lc.start();
await lc.onReady();
lc.onReady()
.then(() => deferred.resolve())
.catch(ex => deferred.reject());
await deferred.promise;

this.output.appendLine(`Language server ready: ${this.sw.elapsedTime} ms`);
context.subscriptions.push(disposable);
} catch (ex) {
if (disposable) {
disposable.dispose();
throw ex;
}
throw ex;
}
}

Expand Down Expand Up @@ -157,12 +178,8 @@ export class AnalysisExtensionActivator implements IExtensionActivator {
// tslint:disable-next-line:no-string-literal
properties['SearchPaths'] = searchPaths;

if (isTestExecution()) {
// tslint:disable-next-line:no-string-literal
properties['TestEnvironment'] = true;
}

const selector: string[] = [PYTHON];

// Options to control the language client
return {
// Register the server for Python documents
Expand All @@ -181,7 +198,8 @@ export class AnalysisExtensionActivator implements IExtensionActivator {
trimDocumentationText: false,
maxDocumentationTextLength: 0
},
asyncStartup: true
asyncStartup: true,
testEnvironment: isTestExecution()
}
};
}
Expand Down
3 changes: 3 additions & 0 deletions src/client/language/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@ export class Tokenizer implements ITokenizer {

private skipToSingleEndQuote(quote: number): void {
while (!this.cs.isEndOfStream()) {
if (this.cs.currentChar === Char.LineFeed || this.cs.currentChar === Char.CarriageReturn) {
return; // Unterminated single-line string
}
if (this.cs.currentChar === Char.Backslash && this.cs.nextChar === quote) {
this.cs.advance(2);
continue;
Expand Down
17 changes: 17 additions & 0 deletions src/test/language/tokenizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,23 @@ suite('Language.Tokenizer', () => {
assert.equal(tokens.getItemAt(0).type, TokenType.String);
assert.equal(tokens.getItemAt(0).length, 14);
});
test('Strings: escape at the end of single quoted string ', async () => {
const t = new Tokenizer();
// tslint:disable-next-line:quotemark
const tokens = t.tokenize("'quoted\\'\nx");
assert.equal(tokens.count, 2);
assert.equal(tokens.getItemAt(0).type, TokenType.String);
assert.equal(tokens.getItemAt(0).length, 9);
assert.equal(tokens.getItemAt(1).type, TokenType.Identifier);
});
test('Strings: escape at the end of double quoted string ', async () => {
const t = new Tokenizer();
const tokens = t.tokenize('"quoted\\"\nx');
assert.equal(tokens.count, 2);
assert.equal(tokens.getItemAt(0).type, TokenType.String);
assert.equal(tokens.getItemAt(0).length, 9);
assert.equal(tokens.getItemAt(1).type, TokenType.Identifier);
});
test('Comments', async () => {
const t = new Tokenizer();
const tokens = t.tokenize(' #co"""mment1\n\t\n#comm\'ent2 ');
Expand Down