4
\$\begingroup\$

I have written the following code in order to:

  1. Find begin and end of FTP user and password from a given URL (ftp://[user[:password]@]host[:port]/url-path)
  2. Allocate memory to hold this string
  3. Copy the sub-string into the allocated memory

Here is the code for your review:

static char * swd_parseUsrPass(void)
{
 char *usr_pass = NULL;
 char *url_str = NULL;
 char *beginIndx = NULL;
 char *endIndx = NULL;
 int str_len = 0;
 url_str = getURLaddr();
 /* Find begin and end indexs */
 beginIndx = url_str + strlen(FTP_PREFIX);
 endIndx = strchr(beginIndx, '@');
 if ((endIndx == NULL) || endIndx <= beginIndx) {
 printf("Could not locate user and password in URL\n");
 return NULL;
 }
 str_len = endIndx - beginIndx;
 /* Allocate maximum possible string */
 usr_pass = malloc((sizeof(char) * str_len) + 1);
 if (usr_pass == NULL) {
 printf("Failed to allocated memory!\n");
 return NULL;
 }
 /* Copy user and password to new string */
 strncpy(usr_pass, beginIndx, str_len);
 *(endIndx + 1) = '0円';
 return usr_pass;
}
asked Oct 9, 2016 at 14:03
\$\endgroup\$
2
  • \$\begingroup\$ Though not common. The @ character can appear in the url-path \$\endgroup\$ Commented Oct 9, 2016 at 15:18
  • \$\begingroup\$ Also if this was going to go into real code. You have to remember that on the web URL don't always follow spec (as a writer of a web crawler I can testify to the number of badly written URLS). So the '/' marking the url-path may not be there so you have to check for all the common mistakes. ? , & , # , : \$\endgroup\$ Commented Oct 9, 2016 at 15:23

2 Answers 2

1
\$\begingroup\$
  • A 0-ary function (swd_parseUsrPass) calling another 0-ary function (swd_getURLaddr) hints that there are global variables lurking around. Using globals is not recommended; if they are unavoidable, at least limit their visibility. Consider char swd_parseUsrPass(char * url_addr), and let the caller provide the argument.

  • sizeof(char) is guaranteed to be 1.

  • *(endIndx + 1) = '0円' modifies the source string, while usr_pass remains not terminated. I presume you meant

    usr_pass[str_len] = '0円';
    
answered Oct 9, 2016 at 16:23
\$\endgroup\$
1
  • \$\begingroup\$ Thank you @vnp. I'm actually not using global variables, but I do use static variables (defined only in this C file content) - do you see any problem with that? \$\endgroup\$ Commented Oct 9, 2016 at 17:54
1
\$\begingroup\$

I agree with all points made by vnp, except I'd suggest swd_parseUsrPass(const char *url_addr) as the prototype, and I'd add:

  • all your pointers except usr_pass should be const char * as you do not intend to modify the string they point to. This would have help you noticing the *(endIndx + 1) = '0円' issue pointed out by vnp.
  • the test endIndx <= beginIndx seems redundant to me
  • I'm not a big fan of Indx in the name: these are pointers, indexes would be integers (not a big fan of mixing underscores and camelCase either, but that's more an issue of taste)
answered Oct 12, 2016 at 13:50
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.