From: Rishi k. K R. <ris...@li...> - 2010-03-22 05:49:02
|
The branch, next, has been updated via c700433fe26d24b975bf241668a9701548e6c21d (commit) from 4d574b9f3c82a58d88b97e201ac49cddf7bc5c24 (commit) - Log ----------------------------------------------------------------- commit c700433fe26d24b975bf241668a9701548e6c21d Author: Rishikesh K Rajak <ris...@li...> Date: Mon Mar 22 11:16:51 2010 +0530 Attached patch fixes possible buffer oveflow in sem_getvalue tests (the buffer overflow happens when getpid() returns number that couldn't fit into buffer) and also cleans coding style. Signed-off-by: Cyril Hrubis ch...@su... Acked-by: Garrett Cooper <yan...@gm...> Signed-off-by: Rishikesh K Rajak <ris...@li...> ----------------------------------------------------------------------- Summary of changes: .../conformance/interfaces/sem_getvalue/1-1.c | 21 +++---- .../conformance/interfaces/sem_getvalue/2-1.c | 19 +++--- .../conformance/interfaces/sem_getvalue/2-2.c | 59 ++++++++------------ .../conformance/interfaces/sem_getvalue/4-1.c | 18 +++--- .../conformance/interfaces/sem_getvalue/5-1.c | 23 ++++---- 5 files changed, 61 insertions(+), 79 deletions(-) diff --git a/testcases/open_posix_testsuite/conformance/interfaces/sem_getvalue/1-1.c b/testcases/open_posix_testsuite/conformance/interfaces/sem_getvalue/1-1.c index 767f1b0..c0746c0 100644 --- a/testcases/open_posix_testsuite/conformance/interfaces/sem_getvalue/1-1.c +++ b/testcases/open_posix_testsuite/conformance/interfaces/sem_getvalue/1-1.c @@ -10,9 +10,7 @@ * This test case will call sem_getvalue to update the location referenced * by the semaphpre without effecting the state of the semaphore. The * updated value represents the actual semaphore value when it was called. -*/ - - + */ #include <stdio.h> #include <errno.h> @@ -20,29 +18,29 @@ #include <semaphore.h> #include <sys/stat.h> #include <fcntl.h> +#include <limits.h> #include "posixtest.h" #define TEST "1-1" #define FUNCTION "sem_getvalue" #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": " - -int main() { - - char semname[28]; +int main(void) +{ + char semname[NAME_MAX - 4]; sem_t *mysemp; int val; - sprintf(semname, "/" FUNCTION "_" TEST "_%d", getpid()); + snprintf(semname, sizeof(semname), "/" FUNCTION "_" TEST "_%d", getpid()); mysemp = sem_open(semname, O_CREAT, 0777, 1); - if( mysemp == SEM_FAILED || mysemp == NULL ) { + if (mysemp == SEM_FAILED || mysemp == NULL) { perror(ERROR_PREFIX "sem_open"); return PTS_UNRESOLVED; } - if( sem_getvalue(mysemp, &val) == -1 ) { + if (sem_getvalue(mysemp, &val) == -1) { perror(ERROR_PREFIX "sem_getvalue"); return PTS_UNRESOLVED; } @@ -50,7 +48,8 @@ int main() { /* printf("Current value is: %d\n", val); */ - if (val == 1 ) { + + if (val == 1) { puts("TEST PASSED"); sem_close(mysemp); sem_unlink(semname); diff --git a/testcases/open_posix_testsuite/conformance/interfaces/sem_getvalue/2-1.c b/testcases/open_posix_testsuite/conformance/interfaces/sem_getvalue/2-1.c index ac19af8..06814fb 100644 --- a/testcases/open_posix_testsuite/conformance/interfaces/sem_getvalue/2-1.c +++ b/testcases/open_posix_testsuite/conformance/interfaces/sem_getvalue/2-1.c @@ -8,8 +8,7 @@ /* * When semaphore is locked, then the value returned by sem_getvalue is zero. -*/ - + */ #include <stdio.h> #include <errno.h> @@ -17,35 +16,35 @@ #include <semaphore.h> #include <sys/stat.h> #include <fcntl.h> +#include <limits.h> #include "posixtest.h" #define TEST "2-1" #define FUNCTION "sem_getvalue" #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": " - -int main() { - - char semname[28]; +int main(void) +{ + char semname[NAME_MAX - 4]; sem_t *mysemp; int val; - sprintf(semname, "/" FUNCTION "_" TEST "_%d", getpid()); + snprintf(semname, sizeof(semname), "/" FUNCTION "_" TEST "_%d", getpid()); mysemp = sem_open(semname, O_CREAT, 0777, 1); - if( mysemp == SEM_FAILED || mysemp == NULL ) { + if (mysemp == SEM_FAILED || mysemp == NULL) { perror(ERROR_PREFIX "sem_open"); return PTS_UNRESOLVED; } /* Lock Semaphore */ - if (sem_trywait(mysemp) == -1 ) { + if (sem_trywait(mysemp) == -1) { perror(ERROR_PREFIX "trywait"); return PTS_UNRESOLVED; } - if( sem_getvalue(mysemp, &val) < 0 ) { + if (sem_getvalue(mysemp, &val) < 0) { perror(ERROR_PREFIX "sem_getvalue"); return PTS_UNRESOLVED; } diff --git a/testcases/open_posix_testsuite/conformance/interfaces/sem_getvalue/2-2.c b/testcases/open_posix_testsuite/conformance/interfaces/sem_getvalue/2-2.c index 5918ceb..9989e48 100644 --- a/testcases/open_posix_testsuite/conformance/interfaces/sem_getvalue/2-2.c +++ b/testcases/open_posix_testsuite/conformance/interfaces/sem_getvalue/2-2.c @@ -83,18 +83,15 @@ /*************************** Test case ***********************************/ /******************************************************************************/ -void * threaded ( void * arg ) +void *threaded(void * arg) { int ret; - do - { + do { ret = sem_wait( arg ); - } - while ( ( ret != 0 ) && ( errno == EINTR ) ); + } while (( ret != 0 ) && (errno == EINTR)); - if ( ret != 0 ) - { + if (ret != 0) { UNRESOLVED( errno, "Failed to wait for the semaphore" ); } @@ -103,7 +100,7 @@ void * threaded ( void * arg ) /* The main test function. */ -int main( int argc, char * argv[] ) +int main(int argc, char *argv[]) { int ret, val; sem_t sem; @@ -113,71 +110,61 @@ int main( int argc, char * argv[] ) output_init(); /* Initialize semaphore */ - ret = sem_init( &sem, 0, 0 ); + ret = sem_init(&sem, 0, 0); - if ( ret != 0 ) - { + if (ret != 0) { UNRESOLVED( errno, "Failed to init semaphore" ); } /* Create the thread */ - ret = pthread_create( &th, NULL, threaded, &sem ); + ret = pthread_create(&th, NULL, threaded, &sem); - if ( ret != 0 ) - { + if (ret != 0) { UNRESOLVED( ret, "Failed to create the thread" ); } /* Sleep 1 sec so the thread enters the sem_wait call */ - sleep( 1 ); + sleep(1); /* Check value */ - ret = sem_getvalue( &sem, &val ); + ret = sem_getvalue(&sem, &val); - if ( ret != 0 ) - { + if (ret != 0) { UNRESOLVED( errno, "Failed to get semaphore value" ); } - if ( ( val != 0 ) && ( val != -1 ) ) - { - output( "Val: %d\n", val ); - FAILED( "Semaphore count is neither 0 nor # of waiting processes" ); + if ((val != 0) && (val != -1)) { + output("Val: %d\n", val ); + FAILED("Semaphore count is neither 0 nor # of waiting processes"); } /* Post the semaphore */ - ret = sem_post( &sem ); + ret = sem_post(&sem); - if ( ret != 0 ) - { - UNRESOLVED( errno, "Failed to post the semaphore" ); + if (ret != 0) { + UNRESOLVED(errno, "Failed to post the semaphore"); } /* Join the thread */ - ret = pthread_join( th, NULL ); + ret = pthread_join(th, NULL); - if ( ret != 0 ) - { + if (ret != 0) { UNRESOLVED( ret, "Failed to join the thread" ); } - /* Destroy the semaphore */ - ret = sem_destroy( &sem ); + ret = sem_destroy(&sem); - if ( ret != 0 ) - { + if (ret != 0) { UNRESOLVED( errno, "Failed to sem_destroy" ); } /* Test passed */ #if VERBOSE > 0 - output( "Test passed\n" ); + output("Test passed\n"); #endif PASSED; } - - diff --git a/testcases/open_posix_testsuite/conformance/interfaces/sem_getvalue/4-1.c b/testcases/open_posix_testsuite/conformance/interfaces/sem_getvalue/4-1.c index 0774acb..2507fd3 100644 --- a/testcases/open_posix_testsuite/conformance/interfaces/sem_getvalue/4-1.c +++ b/testcases/open_posix_testsuite/conformance/interfaces/sem_getvalue/4-1.c @@ -9,9 +9,7 @@ /* * Upon successful completion of calling sem_getvalue, it shall return a value * of zero. -*/ - - + */ #include <stdio.h> #include <errno.h> @@ -19,29 +17,29 @@ #include <semaphore.h> #include <sys/stat.h> #include <fcntl.h> +#include <limits.h> #include "posixtest.h" #define TEST "4-1" #define FUNCTION "sem_getvalue" #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": " - -int main() { - - char semname[28]; +int main(void) +{ + char semname[NAME_MAX - 4]; sem_t *mysemp; int val; - sprintf(semname, "/" FUNCTION "_" TEST "_%d", getpid()); + snprintf(semname, sizeof(semname), "/" FUNCTION "_" TEST "_%d", getpid()); mysemp = sem_open(semname, O_CREAT, 0777, 1); - if( mysemp == SEM_FAILED || mysemp == NULL ) { + if (mysemp == SEM_FAILED || mysemp == NULL) { perror(ERROR_PREFIX "sem_open"); return PTS_UNRESOLVED; } - if( sem_getvalue(mysemp, &val) != 0 ) { + if (sem_getvalue(mysemp, &val) != 0) { puts("TEST FAILED"); return PTS_FAIL; } else { diff --git a/testcases/open_posix_testsuite/conformance/interfaces/sem_getvalue/5-1.c b/testcases/open_posix_testsuite/conformance/interfaces/sem_getvalue/5-1.c index c77b19c..1f17f5f 100644 --- a/testcases/open_posix_testsuite/conformance/interfaces/sem_getvalue/5-1.c +++ b/testcases/open_posix_testsuite/conformance/interfaces/sem_getvalue/5-1.c @@ -9,8 +9,7 @@ /* * This test case verifies that calling sem_getvalue doesn't change the * state of the semaphore. -*/ - + */ #include <stdio.h> #include <errno.h> @@ -18,40 +17,40 @@ #include <semaphore.h> #include <sys/stat.h> #include <fcntl.h> +#include <limits.h> #include "posixtest.h" #define TEST "5-1" #define FUNCTION "sem_getvalue" #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": " - -int main() { - - char semname[20]; +int main(void) +{ + char semname[NAME_MAX - 4]; sem_t *mysemp; int val; - sprintf(semname, "/" FUNCTION "_" TEST "_%d", getpid()); + snprintf(semname, sizeof(semname), "/" FUNCTION "_" TEST "_%d", getpid()); mysemp = sem_open(semname, O_CREAT, 0777, 4); - if( mysemp == SEM_FAILED || mysemp == NULL ) { + if (mysemp == SEM_FAILED || mysemp == NULL) { perror(ERROR_PREFIX "sem_open"); return PTS_UNRESOLVED; } - if( sem_getvalue(mysemp, &val) == -1 ) { + if (sem_getvalue(mysemp, &val) == -1) { perror(ERROR_PREFIX "sem_getvalue"); return PTS_UNRESOLVED; } - if ( sem_trywait(mysemp) == -1 ) { + if (sem_trywait(mysemp) == -1) { perror(ERROR_PREFIX "sem_trywait"); return PTS_UNRESOLVED; } - if( sem_getvalue(mysemp, &val) == -1 ) { + if (sem_getvalue(mysemp, &val) == -1) { perror(ERROR_PREFIX "sem_getvalue"); return PTS_UNRESOLVED; } @@ -60,7 +59,7 @@ int main() { printf("Current value is: %d\n", val); */ - if (val == 3 ) { + if (val == 3) { puts("TEST PASSED"); sem_close(mysemp); sem_unlink(semname); hooks/post-receive -- ltp |