codeQL学习记录(一)

CodeQL学习记录(一)

记录官方教学语法题的答案

1 Find the thief

根据下述问题描述找出小偷
难点在于9、10、11的聚合语句

QuestionAnswer
1Is the thief taller than 150 cm?yes
2Does the thief have blond hair?no
3Is the thief bald?no
4Is the thief younger than 30?no
5Does the thief live east of the castle?yes
6Does the thief have black or brown hair?yes
7Is the thief taller than 180cm and shorter than 190cm?no
8Is the thief the oldest person in the village?no
9Is the thief the tallest person in the village?no
10Is the thief shorter than the average villager?yes
11Is the thief the oldest person in the eastern part of the village?yes
import java
import tutorial

from Person t
where t.getHeight() > 150 and
	not t.getHairColor() = "blond" and
	exists(string c | t.getHairColor() =c) and
	t.getAge() > 30 and
	t.getLocation() = "east" and
	(t.getHairColor() = "black" or t.getHairColor() = "brown") and
	not (t.getHeight() > 180 and t.getHeight() < 190) and
	exists(Person p | p.getAge() > t.getAge()) and
	not t = max(Person p||p order by p.getHeight()) and
	t.getHeight() < avg(Person p||p.getHeight()) and
	t = max(Person p|p.getLocation()="east"|p order by p.getAge())
select t

2.catch the fire starter

筛选的条件不太复杂:肇事者是南方人,光头,而且能够去北方旅行(不是十岁以下的儿童)

官方教程通过这个例子展现CodeQL中predicate谓词(有点像函数)和定义class的用法

类的用法和通常的面向对象编程有相当大的区别

import java
import tutorial
predicate isSouth(Person p){
  p.getLocation() = "south"
}
predicate isBald(Person p){
  not exists (string c|p.getHairColor() = c)
}

class Southerner extends Person{
  Southerner(){isSouth(this)}
}
class Child extends Person{
	Child(){this.getAge()<10}
  	override predicate isAllowedIn(string region){ region = this.getLocation()}
}

from Southerner s
where s.isAllowedIn("north") and isBald(s)
select s

3.Crown the rightful heir

这一章节的内容也是难点:谓词predicate、递归、传递闭包

import tutorial

Person relativeOf(Person p) { parentOf*(result) = parentOf*(p) }
predicate hasCriminalRecord(Person p) {
  p = "Hester" or
  p = "Hugh" or
  p = "Charlie"
}
from Person p
where
  not p.isDeceased() and
  p = relativeOf("King Basil") and
  not hasCriminalRecord(p)
select p

课后习题

https://www.jianshu.com/p/f141c6c85c1d%20

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值