Skip to content

Commit db44c33

Browse files
authored
Merge pull request #76 from VPeruS/fix-buffer
Fixed buffer overflow error while formatting huge files
2 parents 5ee4465 + d3a19ef commit db44c33

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

src/extension.ts

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -181,33 +181,6 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
181181
let formatCommandBinPath = getBinPath(this.getExecutablePath());
182182
let codeContent = document.getText();
183183

184-
let childCompleted = (err, stdout, stderr) => {
185-
try {
186-
if (err && (<any>err).code === 'ENOENT') {
187-
vscode.window.showInformationMessage('The \'' + formatCommandBinPath + '\' command is not available. Please check your clang-format.executable user setting and ensure it is installed.');
188-
return resolve(null);
189-
}
190-
if (stderr) {
191-
outputChannel.show();
192-
outputChannel.clear();
193-
outputChannel.appendLine(stderr);
194-
return reject('Cannot format due to syntax errors.');
195-
}
196-
if (err) {
197-
return reject();
198-
}
199-
200-
let dummyProcessor = (value: string) => {
201-
debugger;
202-
return value;
203-
};
204-
return resolve(this.getEdits(document, stdout, codeContent));
205-
206-
} catch (e) {
207-
reject(e);
208-
}
209-
};
210-
211184
let formatArgs = [
212185
'-output-replacements-xml',
213186
`-style=${this.getStyle(document)}`,
@@ -232,8 +205,37 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
232205
workingPath = path.dirname(document.fileName);
233206
}
234207

235-
let child = cp.execFile(formatCommandBinPath, formatArgs, { cwd: workingPath }, childCompleted);
208+
let stdout = '';
209+
let stderr = '';
210+
let child = cp.spawn(formatCommandBinPath, formatArgs, { cwd: workingPath });
236211
child.stdin.end(codeContent);
212+
child.stdout.on('data', chunk => stdout += chunk);
213+
child.stderr.on('data', chunk => stderr += chunk);
214+
child.on('error', err => {
215+
if (err && (<any>err).code === 'ENOENT') {
216+
vscode.window.showInformationMessage('The \'' + formatCommandBinPath + '\' command is not available. Please check your clang-format.executable user setting and ensure it is installed.');
217+
return resolve(null);
218+
}
219+
return reject(err);
220+
});
221+
child.on('close', code => {
222+
try {
223+
if (stderr.length != 0) {
224+
outputChannel.show();
225+
outputChannel.clear();
226+
outputChannel.appendLine(stderr);
227+
return reject('Cannot format due to syntax errors.');
228+
}
229+
230+
if (code != 0) {
231+
return reject();
232+
}
233+
234+
return resolve(this.getEdits(document, stdout, codeContent));
235+
} catch (e) {
236+
reject(e);
237+
}
238+
});
237239

238240
if (token) {
239241
token.onCancellationRequested(() => {

0 commit comments

Comments
 (0)