@@ -1476,8 +1476,7 @@ fn ddos_protection(ctx: *xdp_md) -> xdp_action {
14761476
14771477@tc("ingress")
14781478fn connection_tracker(ctx: *__sk_buff) -> i32 {
1479- var tcp_info = extract_tcp_info(ctx) // Reuse same helper
1480- if (tcp_info != null) {
1479+ if (var tcp_info = extract_tcp_info(ctx)) { // Reuse same helper
14811480 track_connection(tcp_info.src_port, tcp_info.dst_port)
14821481 }
14831482 return 0 // TC_ACT_OK
@@ -2357,9 +2356,9 @@ fn ebpf_pointer_usage(ctx: *xdp_md) -> xdp_action {
23572356 }
23582357 }
23592358
2360- // Dynptr-backed pointers (transparent to user)
2361- var log_buffer: *u8 = event_log. reserve(256) // Returns dynptr-backed pointer
2362- if (log_buffer != null ) {
2359+ // Dynptr-backed pointers (transparent to user) — `log_buffer` is the
2360+ // *u8 returned by reserve(), in scope only inside the truthy branch.
2361+ if (var log_buffer = event_log.reserve(256) ) {
23632362 // Regular pointer operations - compiler uses dynptr API internally
23642363 log_buffer[0] = EVENT_TYPE_PACKET
23652364 write_packet_summary(log_buffer + 1, packet_data, 255)
@@ -2428,15 +2427,14 @@ var flow_map : hash<FlowKey, FlowData>(1024)
24282427
24292428@helper
24302429fn map_pointer_operations(flow_key: FlowKey) {
2431- // Map lookup returns pointer to value
2432- var flow_data = flow_map[flow_key]
2433-
2434- if (flow_data != null) {
2430+ // Declaration-as-condition: a single map lookup; `flow_data` is the
2431+ // returned pointer, in scope only inside the truthy branch.
2432+ if (var flow_data = flow_map[flow_key]) {
24352433 // Direct modification through pointer
24362434 flow_data->packet_count += 1
24372435 flow_data->byte_count += packet_size
24382436 flow_data->last_seen = bpf_ktime_get_ns()
2439-
2437+
24402438 // Compiler tracks map value lifetime
24412439 // flow_data becomes invalid after certain map operations
24422440 }
@@ -2605,9 +2603,8 @@ fn egress_monitor(ctx: *__sk_buff) -> i32 {
26052603fn security_analyzer(ctx: LsmContext) -> i32 {
26062604 var flow_key = extract_flow_key_from_socket(ctx)?
26072605
2608- // Check global flow statistics
2609- if (global_flows[flow_key] != null) {
2610- var flow_stats = global_flows[flow_key]
2606+ // Check global flow statistics — single lookup via IfLet
2607+ if (var flow_stats = global_flows[flow_key]) {
26112608 if (flow_stats.is_suspicious()) {
26122609 security_events.submit(SecurityEvent {
26132610 event_type: EVENT_TYPE_SUSPICIOUS_CONNECTION,
@@ -2617,7 +2614,7 @@ fn security_analyzer(ctx: LsmContext) -> i32 {
26172614 return -EPERM // Block connection
26182615 }
26192616 }
2620-
2617+
26212618 return 0 // Allow connection
26222619}
26232620```
@@ -3736,9 +3733,8 @@ pin var global_config : array<ConfigKey, ConfigValue>(64)
37363733fn security_filter(ctx: LsmContext) -> i32 {
37373734 var flow_key = extract_flow_key_from_socket(ctx)
37383735
3739- // Check global flow statistics for threat detection
3740- if (global_flows[flow_key] != null) {
3741- var flow_stats = global_flows[flow_key]
3736+ // Check global flow statistics for threat detection — single lookup
3737+ if (var flow_stats = global_flows[flow_key]) {
37423738 if (flow_stats.is_suspicious()) {
37433739 global_events.submit(EVENT_THREAT_DETECTED { flow_key })
37443740 return -EPERM // Block connection
@@ -3779,8 +3775,7 @@ fn start_coordinator() -> i32 {
37793775
37803776fn process_events(coordinator: *SystemCoordinator) {
37813777 // Process events from all programs
3782- var event = coordinator->global_events.read()
3783- if (event != null) {
3778+ if (var event = coordinator->global_events.read()) {
37843779 if (event.event_type == EVENT_PACKET_PROCESSED) {
37853780 print("Processed packet for flow: ", event.flow_key)
37863781 } else if (event.event_type == EVENT_THREAT_DETECTED) {
@@ -3931,17 +3926,17 @@ var event_log : hash<u32, Event>(1024)
39313926
39323927@helper
39333928fn transparent_dynptr_usage(event_data: *u8, data_len: u32) {
3934- // User writes simple pointer code
3935- var log_entry: *u8 = event_log. reserve(data_len + 16) // Dynptr-backed pointer
3936- if (log_entry != null ) {
3929+ // User writes simple pointer code — IfLet binds the *u8 returned by
3930+ // reserve() only inside the truthy branch.
3931+ if (var log_entry = event_log.reserve(data_len + 16) ) {
39373932 // Regular pointer operations - compiler uses dynptr API internally
39383933 var header = log_entry as *EventHeader
39393934 header->timestamp = bpf_ktime_get_ns()
39403935 header->data_len = data_len
3941-
3936+
39423937 // Memory copy using pointer arithmetic
39433938 memory_copy(event_data, log_entry + 16, data_len)
3944-
3939+
39453940 event_log.submit(log_entry) // Compiler ensures proper cleanup
39463941 }
39473942}
@@ -4028,15 +4023,14 @@ var cache_map : hash<u32, DataCache>(1024)
40284023
40294024@helper
40304025fn map_lifetime_safety(key: u32) {
4031- var cache_entry = cache_map[key]
4032- if (cache_entry != null) {
4026+ if (var cache_entry = cache_map[key]) {
40334027 // Compiler tracks that cache_entry is valid here
40344028 cache_entry->access_count += 1
40354029 cache_entry->last_access = bpf_ktime_get_ns()
4036-
4030+
40374031 // Compiler warns/errors if cache_entry used after invalidating operations
40384032 cache_map[other_key] = other_value // Invalidates cache_entry
4039-
4033+
40404034 // ❌ Compiler error: "Use of potentially invalidated map value pointer"
40414035 // cache_entry->access_count += 1
40424036 }
@@ -4084,27 +4078,25 @@ fn kernel_side_processing(ctx: *xdp_md) -> xdp_action {
40844078 var packet_data = ctx->data()
40854079
40864080 // Shared memory through maps - safe across contexts
4087- var shared_buffer = shared_map[0]
4088- if (shared_buffer != null) {
4081+ if (var shared_buffer = shared_map[0]) {
40894082 shared_buffer->kernel_processed_count += 1
40904083 memory_copy(packet_data, shared_buffer->data, min(packet_len, 64))
40914084 }
4092-
4085+
40934086 return XDP_PASS
40944087}
40954088
40964089// Userspace cannot directly access kernel pointers
40974090fn userspace_processing() -> i32 {
40984091 // ❌ Cannot access kernel context pointers directly
40994092 // var packet_data = some_kernel_context.data() // Compilation error
4100-
4093+
41014094 // ✅ Access through shared maps
4102- var shared_buffer = shared_map[0]
4103- if (shared_buffer != null) {
4095+ if (var shared_buffer = shared_map[0]) {
41044096 shared_buffer->userspace_processed_count += 1
41054097 process_shared_data(shared_buffer->data)
41064098 }
4107-
4099+
41084100 return 0
41094101}
41104102```
0 commit comments