Problem with a code of FIR Lowpass filter
The problem is, as you've already pointed out, the choice of the summation limits. Obviously, the array index shouldn't become negative. Look at the definition of convolution:

$$y(n)=\sum_{k=-\infty}^{\infty}h(k)x(n-k)\tag{1}$$

Now assume, as in your case, that the filter length (i.e. the number of coefficients) equals $M+1$, and that the input signal is a finite length sequence in the range $0\le n&lt;N$. Then you have two conditions on the summation index $k$:

$$0\le k \le M\quad\text{and}\quad 0\le n-k \le N-1$$

With these conditions the sum in (1) becomes

$$y(n)=\sum_{k=\max\{0,n-N+1\}}^{\min\{M,n\}}h(k)x(n-k)\tag{2}$$

It is the sum in (2) that you need to implement.

Also note that if you want to implement convolution, then the output signal will be longer than the input signal, so you would need to define two different variables len. But if you only want to compute the output signal as long as there is a non-zero input signal present, then your implementation is fine.

Another improvement would be to have a routine that just takes a scalar input value (the current input sample) and computes the corresponding output sample. In this way you don't need to know the length of the input signal and you don't need to store the complete input signal in an array because you just process each incoming sample directly. Of course you need an internal buffer storing the last $M$ input values. Your version is appropriate for offline-processing of not too long signals.