ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
'read' has nothing to do with memory addresses. When you access an invalid address you get a SIGSEGV signal.
As I understood, system emit signal when SIGSEGV is emit, but after catch the signal, program continue in the same line and execute illegal command again and program crash.
This is precisely what you should do: just go ahead and attempt the operation. If it fails for this reason, a SIGSEGV will be thrown, and you can catch it.
You don't need to try to "test to see if the address will be valid." Just go for it. Nearly all the time, the operation will be successful and you can just go on your merry way. The exception to that rule is when the address isn't valid ... and that's one reason why we call 'em "exceptions."
read() and other system calls check for validity of the address and return -EFAULT if the address is invalid (ie. inaccessible for given purpose). SIGSEGV is thrown when program tries to access given memory directly as in:
Code:
*(volatile char *)pointer;
webquinty, why do you need to check validity of an address? It sounds to me like you're doing something you should not be doing.
IMHO, checking the "validity" of an address in this way (i.e checking that it's in your virtual address space) is totally useless.
Even if you can verify it's in your address space, this isn't really "valid", since it could be pointing anywhere in your address space, including data you don't intend or want to be overwritten.
Take the time you're using trying to pre-empt these mistakes and spend it making sure your code's correct.
One "trick" that I have used a lot is to write a few routines of my own to do memory-allocation within an application.
The first routine allocates a guaranteed zero-filled buffer of the specified size plus 8 bytes more. It puts a "4-byte eyecatcher" at the beginning and at the end. The trailing "eyecatcher" begins with a zero-byte. It returns a pointer past the eyecatcher.
Another routine simply checks for the presence of the eyecatcher (at 4 bytes before the passed cannot-be-NULL pointer, and at the end), and throws an exception if not. Otherwise it returns the value passed.
A final routine checks and then disposes-of the buffer passed, doing nothing if NULL.
Each routine also requires a pointer to a character-string constant which is used in the exception-message if thrown.
So what happens? An amazing number of "nassss-s-s-s-ssty little bugssssss-s-s-s-s-ssss" get stomped during development, and/or almost-instantaneously discovered during initial beta-testing. At basically zero cost in performance.
Another routine simply checks for the presence of the eyecatcher (at 4 bytes before the passed cannot-be-NULL pointer, and at the end), and throws an exception if not. Otherwise it returns the value passed.
This of course won't work for arraies if you pass a pointer to an element in the middle of the array. Strings are among this category.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.