Search code examples
bpfebpfbcc-bpf

Trying to count the correct number of characters in a string in BPF


I am trying to count a passed in pointer string in BPF but I am left with this really long error that I am unable to understand. I am basically trying to recreate strlen within BPF to count the size of my passed in string*. The interesting area from the error can be seen here:

R1 min value is outside of the allowed memory range processed 2353 insns (limit 1000000) max_states_per_insn 1 total_states 242 peak_states 242 mark_read 2

Any idea on how to resolve something else?

Here is the code I have written:

int stringLength(char* txt)
{
    int i=0,count=0;
    
    while(txt[i++]!='\0'){
        count+=1;
    }
    
    return count;
}

It is a helper function that I am calling within my main BPF function.

Pastebin error


Solution

  • The error is about an out-of-range access. Your condition in while(txt[i++]!='\0') assumes that the string does end with a NULL character, but in practice you (or more specifically, the verifier) have no guarantee about that. Try expanding the condition to make sure you do not reach beyond the length of your string.