device: fix read/write_memory python bindings#1056
Conversation
654250f to
ff42f39
Compare
There was a problem hiding this comment.
Pull request overview
This pull request fixes Python bindings for device memory read/write operations by introducing GObject Introspection (GI)-friendly wrapper functions. The original arv_device_read_memory and arv_device_write_memory functions require pre-allocated buffers, which is not idiomatic for language bindings. The new arv_device_read_bytes and arv_device_write_bytes functions use GByteArray for automatic memory management, making them suitable for Python and other high-level language bindings.
Changes:
- Added
arv_device_read_bytesandarv_device_write_byteswrapper functions with(rename-to)annotations for GI bindings - Enhanced fake device implementation with error handling and debug tracing for memory operations
- Added Python test suite to verify the new bindings work correctly
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| src/arvdevice.h | Declares new GI-friendly read_bytes and write_bytes functions |
| src/arvdevice.c | Implements wrapper functions and updates documentation annotations for GI compatibility |
| src/arvfakedevice.c | Adds error handling and debug tracing to fake device memory operations |
| src/arvdebugprivate.h | Adds arv_trace_device macro for device-level trace logging |
| src/arvcamera.c | Adds (get-property device) annotation to arv_camera_get_device |
| tests/meson.build | Registers new Python test file |
| tests/device.py | Tests memory read/write operations through Python bindings |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/arvfakedevice.c
Outdated
|
|
||
| if (!success) | ||
| g_set_error (error, ARV_DEVICE_ERROR, ARV_DEVICE_ERROR_INVALID_PARAMETER, | ||
| "Invalid write at 0x%lx, size %d", address, size); |
There was a problem hiding this comment.
Format specifier issue: Using %lx for a guint64 type is not portable. On 32-bit platforms, long is 32 bits while guint64 is always 64 bits. Use "%" G_GINT64_MODIFIER "x" for hexadecimal format to ensure correct formatting across all platforms.
| buffer = g_new (guint8, size); | ||
|
|
||
| success = ARV_DEVICE_GET_CLASS (device)->read_memory (device, address, size, buffer, error); |
There was a problem hiding this comment.
Potential overflow: The size parameter is size_t (which can be 64-bit) but it's cast to guint32 when passed to read_memory at line 190. If size is larger than GUINT32_MAX, it will be silently truncated, leading to reading fewer bytes than requested. Consider adding a check: if (size > G_MAXUINT32) { g_set_error(...); return NULL; }
| buffer = g_new (guint8, size); | |
| success = ARV_DEVICE_GET_CLASS (device)->read_memory (device, address, size, buffer, error); | |
| if (size > G_MAXUINT32) { | |
| if (error != NULL) { | |
| g_set_error (error, | |
| arv_device_error_quark (), | |
| 0, | |
| "Requested read size (%" G_GSIZE_FORMAT ") exceeds maximum supported size (%u)", | |
| size, | |
| G_MAXUINT32); | |
| } | |
| return NULL; | |
| } | |
| buffer = g_new (guint8, size); | |
| success = ARV_DEVICE_GET_CLASS (device)->read_memory (device, address, (guint32) size, buffer, error); |
src/arvdevice.c
Outdated
| * | ||
| * Reads @size bytes from the device memory. | ||
| * | ||
| * Return value: (transfer full): a new [class@Glib.Bytes), %NULL on error |
There was a problem hiding this comment.
Documentation error: the return value says "a new [class@Glib.Bytes)" but the function actually returns a GByteArray. The documentation should say "[type@GLib.ByteArray]" instead. Also, there's a mismatched closing parenthesis - it should be a closing bracket.]
| * Return value: (transfer full): a new [class@Glib.Bytes), %NULL on error | |
| * Return value: (transfer full): a new [type@GLib.ByteArray], %NULL on error |
src/arvfakedevice.c
Outdated
| return arv_fake_camera_read_memory (priv->camera, address, size, buffer); | ||
| success = arv_fake_camera_read_memory (priv->camera, address, size, buffer); | ||
|
|
||
| arv_trace_device ("Fake device read at address = %ld, size = %d", address, size); |
There was a problem hiding this comment.
Format specifier issue: Using %ld for a guint64 type is not portable. On 32-bit platforms, long is 32 bits while guint64 is always 64 bits. Use G_GUINT64_FORMAT instead to ensure correct formatting across all platforms.
src/arvfakedevice.c
Outdated
|
|
||
| if (!success) | ||
| g_set_error (error, ARV_DEVICE_ERROR, ARV_DEVICE_ERROR_INVALID_PARAMETER, | ||
| "Invalid read at 0x%lx, size %d", address, size); |
There was a problem hiding this comment.
Format specifier issue: Using %lx for a guint64 type is not portable. On 32-bit platforms, long is 32 bits while guint64 is always 64 bits. Use "%" G_GINT64_MODIFIER "x" for hexadecimal format to ensure correct formatting across all platforms.
src/arvfakedevice.c
Outdated
| ArvFakeDevicePrivate *priv = arv_fake_device_get_instance_private (ARV_FAKE_DEVICE (device)); | ||
| gboolean success; | ||
|
|
||
| arv_trace_device ("Fake device write at address = %ld, size = %d", address, size); |
There was a problem hiding this comment.
Format specifier issue: Using %ld for a guint64 type is not portable. On 32-bit platforms, long is 32 bits while guint64 is always 64 bits. Use G_GUINT64_FORMAT instead to ensure correct formatting across all platforms.
ff42f39 to
63b4ab8
Compare
No description provided.