Skip to content

Commit

Permalink
Explicitly use NULL macro in pointer value check (iovisor#2965)
Browse files Browse the repository at this point in the history
 Explicitly use NULL macro in pointer value check
 also updated the tutorial
  • Loading branch information
djosephsen committed Jun 13, 2020
1 parent 6a96194 commit 5966549
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/tutorial_bcc_python_developer.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ int do_trace(struct pt_regs *ctx) {
// attempt to read stored timestamp
tsp = last.lookup(&key);
if (tsp != 0) {
if (tsp != NULL) {
delta = bpf_ktime_get_ns() - *tsp;
if (delta < 1000000000) {
// output if time is less than 1 second
Expand Down Expand Up @@ -163,6 +163,7 @@ Things to learn:
1. ```BPF_HASH(last)```: Creates a BPF map object that is a hash (associative array), called "last". We didn't specify any further arguments, so it defaults to key and value types of u64.
1. ```key = 0```: We'll only store one key/value pair in this hash, where the key is hardwired to zero.
1. ```last.lookup(&key)```: Lookup the key in the hash, and return a pointer to its value if it exists, else NULL. We pass the key in as an address to a pointer.
1. ```if (tsp != NULL) {```: The verifier requires that pointer values derived from a map lookup must be checked for a null value before they can be dereferenced and used.
1. ```last.delete(&key)```: Delete the key from the hash. This is currently required because of [a kernel bug in `.update()`](https://git.kernel.org/cgit/linux/kernel/git/davem/net.git/commit/?id=a6ed3ea65d9868fdf9eff84e6fe4f666b8d14b02).
1. ```last.update(&key, &ts)```: Associate the value in the 2nd argument to the key, overwriting any previous value. This records the timestamp.

Expand Down
2 changes: 1 addition & 1 deletion examples/tracing/sync_timing.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// attempt to read stored timestamp
tsp = last.lookup(&key);
if (tsp != 0) {
if (tsp != NULL) {
delta = bpf_ktime_get_ns() - *tsp;
if (delta < 1000000000) {
// output if time is less than 1 second
Expand Down

0 comments on commit 5966549

Please sign in to comment.