Re: [PATCH V6 3/8] libgpiod: Add rust wrapper crate

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Sep 28, 2022 at 1:10 PM Viresh Kumar <viresh.kumar@xxxxxxxxxx> wrote:
>
> Here is what happens at each call for easier understanding:
>
> On 27-09-22, 17:25, Bartosz Golaszewski wrote:
> > So what happens if I do this:
> >
> > let buf = edge::event::Buffer::new(10)?;
>
> buf1 = gpiod_edge_event_buffer_new()
>
> > let request = chip.request_lines(&rconfig, &lconfig)?;
>
> gpiod_chip_request_lines()
>
> > let count = request.read_edge_events(&buffer)?;
>
> gpiod_line_request_read_edge_event(buf1)
>
> > let event = buffer.event(0);
>
> event1 = gpiod_edge_event_buffer_get_event(buf1, 0)
>
> > let count = request.read_edge_events(&buffer)?;
>
> gpiod_line_request_read_edge_event(buf1)
>
> > println!("line: {}", event.line_offset());
>
> gpiod_edge_event_get_line_offset(event1)
>
>
> We will allocate a single buffer (buf1) and the event (event1) will be a
> reference onto its first event entry in the buffer. It will print offset value
> from the second read_edge_events() here instead of first, as it was just a
> reference to the first event. And for such a use case, the user should do
>
> event = buffer.event(0).event_clone();
>
> --
> viresh

Ok, so this is not correct. The doc for
gpiod_edge_event_buffer_get_event() says:

 * @return Pointer to an event stored in the buffer. The lifetime of the
 *   event is tied to the buffer object. Users must not free the event
 *   returned by this function.

We make no guarantees that the address returned by
gpiod_edge_event_buffer_get_event() will still be valid after a call
to gpiod_line_request_read_edge_event(). In fact the doc for the
latter says:

 * @note Any exising events in the buffer are overwritten.  This is not an
 *       append operation.

So we're being clear that after a call to
gpiod_line_request_read_edge_event(), all previously returned edge
event pointers are invalid unless the objects to which they point were
explicitly copied.

Current implementation does indeed work like what you describe but
that behavior is not "contractually" guaranteed and can change even
between minor releases.

In C++ if you do:

const edge_event& ev = buffer.get_event(0);
request.read_edge_event(buffer);
std::cout << ev << std::endl;

You risk a segfault.

My understanding is that Rust should not allow a crash in this
situation by design.

Bart



[Index of Archives]     [Linux SPI]     [Linux Kernel]     [Linux ARM (vger)]     [Linux ARM MSM]     [Linux Omap]     [Linux Arm]     [Linux Tegra]     [Fedora ARM]     [Linux for Samsung SOC]     [eCos]     [Linux Fastboot]     [Gcc Help]     [Git]     [DCCP]     [IETF Announce]     [Security]     [Linux MIPS]     [Yosemite Campsites]

  Powered by Linux