Devicetree
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Jinseob Kim <kimjinseob88@gmail.com>
Cc: Jonathan Cameron <jic23@kernel.org>,
	Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>,
	David Lechner <dlechner@baylibre.com>,
	Nuno Sa <nuno.sa@analog.com>, Andy Shevchenko <andy@kernel.org>,
	Jonathan Corbet <corbet@lwn.net>,
	Shuah Khan <skhan@linuxfoundation.org>,
	linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
	linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v7 4/5] iio: osf: add authenticated stream parser
Date: Tue, 7 Jul 2026 18:33:41 +0300	[thread overview]
Message-ID: <ak0cVeEUhNP1wTkQ@ashevche-desk.local> (raw)
In-Reply-To: <20260707014525.1015-5-kimjinseob88@gmail.com>

On Tue, Jul 07, 2026 at 10:45:24AM +0900, Jinseob Kim wrote:
> Add a UART byte-stream parser for Open Sensor Fusion frames.
> 
> The parser searches for the OSF0 wire magic, keeps partial frames
> buffered, checks header length and payload bounds, and passes complete
> candidate frames to the core decoder.
> 
> Rejected candidate frames drop only the current head byte before
> resynchronizing, so a corrupted unauthenticated payload length cannot
> make the parser skip later valid frames.

...

> +static bool osf_stream_frame_prefix_match(const u8 *buf, size_t len)
> +{
> +	for (size_t i = 0; i < len; i++) {
> +		if (buf[i] != (u8)(OSF_FRAME_MAGIC >> (i * 8)))
> +			return false;
> +	}
> +
> +	return true;

Why so complicated? le32_to_cpup() + just integer comparison should work, no?
Alternatively get_unaligned_le32() if the buffer is unaligned.

> +}

...

> +static size_t osf_stream_discard_to_magic(struct osf_stream *stream)
> +{
> +	size_t old_len = stream->len;
> +	size_t match_len;
> +
> +	for (size_t i = 0; i < stream->len; i++) {
> +		match_len = stream->len - i;
> +		if (match_len > OSF_STREAM_MAGIC_LEN)
> +			match_len = OSF_STREAM_MAGIC_LEN;

Seems like reinvention of min() from minmax.h.

> +		if (osf_stream_frame_prefix_match(stream->buf + i, match_len)) {
> +			if (i)
> +				osf_stream_discard(stream, i);
> +			return i;
> +		}
> +	}
> +
> +	stream->len = 0;
> +	return old_len;
> +}

...

> +void osf_stream_init(struct osf_stream *stream, struct osf_device *osf)
> +{
> +	if (!stream)
> +		return;
> +
> +	stream->osf = osf;
> +	stream->len = 0;
> +	memset(&stream->stats, 0, sizeof(stream->stats));
> +}
> +
> +void osf_stream_reset(struct osf_stream *stream)
> +{
> +	if (stream) {

I believe I have commented on this already. Please, go and double check all comments.

> +		stream->len = 0;
> +		memset(&stream->stats, 0, sizeof(stream->stats));
> +	}

My comment was to use the same pattern as in _init() above.

> +}

...

> +int osf_stream_receive_bytes(struct osf_stream *stream, const u8 *buf,
> +			     size_t len)

Not sure, but likely also commented on this, id est to wrap on logical split:

int osf_stream_receive_bytes(struct osf_stream *stream,
			     const u8 *buf, size_t len)

OR simply do in a single line (it's only 82 characters with increased
readability):

int osf_stream_receive_bytes(struct osf_stream *stream, const u8 *buf, size_t len)

> +{
> +	size_t copy_len;
> +	size_t space;
> +	int first_err = 0;
> +	int ret;
> +
> +	if (!stream || !stream->osf || (!buf && len))
> +		return -EINVAL;
> +
> +	if (!len) {
> +		ret = osf_stream_process(stream);
> +		if (ret && !first_err)
> +			first_err = ret;
> +		return first_err;

Why all this dances? first_err is 0, we all know this.

> +	}
> +
> +	while (len) {
> +		space = OSF_STREAM_MAX_FRAME_LEN - stream->len;
> +		if (!space) {
> +			stream->stats.dropped_bytes++;
> +			osf_stream_discard(stream, 1);
> +			if (!first_err)
> +				first_err = -EMSGSIZE;
> +			continue;
> +		}
> +
> +		copy_len = len < space ? len : space;
> +		memcpy(stream->buf + stream->len, buf, copy_len);
> +		stream->len += copy_len;
> +		buf += copy_len;
> +		len -= copy_len;
> +
> +		ret = osf_stream_process(stream);
> +		if (ret && !first_err)
> +			first_err = ret;

This error checking and handling is unusual. It requires a good comment
explaining what's going on.

> +	}
> +
> +	return first_err;
> +}

-- 
With Best Regards,
Andy Shevchenko



  reply	other threads:[~2026-07-07 15:33 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-07  1:45 [PATCH v7 0/5] iio: add Open Sensor Fusion IIO driver Jinseob Kim
2026-07-07  1:45 ` [PATCH v7 1/5] dt-bindings: iio: add Open Sensor Fusion device Jinseob Kim
2026-07-07  1:50   ` sashiko-bot
2026-07-07 16:01   ` Conor Dooley
2026-07-07  1:45 ` [PATCH v7 2/5] Documentation: iio: add Open Sensor Fusion driver overview Jinseob Kim
2026-07-07  4:07   ` Randy Dunlap
2026-07-07  1:45 ` [PATCH v7 3/5] iio: osf: add protocol decoding Jinseob Kim
2026-07-07  1:51   ` sashiko-bot
2026-07-07  1:45 ` [PATCH v7 4/5] iio: osf: add authenticated stream parser Jinseob Kim
2026-07-07 15:33   ` Andy Shevchenko [this message]
2026-07-07  1:45 ` [PATCH v7 5/5] iio: osf: add UART IIO driver Jinseob Kim
2026-07-07  8:47   ` Uwe Kleine-König
2026-07-07  8:48   ` Uwe Kleine-König
2026-07-07  9:00   ` Joshua Crofts

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ak0cVeEUhNP1wTkQ@ashevche-desk.local \
    --to=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=corbet@lwn.net \
    --cc=devicetree@vger.kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=kimjinseob88@gmail.com \
    --cc=krzk+dt@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=robh@kernel.org \
    --cc=skhan@linuxfoundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox