diff options
-rw-r--r-- | src/libgamma-facade.c | 32 | ||||
-rw-r--r-- | src/libgamma-facade.h | 8 | ||||
-rw-r--r-- | test/test.c | 36 |
3 files changed, 40 insertions, 36 deletions
diff --git a/src/libgamma-facade.c b/src/libgamma-facade.c index 25a7ba8..528a2fd 100644 --- a/src/libgamma-facade.c +++ b/src/libgamma-facade.c @@ -159,6 +159,9 @@ static int libgamma_list_method_test(int method, int operation) * List available adjustment methods by their order of preference based on the environment * * @param methods Output array of methods, should be able to hold `GAMMA_METHOD_COUNT` elements + * @þaram buf_size The number of elements that fits in `methods`, it should be `GAMMA_METHOD_COUNT`, + * This is used to avoid writing outside the output buffer if this library adds new + * adjustment methods without the users of the library recompiling * @param operation Allowed values: * 0: Methods that the environment suggests will work, excluding fake. * 1: Methods that the environment suggests will work, including fake. @@ -166,9 +169,10 @@ static int libgamma_list_method_test(int method, int operation) * 3: All real methods. * 4: All methods. * Other values invoke undefined behaviour. - * @return The number of element that have been stored in `methods` + * @return The number of element that have been stored in `methods`, or should + * have been stored if the buffer was large enought */ -size_t libgamma_list_methods(int* restrict methods, int operation) +size_t libgamma_list_methods(int* restrict methods, size_t buf_size, int operation) { #ifdef HAVE_NO_GAMMA_METHODS (void) methods; @@ -178,28 +182,28 @@ size_t libgamma_list_methods(int* restrict methods, int operation) size_t n = 0; #ifdef HAVE_GAMMA_METHOD_X_RANDR - if (libgamma_list_method_test(GAMMA_METHOD_X_RANDR, operation)) - methods[n++] = GAMMA_METHOD_X_RANDR; + if (libgamma_list_method_test(GAMMA_METHOD_X_RANDR, operation) && (n++ < buf_size)) + methods[n - 1] = GAMMA_METHOD_X_RANDR; #endif #ifdef HAVE_GAMMA_METHOD_X_VIDMODE - if (libgamma_list_method_test(GAMMA_METHOD_X_VIDMODE, operation)) - methods[n++] = GAMMA_METHOD_X_VIDMODE; + if (libgamma_list_method_test(GAMMA_METHOD_X_VIDMODE, operation) && (n++ < buf_size)) + methods[n - 1] = GAMMA_METHOD_X_VIDMODE; #endif #ifdef HAVE_GAMMA_METHOD_LINUX_DRM - if (libgamma_list_method_test(GAMMA_METHOD_LINUX_DRM, operation)) - methods[n++] = GAMMA_METHOD_LINUX_DRM; + if (libgamma_list_method_test(GAMMA_METHOD_LINUX_DRM, operation) && (n++ < buf_size)) + methods[n - 1] = GAMMA_METHOD_LINUX_DRM; #endif #ifdef HAVE_GAMMA_METHOD_W32_GDI - if (libgamma_list_method_test(GAMMA_METHOD_W32_GDI, operation)) - methods[n++] = GAMMA_METHOD_W32_GDI; + if (libgamma_list_method_test(GAMMA_METHOD_W32_GDI, operation) && (n++ < buf_size)) + methods[n - 1] = GAMMA_METHOD_W32_GDI; #endif #ifdef HAVE_GAMMA_METHOD_QUARTZ_CORE_GRAPHICS - if (libgamma_list_method_test(GAMMA_METHOD_QUARTZ_CORE_GRAPHICS, operation)) - methods[n++] = GAMMA_METHOD_QUARTZ_CORE_GRAPHICS; + if (libgamma_list_method_test(GAMMA_METHOD_QUARTZ_CORE_GRAPHICS, operation) && (n++ < buf_size)) + methods[n - 1] = GAMMA_METHOD_QUARTZ_CORE_GRAPHICS; #endif #ifdef HAVE_GAMMA_METHOD_DUMMY - if (libgamma_list_method_test(GAMMA_METHOD_DUMMY, operation)) - methods[n++] = GAMMA_METHOD_DUMMY; + if (libgamma_list_method_test(GAMMA_METHOD_DUMMY, operation) && (n++ < buf_size)) + methods[n - 1] = GAMMA_METHOD_DUMMY; #endif return n; diff --git a/src/libgamma-facade.h b/src/libgamma-facade.h index ecc3b13..f3d6ef8 100644 --- a/src/libgamma-facade.h +++ b/src/libgamma-facade.h @@ -77,6 +77,9 @@ typedef double libgamma_gamma_rampsd_fun(double encoding); * List available adjustment methods by their order of preference based on the environment * * @param methods Output array of methods, should be able to hold `GAMMA_METHOD_COUNT` elements + * @þaram buf_size The number of elements that fits in `methods`, it should be `GAMMA_METHOD_COUNT`, + * This is used to avoid writing outside the output buffer if this library adds new + * adjustment methods without the users of the library recompiling * @param operation Allowed values: * 0: Methods that the environment suggests will work, excluding fake. * 1: Methods that the environment suggests will work, including fake. @@ -84,9 +87,10 @@ typedef double libgamma_gamma_rampsd_fun(double encoding); * 3: All real methods. * 4: All methods. * Other values invoke undefined behaviour. - * @return The number of element that have been stored in `methods` + * @return The number of element that have been stored in `methods`, or should + * have been stored if the buffer was large enought */ -size_t libgamma_list_methods(int* restrict methods, int operation); +size_t libgamma_list_methods(int* restrict methods, size_t buf_size, int operation); /** * Return the capabilities of an adjustment method diff --git a/test/test.c b/test/test.c index 9dc3b1b..4b499b3 100644 --- a/test/test.c +++ b/test/test.c @@ -18,35 +18,31 @@ #include <libgamma.h> #include <stdio.h> +#include <stdlib.h> int main(void) { - int methods[GAMMA_METHOD_COUNT]; - size_t n = libgamma_list_methods(methods, 0); + int* methods = malloc(GAMMA_METHOD_COUNT * sizeof(int)); + size_t n = libgamma_list_methods(methods, GAMMA_METHOD_COUNT, 0); size_t i; + if (n > GAMMA_METHOD_COUNT) + { + printf("Warning: you should to recompile the program, libgamma has been updated.\n"); + methods = realloc(methods, n * sizeof(int)); + libgamma_list_methods(methods, n, 0); + } + for (i = 0; i < n; i++) switch (methods[i]) { - case GAMMA_METHOD_DUMMY: - printf("dummy\n"); - break; - case GAMMA_METHOD_X_RANDR: - printf("RandR X extension\n"); - break; - case GAMMA_METHOD_X_VIDMODE: - printf("VidMode X extension\n"); - break; - case GAMMA_METHOD_LINUX_DRM: - printf("Linux DRM\n"); - break; - case GAMMA_METHOD_W32_GDI: - printf("Windows GDI\n"); - break; - case GAMMA_METHOD_QUARTZ_CORE_GRAPHICS: - printf("Quartz using Core Graphics\n"); - break; + case GAMMA_METHOD_DUMMY: printf("dummy\n"); break; + case GAMMA_METHOD_X_RANDR: printf("RandR X extension\n"); break; + case GAMMA_METHOD_X_VIDMODE: printf("VidMode X extension\n"); break; + case GAMMA_METHOD_LINUX_DRM: printf("Linux DRM\n"); break; + case GAMMA_METHOD_W32_GDI: printf("Windows GDI\n"); break; + case GAMMA_METHOD_QUARTZ_CORE_GRAPHICS: printf("Quartz using Core Graphics\n"); break; default: #if GAMMA_METHOD_COUNT != 6 # warning List of adjustment methods is out of date |