考虑到安全问题,我为此问题开发了一个解决方案。作为一般的做法,在子程序中不干预原始序列内容和地址。我已经考虑了我开发的解决方案中可能出现的一些错误元素。以下链接可用于测试程序:GodBolt
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/// It is used for the simplest form of adaptation to varying array sizes.
#define ARRAY_SIZE 7
/**
* @brief It will be used to handle some errors that may occur in the program.
*/
enum errorCode
{
faultless = 0, // No error occurred in the program
sourceArrayError = -1, // Source array doesn't point to an address.
destinationArrayError = -2, // Destination array doesn't point to an address.
locationTargetError = -3, // Location target exceeds array size.
copyError = -4, // Copy error has occurred.
sizeError = -5, // Array sizes are not equal.
};
/**
* @brief The error message is printed to the stderr stream to show the cause
* of the error in the console.
* @param signum is the error number argument.
*/
void errorHandler( int signum )
{
switch( signum )
{
case sourceArrayError:
fprintf( stderr, "Source array doesn't point to an address." );
break;
case destinationArrayError:
fprintf( stderr, "Destination array doesn't point to an address." );
break;
case locationTargetError:
fprintf( stderr, "Location target exceeds array size." );
break;
case copyError:
fprintf( stderr, "Copy error has occurred." );
break;
case sizeError:
fprintf( stderr, "Array sizes are not equal." );
break;
default:
fprintf( stderr, "An unknown error has occurred." );
break;
}
exit( EXIT_FAILURE );
}
/**
* @brief Subprogram that can be used for safe transport of the contents of an array.
* @param originalArray The content of the originalArray array is passed to the program as
* an address to be manipulated. Since the array originalArray is passed to the
* subprogram as "const int * const", its address and content cannot be changed.
* @param resultArray Is the array where the result data will be stored.
* @param size Refers to the size of the original directory.
* @param rLocation Refers to the target location.
* @param mLocation Refers to the position of the data to be moved in the array.
* @return Returns the verification code if no error occurs in the program.
*/
int move( const int * const originalArray, int * const resultArray, const int size, const int rLocation, const int mLocation )
{
/// If the array doesn't point to an address, terminate the program.
if( originalArray == NULL )
{
errorHandler( sourceArrayError );
}
/// If the array doesn't point to an address, terminate the program.
if( resultArray == NULL )
{
errorHandler( destinationArrayError );
}
/// Terminate the program if the position of the element to be read in the array is outside the array dimensions.
if( mLocation >= size )
{
errorHandler( locationTargetError );
}
/// Terminate the program if the array size argument passed to the subprogram is greater than the current array size.
if( ARRAY_SIZE < size )
{
errorHandler( sizeError );
}
/// The data in the position passed on an argument is being moved.
/// originalArray should only be used as a rvalue in the program.
resultArray[ 0 ] = originalArray[ mLocation ];
/// The first episode of the original array is copied to the new array.
if( memcpy( &resultArray[ rLocation + 1 ], &originalArray[ rLocation ], ( mLocation - rLocation ) * sizeof( int ) ) == NULL )
{
errorHandler( copyError );
}
/// The second part of the original array is copied to the new array.
if( memcpy( &resultArray[ mLocation + 1 ], &originalArray[ mLocation + 1 ], ( ARRAY_SIZE - ( mLocation + 1 ) ) * sizeof( int ) ) == NULL )
{
errorHandler( copyError );
}
return faultless;
}
/**
* @brief Main Program
* @return Returns 0 if the program is successful.
*/
int main( void )
{
int originalArray[ ARRAY_SIZE ] = { 1, 2, 3, 0, 4, 5, 6 };
/// Any intervention to the original array isn't acceptable for high security purposes.
int resultArray[ ARRAY_SIZE ] = { 0 };
/// The new order will be stored in this array for testing purposes.
unsigned int index = 0;
/// It will be used for counter checking in the for loop.
int errorCode;
/// Will be used to provide error checking.
errorCode = move( originalArray, resultArray, ARRAY_SIZE, 0, 3 );
/// The 4 th element of the array will be moved to the memory address where the 1st
/// element of the array is stored. Other array elements will be right shifted.
/// If there was no error in the subprogram, print the contents of the array to the console.
if( errorCode == faultless )
{
for( index = 0 ; index < ARRAY_SIZE ; ++index )
{
printf( "originalArray[ %u ]: %d\t\t", index, originalArray[ index ] );
printf( "resultArray[ %u ]: %d\n", index, resultArray[ index ] );
}
}
return EXIT_SUCCESS;
}