Wednesday, March 6, 2013

Type safety

  • Type safety

In computer science, type safety is the extent to which a programming language discourages or prevents type errors.
A type error is erroneous or undesirable program behaviour caused by a discrepancy between differing data types for the program's constants, variables, and methods (functions), e.g., treating an integer (int) as a floating-point number (float).
some languages have type-safe facilities that can be circumvented by programmers who adopt practices that exhibit poor type safety

Type enforcement can be static, catching potential errors at compile time, or dynamic, associating type information with values at run-time and consulting them as needed to detect imminent errors, or a combination of both.

Type safety is closely linked to memory safety, a restriction on the ability to copy arbitrary bit patterns from one memory location to another. For instance, in an implementation of a language that has some type t, such that some sequence of bits (of the appropriate length) does not represent a legitimate member of t, if that language allows data to be copied into a variable of type t, then it is not type-safe because such an operation might assign a non-t value to that variable.

Conversely, if the language is type-unsafe to the extent of allowing an arbitrary integer to be used as a pointer, then it is clearly not memory-safe.

Type-safe code accesses only the memory locations it is authorized to access
(For this discussion, type safety specifically refers to memory type safety and should not be confused with type safety in a broader respect.)
For example, type-safe code cannot read values from another object's private fields.


Type safety is ultimately aimed at excluding other problems
Prevention of illegal operations. For example, we can identify an expression 3 / "Hello, World" as invalid, because the rules of arithmetic do not specify how to divide an integer by a string.
Memory safety
    Wild pointers can arise when a pointer to one type object is treated as a pointer to another type. For instance, the size of an object depends on the type, so if a pointer is incremented under the wrong credentials, it will end up pointing at some random area of memory.
Buffer overflow - Out-of bound writes can corrupt the contents of objects already present on the heap. This can occur when a larger object of one type is crudely copied into smaller object of another type.
Logic errors originating in the semantics of different types. For instance, inches and millimeters may both be stored as integers, but should not be substituted for each other or added. A type system can enforce two different types of integer for them.


#include <iostream>
using namespace std;

int main()
{
    int ival = 5;                   // A four-byte integer (on most processors)
    void *pval = &ival;             // Store the address of ival in an untyped pointer
    double dval = *((double*)pval); // Convert it to a double pointer and get the value at that address
    cout << dval << endl;           // Output the double (this will be garbage and not 5!)
    return 0;
}

/*
Even though pval does contain the correct address of ival,
when pval is cast from a void pointer to a double pointer the resulting double value is not 5,
but an undefined garbage value. On the machine code level,
this program has explicitly prevented the processor from performing the correct conversion from a four-byte integer to an eight-byte floating-point value.
When the program is run it will output a garbage floating-point value and possibly raise a memory exception.
Thus, C++ (and C) allow type-unsafe code.
*/

http://en.wikipedia.org/wiki/Type_safety





  • Type safety means that the compiler will validate types while compiling, and throw an error if you try to assign the wrong type to a variable.


Some simple examples:
// Fails, Trying to put an integer in a string
String one = 1;
// Also fails.
int foo = "bar";


This also applies to method arguments, since you are passing explicit types to them:
int AddTwoNumbers(int a, int b)
{
    return a + b;
}

If I tried to call that using: The compiler would throw an error, because I am passing a string ("5"), and it is expecting an integer.
int Sum = AddTwoNumbers(5, "5");



In a loosely typed language, such as javascript
function AddTwoNumbers(a, b)
{
    return a + b;
}
Sum = AddTwoNumbers(5, "5");
Javascript automaticly converts the 5 to a string, and returns "55".
This is due to javascript using the + sign for string concatenation
To make it type-aware, you would need to do something like:

function AddTwoNumbers(a, b)
{
    return Number(a) + Number(b);
}

Or, possibly:

function AddOnlyTwoNumbers(a, b)
{
    if (isNaN(a) || isNaN(b))
        return false;
    return Number(a) + Number(b);
}


http://stackoverflow.com/questions/260626/what-is-type-safe


No comments:

Post a Comment