Writing Device Drivers

Handling copyout() Overflow

Sometimes a driver needs to copy out a native quantity that no longer fits in the 32-bit sized structure. In this case, the driver should return EOVERFLOW to the caller as an indication that the data type in the interface is too small to hold the value to be returned, as shown in Example 10–16.


Example 10–16 Handling copyout(9F) Overflow

int
    xxioctl(dev_t dev, int cmd, intptr_t arg, int mode,
         cred_t *cr, int *rval_p)
    {
            struct resdata res;

            ... body of driver code ...

            switch (ddi_model_convert_from(mode & FMODELS)) {
            case DDI_MODEL_ILP32: {
                        struct resdata32 res32;

                        if (res.size > UINT_MAX)
                                    return (EOVERFLOW);    
                        res32.size = (size32_t)res.size;
                        res32.flag = res.flag;
                        if (ddi_copyout(&res32,
                                (void *)arg, sizeof (res32), mode))
                                    return (EFAULT);
            }
            break;

            case DDI_MODEL_NONE:
                        if (ddi_copyout(&res, (void *)arg, sizeof (res), mode))
                                    return (EFAULT);
                        break;
            }
            return (0);
    }