On Sat, Nov 14, 2026 at 10:20:35PM -0500, Brad Smith wrote:
> On Sat, Nov 14, 2026 at 07:45:06PM -0500, Brad Smith wrote:
> > Fix the XShm 1.2 support in the vo_x11 and vo_xv output drivers.
> 
> An updated diff.

ok matthieu@

> 
> Fix the XShm 1.2 support in the vo_x11 and vo_xv output drivers.
> 
> - don't close the fd after xcb_shm_attach_fd(), xcb owns it and
>   closes it after sending it; closing it again could close an
>   unrelated fd
> - release the mapping with munmap() instead of shmdt(), which
>   fails on it and leaked every buffer
> - check ftruncate() and do it before mmap(), drop __MAP_NOFAULT
> - unlink the shm name right after creating it
> - use xcb_shm_attach_fd_checked() and check for errors
> - only use SHM with MIT-SHM >= 1.2 over a unix socket
> - fix an out of bounds read when copying the shm name template
> - fix the xcb version requirements in meson.build
> 
> 
> Index: Makefile
> ===================================================================
> RCS file: /cvs/ports/multimedia/mpv/Makefile,v
> retrieving revision 1.111
> diff -u -p -u -p -r1.111 Makefile
> --- Makefile  11 Sep 2026 16:35:40 -0000      1.111
> +++ Makefile  15 Nov 2026 02:44:19 -0000
> @@ -5,7 +5,7 @@ USE_NOBTCFI =         Yes
>  GH_ACCOUNT =         mpv-player
>  GH_PROJECT =         mpv
>  GH_TAGNAME =         v0.41.0
> -REVISION =           1
> +REVISION =           2
>  
>  SHARED_LIBS +=               mpv             2.1
>  
> Index: patches/patch-meson_build
> ===================================================================
> RCS file: /cvs/ports/multimedia/mpv/patches/patch-meson_build,v
> retrieving revision 1.13
> diff -u -p -u -p -r1.13 patch-meson_build
> --- patches/patch-meson_build 5 Mar 2026 12:10:21 -0000       1.13
> +++ patches/patch-meson_build 15 Nov 2026 02:44:19 -0000
> @@ -32,9 +32,9 @@ Index: meson.build
>                dependency('xpresent', version: '>= 1.0.0', required: x11_opt),
>  -             dependency('xrandr', version: '>= 1.4.0', required: x11_opt)],
>  +             dependency('xrandr', version: '>= 1.4.0', required: x11_opt),
> -+             dependency('x11-xcb', version: '>= 1.8.9', required: x11_opt),
> -+             dependency('xcb-shm', version: '>= 1.16', required: x11_opt),
> -+             dependency('xcb', version: '>= 1.16', required: x11_opt),]
> ++             dependency('x11-xcb', required: x11_opt),
> ++             dependency('xcb-shm', version: '>= 1.10', required: x11_opt),
> ++             dependency('xcb', version: '>= 1.10', required: x11_opt)]
>   }
>   x11_deps = true
>   foreach dep: x11['deps']
> Index: patches/patch-video_out_vo_x11_c
> ===================================================================
> RCS file: /cvs/ports/multimedia/mpv/patches/patch-video_out_vo_x11_c,v
> retrieving revision 1.1
> diff -u -p -u -p -r1.1 patch-video_out_vo_x11_c
> --- patches/patch-video_out_vo_x11_c  2 Mar 2025 10:17:37 -0000       1.1
> +++ patches/patch-video_out_vo_x11_c  15 Nov 2026 02:44:19 -0000
> @@ -1,4 +1,10 @@
> -Implement XShm 1.2
> +Use MIT-SHM 1.2 (fd passing) with POSIX shm instead of SysV shm.
> +
> +xcb owns the fd passed to xcb_shm_attach_fd() and closes it after
> +sending it, so it must not be closed again later.  The mapping comes
> +from mmap(2), so release it with munmap(2) rather than shmdt(2).
> +Only use SHM if the server supports MIT-SHM 1.2 and the connection
> +is a unix domain socket.
>  
>  Index: video/out/vo_x11.c
>  --- video/out/vo_x11.c.orig
> @@ -11,45 +17,113 @@ Index: video/out/vo_x11.c
>   #include <sys/types.h>
>   
>   #include <libswscale/swscale.h>
> -@@ -38,7 +39,10 @@
> +@@ -36,9 +37,11 @@
> + #include "present_sync.h"
> + #include "x11_common.h"
>   
> - #include <sys/ipc.h>
> - #include <sys/shm.h>
> +-#include <sys/ipc.h>
> +-#include <sys/shm.h>
>  +#include <sys/mman.h>
> ++#include <sys/socket.h>
>   #include <X11/extensions/XShm.h>
>  +#include <X11/Xlib-xcb.h>
>  +#include <xcb/shm.h>
>   
>   #include "sub/osd.h"
>   #include "sub/draw_bmp.h"
> -@@ -76,14 +80,26 @@ struct priv {
> +@@ -76,15 +79,90 @@ struct priv {
>   
>       int Shmem_Flag;
>       XShmSegmentInfo Shminfo[2];
> -+    char shmname[128];
> ++    size_t shmlen[2];
>       int Shm_Warned_Slow;
>   };
>   
>   static bool resize(struct vo *vo);
>   
> -+static Bool XShmAttachFd(Display *dpy, XShmSegmentInfo *shminfo)
> ++/*
> ++ * MIT-SHM 1.2 (fd passing) is needed, and fd passing only works over
> ++ * a unix domain socket.
> ++ */
> ++static bool shm_fd_usable(Display *dpy)
> ++{
> ++    struct sockaddr_storage ss;
> ++    socklen_t sslen = sizeof(ss);
> ++    int major, minor;
> ++    Bool pixmaps;
> ++
> ++    if (!XShmQueryVersion(dpy, &major, &minor, &pixmaps))
> ++        return false;
> ++    if (major < 1 || (major == 1 && minor < 2))
> ++        return false;
> ++    if (getsockname(ConnectionNumber(dpy), (struct sockaddr *)&ss,
> ++                    &sslen) == -1)
> ++        return false;
> ++    return ss.ss_family == AF_UNIX;
> ++}
> ++
> ++/*
> ++ * Create a POSIX shm segment of len bytes, map it and pass it to the
> ++ * X server.  xcb owns the fd once it has been handed to
> ++ * xcb_shm_attach_fd_checked() and closes it after sending it, so it
> ++ * must not be closed here after that point.
> ++ */
> ++static bool shm_fd_attach(Display *dpy, XShmSegmentInfo *si, size_t len)
>  +{
> -+    xcb_connection_t *xcb_conn = XGetXCBConnection(dpy);
> ++    char name[] = "/tmp/mpv-x11-XXXXXXXXXX";
> ++    xcb_connection_t *c = XGetXCBConnection(dpy);
> ++    xcb_generic_error_t *err;
> ++    void *addr;
> ++    int fd;
> ++
> ++    *si = (XShmSegmentInfo){0};
> ++    si->shmid = -1;
> ++
> ++    fd = shm_mkstemp(name);
> ++    if (fd == -1)
> ++        return false;
> ++    shm_unlink(name);
> ++    if (ftruncate(fd, len) == -1) {
> ++        close(fd);
> ++        return false;
> ++    }
> ++    addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
> ++    if (addr == MAP_FAILED) {
> ++        close(fd);
> ++        return false;
> ++    }
> ++
> ++    si->shmseg = xcb_generate_id(c);
> ++    si->readOnly = False;
> ++    err = xcb_request_check(c, xcb_shm_attach_fd_checked(c, si->shmseg,
> ++                                                         fd, si->readOnly));
> ++    if (err) {
> ++        free(err);
> ++        munmap(addr, len);
> ++        *si = (XShmSegmentInfo){0};
> ++        return false;
> ++    }
> ++    si->shmaddr = addr;
> ++    return true;
> ++}
>  +
> -+    shminfo->shmseg = xcb_generate_id(xcb_conn);
> -+    xcb_shm_attach_fd(xcb_conn, shminfo->shmseg,
> -+                   shminfo->shmid, shminfo->readOnly);
> -+    return 1;
> ++static void shm_fd_detach(Display *dpy, XShmSegmentInfo *si, size_t len)
> ++{
> ++    XShmDetach(dpy, si);
> ++    munmap(si->shmaddr, len);
> ++    *si = (XShmSegmentInfo){0};
>  +}
>  +
>   static bool getMyXImage(struct priv *p, int foo)
>   {
>       struct vo *vo = p->vo;
> +-    if (vo->x11->display_is_local && XShmQueryExtension(vo->x11->display)) {
>  +    size_t len;
> -     if (vo->x11->display_is_local && XShmQueryExtension(vo->x11->display)) {
> ++    if (vo->x11->display_is_local && shm_fd_usable(vo->x11->display)) {
>           p->Shmem_Flag = 1;
>           vo->x11->ShmCompletionEvent = XShmGetEventBase(vo->x11->display)
> -@@ -102,16 +118,20 @@ static bool getMyXImage(struct priv *p, int foo)
> +                                     + ShmCompletion;
> +@@ -102,29 +180,18 @@ static bool getMyXImage(struct priv *p, int foo)
>               MP_WARN(vo, "Shared memory error,disabling ( Ximage error )\n");
>               goto shmemerror;
>           }
> @@ -57,43 +131,44 @@ Index: video/out/vo_x11.c
>  -                                       p->myximage[foo]->bytes_per_line *
>  -                                       p->myximage[foo]->height,
>  -                                       IPC_CREAT | 0777);
> -+    len = p->myximage[foo]->bytes_per_line * p->myximage[foo]->height;
> -+    memcpy(p->shmname, "/tmp/mpv-x11-XXXXXXXXXX",
> -+           sizeof(p->shmname));
> -+    p->Shminfo[foo].shmid = shm_mkstemp(p->shmname);
> -         if (p->Shminfo[foo].shmid < 0) {
> +-        if (p->Shminfo[foo].shmid < 0) {
> ++        len = (size_t)p->myximage[foo]->bytes_per_line *
> ++              p->myximage[foo]->height;
> ++        if (!shm_fd_attach(vo->x11->display, &p->Shminfo[foo], len)) {
>               XDestroyImage(p->myximage[foo]);
> -             MP_WARN(vo, "Shared memory error,disabling ( seg id error )\n");
> +-            MP_WARN(vo, "Shared memory error,disabling ( seg id error )\n");
> ++            p->myximage[foo] = NULL;
> ++            MP_WARN(vo, "Shared memory error,disabling ( attach error )\n");
>               goto shmemerror;
>           }
>  -        p->Shminfo[foo].shmaddr = (char *) shmat(p->Shminfo[foo].shmid, 0, 
> 0);
> -+        p->Shminfo[foo].shmaddr = mmap(NULL, len,
> -+                                   PROT_READ | PROT_WRITE,
> -+                                   MAP_SHARED | __MAP_NOFAULT,
> -+                                       p->Shminfo[foo].shmid, 0);
> -+        ftruncate(p->Shminfo[foo].shmid, len);
> - 
> -         if (p->Shminfo[foo].shmaddr == ((char *) -1)) {
> -             XDestroyImage(p->myximage[foo]);
> -@@ -120,11 +140,11 @@ static bool getMyXImage(struct priv *p, int foo)
> -         }
> +-
> +-        if (p->Shminfo[foo].shmaddr == ((char *) -1)) {
> +-            XDestroyImage(p->myximage[foo]);
> +-            MP_WARN(vo, "Shared memory error,disabling ( address error 
> )\n");
> +-            goto shmemerror;
> +-        }
> ++        p->shmlen[foo] = len;
>           p->myximage[foo]->data = p->Shminfo[foo].shmaddr;
> -         p->Shminfo[foo].readOnly = False;
> +-        p->Shminfo[foo].readOnly = False;
>  -        XShmAttach(vo->x11->display, &p->Shminfo[foo]);
> -+        XShmAttachFd(vo->x11->display, &p->Shminfo[foo]);
>   
>           XSync(vo->x11->display, False);
> - 
> +-
>  -        shmctl(p->Shminfo[foo].shmid, IPC_RMID, 0);
> -+    shm_unlink(p->shmname);
>       } else {
>   shmemerror:
>           p->Shmem_Flag = 0;
> -@@ -151,6 +171,7 @@ static void freeMyXImage(struct priv *p, int foo)
> -     if (p->Shmem_Flag) {
> -         XShmDetach(vo->x11->display, &p->Shminfo[foo]);
> +@@ -148,10 +215,9 @@ shmemerror:
> + static void freeMyXImage(struct priv *p, int foo)
> + {
> +     struct vo *vo = p->vo;
> +-    if (p->Shmem_Flag) {
> +-        XShmDetach(vo->x11->display, &p->Shminfo[foo]);
> ++    if (p->Shminfo[foo].shmaddr) {
>           XDestroyImage(p->myximage[foo]);
> -+    close(p->Shminfo[foo].shmid);
> -         shmdt(p->Shminfo[foo].shmaddr);
> +-        shmdt(p->Shminfo[foo].shmaddr);
> ++        shm_fd_detach(vo->x11->display, &p->Shminfo[foo], p->shmlen[foo]);
>       } else {
>           if (p->myximage[foo]) {
> +             // XDestroyImage() would free the data too since XFree() just 
> calls
> Index: patches/patch-video_out_vo_xv_c
> ===================================================================
> RCS file: /cvs/ports/multimedia/mpv/patches/patch-video_out_vo_xv_c,v
> retrieving revision 1.1
> diff -u -p -u -p -r1.1 patch-video_out_vo_xv_c
> --- patches/patch-video_out_vo_xv_c   2 Mar 2025 10:17:37 -0000       1.1
> +++ patches/patch-video_out_vo_xv_c   15 Nov 2026 02:44:19 -0000
> @@ -1,4 +1,10 @@
> -Implment XShm 1.2
> +Use MIT-SHM 1.2 (fd passing) with POSIX shm instead of SysV shm.
> +
> +xcb owns the fd passed to xcb_shm_attach_fd() and closes it after
> +sending it, so it must not be closed again later.  The mapping comes
> +from mmap(2), so release it with munmap(2) rather than shmdt(2).
> +Only use SHM if the server supports MIT-SHM 1.2 and the connection
> +is a unix domain socket.
>  
>  Index: video/out/vo_xv.c
>  --- video/out/vo_xv.c.orig
> @@ -11,43 +17,118 @@ Index: video/out/vo_xv.c
>   #include <errno.h>
>   #include <X11/Xlib.h>
>   #include <X11/Xutil.h>
> -@@ -32,7 +33,10 @@
> +@@ -30,9 +31,11 @@
> + #include <libavutil/common.h>
> + 
>   #include <sys/types.h>
> - #include <sys/ipc.h>
> - #include <sys/shm.h>
> +-#include <sys/ipc.h>
> +-#include <sys/shm.h>
>  +#include <sys/mman.h>
> ++#include <sys/socket.h>
>   #include <X11/extensions/XShm.h>
>  +#include <X11/Xlib-xcb.h>
>  +#include <xcb/shm.h>
>   
>   // Note: depends on the inclusion of X11/extensions/XShm.h
>   #include <X11/extensions/Xv.h>
> -@@ -91,6 +95,7 @@ struct xvctx {
> +@@ -91,6 +94,7 @@ struct xvctx {
>       GC vo_gc;   // used to paint video
>       int Shmem_Flag;
>       XShmSegmentInfo Shminfo[MAX_BUFFERS];
> -+    char shmname[128];
> ++    size_t shmlen[MAX_BUFFERS];
>       int Shm_Warned_Slow;
>       struct mp_image_params dst_params;
>   };
> -@@ -126,6 +131,16 @@ static int find_xv_format(int imgfmt)
> +@@ -126,6 +130,79 @@ static int find_xv_format(int imgfmt)
>       return 0;
>   }
>   
> -+static Bool XShmAttachFd(Display *dpy, XShmSegmentInfo *shminfo)
> ++/*
> ++ * MIT-SHM 1.2 (fd passing) is needed, and fd passing only works over
> ++ * a unix domain socket.
> ++ */
> ++static bool shm_fd_usable(Display *dpy)
>  +{
> -+    xcb_connection_t *xcb_conn = XGetXCBConnection(dpy);
> ++    struct sockaddr_storage ss;
> ++    socklen_t sslen = sizeof(ss);
> ++    int major, minor;
> ++    Bool pixmaps;
>  +
> -+    shminfo->shmseg = xcb_generate_id(xcb_conn);
> -+    xcb_shm_attach_fd(xcb_conn, shminfo->shmseg,
> -+                shminfo->shmid, shminfo->readOnly);
> -+    return 1;
> ++    if (!XShmQueryVersion(dpy, &major, &minor, &pixmaps))
> ++        return false;
> ++    if (major < 1 || (major == 1 && minor < 2))
> ++        return false;
> ++    if (getsockname(ConnectionNumber(dpy), (struct sockaddr *)&ss,
> ++                    &sslen) == -1)
> ++        return false;
> ++    return ss.ss_family == AF_UNIX;
> ++}
> ++
> ++/*
> ++ * Create a POSIX shm segment of len bytes, map it and pass it to the
> ++ * X server.  xcb owns the fd once it has been handed to
> ++ * xcb_shm_attach_fd_checked() and closes it after sending it, so it
> ++ * must not be closed here after that point.
> ++ */
> ++static bool shm_fd_attach(Display *dpy, XShmSegmentInfo *si, size_t len)
> ++{
> ++    char name[] = "/tmp/mpv-xv-XXXXXXXXXX";
> ++    xcb_connection_t *c = XGetXCBConnection(dpy);
> ++    xcb_generic_error_t *err;
> ++    void *addr;
> ++    int fd;
> ++
> ++    *si = (XShmSegmentInfo){0};
> ++    si->shmid = -1;
> ++
> ++    fd = shm_mkstemp(name);
> ++    if (fd == -1)
> ++        return false;
> ++    shm_unlink(name);
> ++    if (ftruncate(fd, len) == -1) {
> ++        close(fd);
> ++        return false;
> ++    }
> ++    addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
> ++    if (addr == MAP_FAILED) {
> ++        close(fd);
> ++        return false;
> ++    }
> ++
> ++    si->shmseg = xcb_generate_id(c);
> ++    si->readOnly = False;
> ++    err = xcb_request_check(c, xcb_shm_attach_fd_checked(c, si->shmseg,
> ++                                                         fd, si->readOnly));
> ++    if (err) {
> ++        free(err);
> ++        munmap(addr, len);
> ++        *si = (XShmSegmentInfo){0};
> ++        return false;
> ++    }
> ++    si->shmaddr = addr;
> ++    return true;
> ++}
> ++
> ++static void shm_fd_detach(Display *dpy, XShmSegmentInfo *si, size_t len)
> ++{
> ++    XShmDetach(dpy, si);
> ++    munmap(si->shmaddr, len);
> ++    *si = (XShmSegmentInfo){0};
>  +}
>  +
>   static int xv_find_atom(struct vo *vo, uint32_t xv_port, const char *name,
>                           bool get, int *min, int *max)
>   {
> -@@ -561,18 +576,22 @@ static bool allocate_xvimage(struct vo *vo, int foo)
> +@@ -544,7 +621,7 @@ static bool allocate_xvimage(struct vo *vo, int foo)
> +     int aligned_w = FFALIGN(ctx->image_width, 32);
> +     // round up the height to next chroma boundary too
> +     int aligned_h = FFALIGN(ctx->image_height, 2);
> +-    if (x11->display_is_local && XShmQueryExtension(x11->display)) {
> ++    if (x11->display_is_local && shm_fd_usable(x11->display)) {
> +         ctx->Shmem_Flag = 1;
> +         x11->ShmCompletionEvent = XShmGetEventBase(x11->display)
> +                                 + ShmCompletion;
> +@@ -561,18 +638,13 @@ static bool allocate_xvimage(struct vo *vo, int foo)
>           if (!ctx->xvimage[foo])
>               return false;
>   
> @@ -55,32 +136,32 @@ Index: video/out/vo_xv.c
>  -                                         ctx->xvimage[foo]->data_size,
>  -                                         IPC_CREAT | 0777);
>  -        ctx->Shminfo[foo].shmaddr = shmat(ctx->Shminfo[foo].shmid, 0, 0);
> -+    memcpy(ctx->shmname, "/tmp/mpv-xv-XXXXXXXXXX",
> -+           sizeof(ctx->shmname));
> -+    ctx->Shminfo[foo].shmid = shm_mkstemp(ctx->shmname);
> -+    ctx->Shminfo[foo].shmaddr = mmap(NULL, ctx->xvimage[foo]->data_size,
> -+                                     PROT_READ | PROT_WRITE,
> -+                                     MAP_SHARED | __MAP_NOFAULT,
> -+                                     ctx->Shminfo[foo].shmid, 0);
> -+    ftruncate(ctx->Shminfo[foo].shmid, ctx->xvimage[foo]->data_size);
> -         if (ctx->Shminfo[foo].shmaddr == (void *)-1)
> +-        if (ctx->Shminfo[foo].shmaddr == (void *)-1)
> ++        if (!shm_fd_attach(x11->display, &ctx->Shminfo[foo],
> ++                           ctx->xvimage[foo]->data_size))
>               return false;
> -         ctx->Shminfo[foo].readOnly = False;
> +-        ctx->Shminfo[foo].readOnly = False;
> ++        ctx->shmlen[foo] = ctx->xvimage[foo]->data_size;
>   
>           ctx->xvimage[foo]->data = ctx->Shminfo[foo].shmaddr;
>  -        XShmAttach(x11->display, &ctx->Shminfo[foo]);
> -+        XShmAttachFd(x11->display, &ctx->Shminfo[foo]);
>           XSync(x11->display, False);
>  -        shmctl(ctx->Shminfo[foo].shmid, IPC_RMID, 0);
> -+    shm_unlink(ctx->shmname);
>       } else {
>           ctx->xvimage[foo] =
>               (XvImage *) XvCreateImage(x11->display, ctx->xv_port,
> -@@ -605,6 +624,7 @@ static void deallocate_xvimage(struct vo *vo, int foo)
> +@@ -603,10 +675,10 @@ static bool allocate_xvimage(struct vo *vo, int foo)
> + static void deallocate_xvimage(struct vo *vo, int foo)
> + {
>       struct xvctx *ctx = vo->priv;
> -     if (ctx->Shmem_Flag) {
> -         XShmDetach(vo->x11->display, &ctx->Shminfo[foo]);
> -+    close(ctx->Shminfo[foo].shmid);
> -         shmdt(ctx->Shminfo[foo].shmaddr);
> -     } else {
> +-    if (ctx->Shmem_Flag) {
> +-        XShmDetach(vo->x11->display, &ctx->Shminfo[foo]);
> +-        shmdt(ctx->Shminfo[foo].shmaddr);
> +-    } else {
> ++    if (ctx->Shminfo[foo].shmaddr) {
> ++        shm_fd_detach(vo->x11->display, &ctx->Shminfo[foo],
> ++                      ctx->shmlen[foo]);
> ++    } else if (ctx->xvimage[foo]) {
>           av_free(ctx->xvimage[foo]->data);
> +     }
> +     if (ctx->xvimage[foo])
> 

-- 
Matthieu Herrb

Reply via email to