Deleted Added
full compact
2537,2539c2537,2538
< char tried[512] = { '\0' };
< char buff[128];
< char *pwd, *p;
---
> size_t tried_size, buff_size;
> char *buff, *tried, *pwd = NULL, *p = NULL;
2540a2540,2558
> #ifdef PATH_MAX
> buff_size = PATH_MAX;
> #else
> buff_size = 8192;
> #endif
> buff = calloc(buff_size, 1);
> if (buff == NULL) {
> fprintf(stderr, "Unable to allocate memory\n");
> exit(1);
> }
>
> /* Allocate a buffer to hold the various directories we checked. */
> tried_size = buff_size * 2;
> tried = calloc(tried_size, 1);
> if (tried == NULL) {
> fprintf(stderr, "Unable to allocate memory\n");
> exit(1);
> }
>
2544c2562
< snprintf(buff, sizeof(buff), "%s", d);
---
> snprintf(buff, buff_size, "%s", d);
2547,2548c2565,2566
< strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
< strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
---
> strncat(tried, buff, tried_size - strlen(tried) - 1);
> strncat(tried, "\n", tried_size - strlen(tried) - 1);
2562c2580
< snprintf(buff, sizeof(buff), "%s", pwd);
---
> snprintf(buff, buff_size, "%s", pwd);
2565,2566c2583,2584
< strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
< strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
---
> strncat(tried, buff, tried_size - strlen(tried) - 1);
> strncat(tried, "\n", tried_size - strlen(tried) - 1);
2568c2586
< snprintf(buff, sizeof(buff), "%s/test", pwd);
---
> snprintf(buff, buff_size, "%s/test", pwd);
2571,2572c2589,2590
< strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
< strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
---
> strncat(tried, buff, tried_size - strlen(tried) - 1);
> strncat(tried, "\n", tried_size - strlen(tried) - 1);
2575c2593
< snprintf(buff, sizeof(buff), "%s/%s/test", pwd, LIBRARY);
---
> snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY);
2577c2595
< snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM);
---
> snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM);
2581,2582c2599,2600
< strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
< strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
---
> strncat(tried, buff, tried_size - strlen(tried) - 1);
> strncat(tried, "\n", tried_size - strlen(tried) - 1);
2585c2603
< snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM_ALIAS);
---
> snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS);
2588,2589c2606,2607
< strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
< strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
---
> strncat(tried, buff, tried_size - strlen(tried) - 1);
> strncat(tried, "\n", tried_size - strlen(tried) - 1);
2593c2611
< snprintf(buff, sizeof(buff), "%s", pwd + 8);
---
> snprintf(buff, buff_size, "%s", pwd + 8);
2596,2597c2614,2615
< strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
< strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
---
> strncat(tried, buff, tried_size - strlen(tried) - 1);
> strncat(tried, "\n", tried_size - strlen(tried) - 1);
2599c2617
< snprintf(buff, sizeof(buff), "%s/test", pwd + 8);
---
> snprintf(buff, buff_size, "%s/test", pwd + 8);
2602,2603c2620,2621
< strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
< strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
---
> strncat(tried, buff, tried_size - strlen(tried) - 1);
> strncat(tried, "\n", tried_size - strlen(tried) - 1);
2618c2636,2641
< return strdup(buff);
---
> free(tried);
>
> /* Copy result into a fresh buffer to reduce memory usage. */
> p = strdup(buff);
> free(buff);
> return p;