Skip to content

g-udisks-volume: fix incorrect g_realloc and memcpy usage#124

Open
mtasaka wants to merge 1 commit into
lxde:masterfrom
mtasaka:fix-g_realloc_memcpy-usage
Open

g-udisks-volume: fix incorrect g_realloc and memcpy usage#124
mtasaka wants to merge 1 commit into
lxde:masterfrom
mtasaka:fix-g_realloc_memcpy-usage

Conversation

@mtasaka

@mtasaka mtasaka commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

gcc17 -fanalyzer warns:
src/udisks/g-udisks-volume.c:302:45: warning: allocated buffer size is not a multiple of the pointee's size [CWE-131] [-Wanalyzer-allocation-size]

Actually:

  1. The second argument of g_realloc: "+2" is apparently wrong: it should be len + 2, also should be multiplied by the size of element.
  2. The direction (in other words, the first and second arguments) of memcpy is swapped. The latter line seems to be trying to prepend OUT_mount_path to mount_paths, so mount_paths[0] should be moved to mount_paths[1]
  3. data->vol->dev->mount_paths + sizeof(char*) points to the sizeof(char*)-th element of mount_paths (starting at 0), which is apparently not intended: which should be 1th element (starting at 0).
  4. Using memcpy for overwrapping region is incorrect. That should be memmove.
  5. len value, which is the result of g_strv_length, does not count the last NULL sentinel. So to count the size of memmove, the last NULL sentinel must be considered.

This change fixes the above issues.

gcc17 -fanalyzer warns:
src/udisks/g-udisks-volume.c:302:45: warning: allocated buffer size is not a multiple of the pointee's size [CWE-131] [-Wanalyzer-allocation-size]

Actually:
1. The second argument of g_realloc: "+2" is apparently wrong: it should be `len + 2`, also should be multiplied by the size of element.
2. The direction (in other words, the first and second arguments) of memcpy is swapped. The latter line seems to be trying to prepend `OUT_mount_path` to mount_paths, so mount_paths[0] should be moved to mount_paths[1]
3. `data->vol->dev->mount_paths + sizeof(char*)` points to the `sizeof(char*)`-th element of `mount_paths` (starting at 0), which is apparently not intended: which should be 1th element (starting at 0).
4. Using `memcpy` for overwrapping region is incorrect. That should be `memmove`.
5. `len` value, which is the result of `g_strv_length`, does not count the last NULL sentinel. So to count the size of `memmove`, the last NULL sentinel must be considered.

This change fixes the above issues.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant