Skip to content

Commit df21835

Browse files
dave-bartolomeoDave Bartolomeo
authored andcommitted
C++/C#: Refactor some integer constant code
Make `bitsToBytesAndBits` omit the leftover bits if zero.
1 parent 51ff262 commit df21835

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

cpp/ql/src/semmle/code/cpp/ir/internal/IntegerConstant.qll

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,36 @@ predicate isGT(IntValue a, IntValue b) { hasValue(a) and hasValue(b) and a > b }
192192
*/
193193
bindingset[a, b]
194194
predicate isGE(IntValue a, IntValue b) { hasValue(a) and hasValue(b) and a >= b }
195+
196+
/**
197+
* Converts the bit count in `bits` to a byte count and a bit count in the form
198+
* "bytes:bits". If `bits` represents an integer number of bytes, the ":bits" section is omitted.
199+
* If `bits` does not have a known value, the result is "?".
200+
*/
201+
bindingset[bits]
202+
string bitsToBytesAndBits(IntValue bits) {
203+
exists(int bytes, int leftoverBits |
204+
hasValue(bits) and
205+
bytes = bits / 8 and
206+
leftoverBits = bits % 8 and
207+
if leftoverBits = 0 then
208+
result = bytes.toString()
209+
else
210+
result = bytes + ":" + leftoverBits
211+
) or
212+
not hasValue(bits) and result = "?"
213+
}
214+
215+
/**
216+
* Gets a printable string for a bit offset with possibly unknown value.
217+
*/
218+
bindingset[bitOffset]
219+
string getBitOffsetString(IntValue bitOffset) {
220+
if hasValue(bitOffset) then
221+
if bitOffset >= 0 then
222+
result = "+" + bitsToBytesAndBits(bitOffset)
223+
else
224+
result = "-" + bitsToBytesAndBits(neg(bitOffset))
225+
else
226+
result = "+?"
227+
}

cpp/ql/src/semmle/code/cpp/ir/internal/IntegerInterval.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ Overlap getOverlap(IntValue defStart, IntValue defEnd, IntValue useStart, IntVal
3030
bindingset[start, end]
3131
string getIntervalString(IntValue start, IntValue end) {
3232
// We represent an interval has half-open, so print it as "[start..end)".
33-
result = "[" + intValueToString(start) + ".." + intValueToString(end) + ")"
33+
result = "[" + bitsToBytesAndBits(start) + ".." + bitsToBytesAndBits(end) + ")"
3434
}

csharp/ql/src/semmle/code/csharp/ir/internal/IntegerConstant.qll

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,36 @@ predicate isGT(IntValue a, IntValue b) { hasValue(a) and hasValue(b) and a > b }
192192
*/
193193
bindingset[a, b]
194194
predicate isGE(IntValue a, IntValue b) { hasValue(a) and hasValue(b) and a >= b }
195+
196+
/**
197+
* Converts the bit count in `bits` to a byte count and a bit count in the form
198+
* "bytes:bits". If `bits` represents an integer number of bytes, the ":bits" section is omitted.
199+
* If `bits` does not have a known value, the result is "?".
200+
*/
201+
bindingset[bits]
202+
string bitsToBytesAndBits(IntValue bits) {
203+
exists(int bytes, int leftoverBits |
204+
hasValue(bits) and
205+
bytes = bits / 8 and
206+
leftoverBits = bits % 8 and
207+
if leftoverBits = 0 then
208+
result = bytes.toString()
209+
else
210+
result = bytes + ":" + leftoverBits
211+
) or
212+
not hasValue(bits) and result = "?"
213+
}
214+
215+
/**
216+
* Gets a printable string for a bit offset with possibly unknown value.
217+
*/
218+
bindingset[bitOffset]
219+
string getBitOffsetString(IntValue bitOffset) {
220+
if hasValue(bitOffset) then
221+
if bitOffset >= 0 then
222+
result = "+" + bitsToBytesAndBits(bitOffset)
223+
else
224+
result = "-" + bitsToBytesAndBits(neg(bitOffset))
225+
else
226+
result = "+?"
227+
}

csharp/ql/src/semmle/code/csharp/ir/internal/IntegerInterval.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ Overlap getOverlap(IntValue defStart, IntValue defEnd, IntValue useStart, IntVal
3030
bindingset[start, end]
3131
string getIntervalString(IntValue start, IntValue end) {
3232
// We represent an interval has half-open, so print it as "[start..end)".
33-
result = "[" + intValueToString(start) + ".." + intValueToString(end) + ")"
33+
result = "[" + bitsToBytesAndBits(start) + ".." + bitsToBytesAndBits(end) + ")"
3434
}

0 commit comments

Comments
 (0)