Thinking about the defects of basename function in Linux system

Source: Internet
Author: User
Tags function prototype prototype definition

When a module runs independently as a foreground process, the command-line arguments are run with command-line arguments, and command-row parameters need to be cured in the code as a daemon sub-process running on a platform. Similar to the following wording:

Char *argv[] = {"./dsldriver", "-T", "/bin/vdslmodemsco.bin"};

int argc = sizeof (argv)/sizeof (argv[0]);

Then, call the BaseName function (the header file is Libgen.h) to parse argv[0], which is "./dsldriver". It is found that the parsing is normal in Linux native system, and a segment error occurs when parsing on a platform.

The rational idea is naturally suspect that the implementation of the basename function is different in both environments. Linux native function source code is not found, but a platform UCLIBC source code can find the implementation of the basename function:

1 /*Return Final component of PATH.2 This is the weird XPG version of this function. It sometimes would3 modify its argument. Therefore we normally use the GNU version ( in4 <string.h>) and only if this header is included make the XPG5 version available under the real name. */6 extern Char*__xpg_basename (Char*__path) __throw;7 #defineBaseName __xpg_basename
Libgen.h

Regardless of the annotation content, locate the __xpg_basename definition directly:

1 Char*__xpg_basename (RegisterChar*path)2 {3     Static Const CharNull_or_empty[] =".";4     Char*First ;5RegisterChar*Last ;6 7First = (Char*) Null_or_empty;8 9     if(Path && *path) {TenFirst =path; OneLast = path-1; A  -          Do { -             if((*path! ='/') && (Path > + +Last )) { thelast = First =path; -             } -} while(*++path); -  +         if(*first = ='/') { -Last =First ; +         } Alast[1] =0;//pay attention to this sentence!  at     } -  -     returnFirst ; -}
wstring.c

It is visible that the function adds a terminator ('% ') to the tail of the path parameter. Now the truth is, basename ("./dsldriver") passed in a read-only string, causing the basename function to attempt to modify the read-only data area, of course, a segment error occurs. In the Run command, the command-line argument is not read-only (it is actually a character array), so there is no segment error.

To safely use the basename function under a platform, there are two ways to change it:

1) incoming character array. As follows:

Char argv[][sizeof ("/bin/vdslmodemsco.bin")] = {"./dsldriver", "-T", "/bin/vdslmodemsco.bin"};

2) Use the GNU Implementation version (header file is string.h). The GNU version will never change its parameters, so static strings can be handled correctly.

Thinking

Recall, is it necessary to add a terminator to path in basename? Personally, it is not necessary to restrict the application of the function. Theoretically, if path is a read-only string, it means that the caller knows the path content, manually stripping it, without calling the BaseName function (the "reasonable" of the source code). This module is only used in a special way. On the other hand, the flaw also alert the coder, do not "memset" to enter the contents of the parameter (to make it lose cumulative), or add a terminator to the argument string. These details are left to the caller to be free to play.

"Tips"

How do I find the prototype definition of a library function?

Very simply, "overwrite" the function declaration at the call. For example, the expected function prototype is an int func (char *)--usually guessed by the invocation, and declared as an int func (int) before the call. The compiler will instruct previous declaration of Thelibfuncby conflicting types Error!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.