fix problems found by deepscan.io

This commit is contained in:
undefined committed 2020-11-08 23:44:16 +08:00
1 parent e8dcd97561
commit dd4ecdb029
13 files changed
+33 -29

No files matched your search

+3 -2
View File
@@ -4,6 +4,7 @@ import axios from 'axios';
import AdmZip from 'adm-zip';
import fs from 'fs-extra';
import WebSocket from 'ws';
import { noop } from 'lodash';
import * as tmpfs from '../tmpfs';
import log from '../log';
import { compilerText, Queue } from '../utils';
@@ -99,7 +100,7 @@ class JudgeTask {
});
}
}
for (const clean of this.clean) await clean().catch();
for (const clean of this.clean) await clean().catch(noop);
tmpfs.umount(this.tmpdir);
fs.removeSync(this.tmpdir);
}
@@ -257,7 +258,7 @@ export default class VJ4 {
});
});
await fs.unlink(tmpFilePath);
await this.processData(savePath).catch();
await this.processData(savePath).catch(noop);
} catch (e) {
if (retry) await this.problemData(domainId, pid, savePath, retry - 1);
else throw e;
-1
View File
@@ -63,7 +63,6 @@ function judgeCase(c) {
});
}
} else if (status === STATUS.STATUS_RUNTIME_ERROR && code) {
status = STATUS.STATUS_RUNTIME_ERROR;
if (code < 32) message = signals[code];
else message = `您的程序返回了 ${code}.`;
}
+1 -1
View File
@@ -20,7 +20,7 @@ const statusMap = {
};
function proc({
execute = null,
execute = '',
time_limit_ms = 16000,
memory_limit_mb = 1024,
process_limit = 32,
+3 -2
View File
@@ -6,6 +6,7 @@ import { ObjectID } from 'bson';
import fs from 'fs-extra';
import { homedir, tmpdir } from 'os';
import AdmZip from 'adm-zip';
import { noop } from 'lodash';
declare module 'hydrooj/dist/interface' {
interface SystemKeys {
@@ -83,7 +84,7 @@ async function postInit() {
});
});
await fs.unlink(tmpFilePath);
await processData(savePath).catch();
await processData(savePath).catch(noop);
return savePath;
}
@@ -239,7 +240,7 @@ async function postInit() {
}
}
// eslint-disable-next-line no-await-in-loop
for (const clean of this.clean) await clean().catch();
for (const clean of this.clean) await clean().catch(noop);
tmpfs.umount(this.tmpdir);
fs.removeSync(this.tmpdir);
}
+2 -1
View File
@@ -2,6 +2,7 @@ import path from 'path';
import fs from 'fs-extra';
import systeminformation from 'systeminformation';
import { tmpdir } from 'os';
import { noop } from 'lodash';
import { judge } from './judge/run';
import * as tmpfs from './tmpfs';
@@ -49,7 +50,7 @@ int main(){
tmpfs.mount(context.tmpdir, '64m');
await judge(context).catch((e) => console.error(e));
// eslint-disable-next-line no-await-in-loop
for (const clean of context.clean) await clean().catch();
for (const clean of context.clean) await clean().catch(noop);
tmpfs.umount(context.tmpdir);
fs.removeSync(context.tmpdir);
} catch (e) {
+2
View File
@@ -13,6 +13,8 @@ const EMPTY_STR = /^[ \r\n\t]*$/i;
export const cmd = parse;
export function noop() { }
export function parseTimeMS(str: string) {
const match = TIME_RE.exec(str);
if (!match) throw new FormatError(str, ['error parsing time']);
+1 -1
View File
@@ -323,8 +323,8 @@ export class ProblemPretestConnectionHandler extends ConnectionHandler {
@param('pid', Types.String)
async prepare(domainId: string, pid: string) {
const pdoc = await problem.get(domainId, pid);
if (pdoc.hidden) this.checkPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN);
if (!pdoc) throw new ProblemNotFoundError(domainId, pid);
if (pdoc.hidden) this.checkPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN);
this.pid = pdoc.docId.toString();
this.domainId = domainId;
this.dispose = bus.on('record/change', this.onRecordChange.bind(this));
+4 -5
View File
@@ -1,8 +1,7 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { Readable, Writable } from 'stream';
import { ObjectID } from 'mongodb';
import fs from 'fs';
import { Dictionary, NumericDictionary } from 'lodash';
import type { Readable, Writable } from 'stream';
import type { ObjectID } from 'mongodb';
import type fs from 'fs';
import type { Dictionary, NumericDictionary } from 'lodash';
import type { Pdoc } from './model/problem';
type document = typeof import('./model/document');
+2 -2
View File
@@ -55,8 +55,8 @@ export async function getWithSecret(_id: ObjectID, secret: string, reject?: Func
const file = await coll.findOne({ _id });
if (!file) throw new NotFoundError(_id);
const timestamp = _timestamp();
if (!(hash(file.secret, timestamp.toString()) === secret)) {
if (!(hash(file.secret, (timestamp - 1).toString()) === secret)) {
if (hash(file.secret, timestamp.toString()) !== secret) {
if (hash(file.secret, (timestamp - 1).toString()) !== secret) {
throw new ForbiddenError();
}
}
+1 -1
View File
@@ -61,7 +61,7 @@ export async function judge(domainId: string, rid: ObjectID) {
if (rdoc.pid) {
const pdoc = await problem.get(domainId, rdoc.pid);
if (!config) config = pdoc?.config || {};
data = pdoc.data;
data = pdoc?.data;
}
delete rdoc._id;
await task.add({
+1 -1
View File
@@ -129,7 +129,7 @@ export class User implements _User {
checkPassword(password: string) {
const h = global.Hydro.lib[`hash.${this.hashType}`];
if (!h) throw new Error('Unknown hash method');
if (!(h(password, this.salt(), this) === this.hash())) {
if (h(password, this.salt(), this) !== this.hash()) {
throw new LoginError(this.uname);
} else if (this.hashType !== 'hydro') {
setPassword(this._id, password);
+11 -10
View File
@@ -440,8 +440,7 @@ export class Handler {
args.domainId = args.domainId || this.domainId;
}
const { anchor } = args;
if (query) res = router.url(name, args, { query });
else res = router.url(name, args);
res = router.url(name, args, { query });
if (anchor) return `${res}#${anchor}`;
} catch (e) {
console.error(e.message);
@@ -563,10 +562,8 @@ export class Handler {
this.ctx.response.status = 302;
this.ctx.redirect(this.response.redirect);
} else {
if (this.response.body != null) {
this.ctx.response.body = this.response.body;
this.ctx.response.status = this.response.status || 200;
}
this.ctx.response.body = this.response.body;
this.ctx.response.status = this.response.status || 200;
this.ctx.response.type = this.request.json
? 'application/json'
: this.response.type
@@ -683,7 +680,7 @@ const Checker = (permPrivChecker) => {
if (typeof item === 'object') {
if (typeof item.call !== 'undefined') {
checker = item;
} if (typeof item[0] === 'number') {
} else if (typeof item[0] === 'number') {
priv = item;
} else if (typeof item[0] === 'bigint') {
perm = item;
@@ -772,18 +769,22 @@ export class ConnectionHandler {
url(name: string, kwargs: any = {}) {
let res = '#';
const args: any = {};
const query: any = {};
for (const key in kwargs) {
if (kwargs[key] instanceof ObjectID) args[key] = kwargs[key].toHexString();
else args[key] = kwargs[key].toString();
}
for (const key in kwargs.query || {}) {
if (query[key] instanceof ObjectID) query[key] = kwargs.query[key].toHexString();
else query[key] = kwargs.query[key].toString();
}
try {
if (this.args.domainId !== 'system' || args.domainId) {
name += '_with_domainId';
args.domainId = args.domainId || this.args.domainId;
}
const { anchor, query } = args;
if (query) res = router.url(name, args, { query });
else res = router.url(name, args);
const { anchor } = args;
res = router.url(name, args, { query });
if (anchor) return `${res}#${anchor}`;
} catch (e) {
console.error(e.message);
+2 -2
View File
@@ -401,7 +401,7 @@ async function message(src: Db, report: Function) {
}
}
async function removeInvalidPid(src: Db, report: Function) {
async function removeInvalidPid(report: Function) {
const count = await dst.collection('document').find({ docType: 10 }).count();
const bulk = dst.collection('document').initializeUnorderedBulkOp();
await report({ progress: 1, message: `Remove pid: ${count}` });
@@ -550,7 +550,7 @@ export async function run({
await discussionNode(src, report);
await message(src, report);
await userfileUsage(src, report);
await removeInvalidPid(src, report);
await removeInvalidPid(report);
return true;
}