z

Commercial Support Clients: Clients with support contracts can get escalated support assistance by visiting Nagios Answer Hub. These forums are for community support services. Although we at Nagios try our best to help out on the forums here, we always give priority support to our support clients.

Re: [Nagios-devel] Erroneous use of getcwd on lib/nspath.c

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.

Re: [Nagios-devel] Erroneous use of getcwd on lib/nspath.c

Postby Guest » Fri Oct 26, 2012 3:10 pm

Good thing I read the mail before I submitted my previous code.

This looks good. Thanks.

On 10/26/2012 05:23 PM, Ricardo Jose Maraschini wrote:
> * Ricardo Jose Maraschini (ricardo.maraschini@opservices.com.br) wrote:
>> * Ricardo Jose Maraschini (ricardo.maraschini@opservices.com.br) wrote:
>>>
>>> Accordingly to manpages and some home made tests, getcwd returns NULL
>>> when an error occurs, not a value < 0.
>>>
>>> Following, on openbsd MAX_PATH is 1024 bytes while in linux it is 4096
>>> bytes, so i thought it would be ok to include and use macro
>>> MAX_PATH, not a fixed size of 4096.
>>>
>>> Also we don't need to make sizeof(path) - 1 when calling getcwd.
>>> Quoting manpage:
>>>
>>> "The size argument is the size, in bytes, of the array referenced by
>>> buf."
>>>
>>> Is it sounds resonable?
>>> Comments?
>>
>> I keep trolling nagios and discovered that it segfaults, or, as the code
>> states "256 components will fail hard" with a configuration path with
>> more than 256 directories.
>>
>> I know that a configuration with more than 256 directories is, above
>> all, stupid, but i do prefer to have it working even if the
>> administrator isn't so much clever.
>>
>> So, the previous patch may be ignored cause it's included on this.
>>
>
> Sorry, wrong patch, here goes the right one.
>
> ===================================================================
> --- lib/nspath.c (revision 2409)
> +++ lib/nspath.c (working copy)
> @@ -5,6 +5,7 @@
> #include
> #include
> #include
> +#include
> #include "nspath.h"
>
> #ifndef PATH_MAX
> @@ -64,7 +65,7 @@
> */
> char *nspath_normalize(const char *orig_path)
> {
> - struct pcomp pcomp[256]; /* >256 components will fail hard */
> + struct pcomp *pcomp = NULL;
> int comps, i = 0, m, depth = 0;
> char *path, *rpath, *p, *slash;
>
> @@ -73,7 +74,10 @@
>
> rpath = strdup(orig_path);
> comps = path_components(rpath);
> - memset(pcomp, 0, sizeof(pcomp));
> + pcomp = calloc(comps,sizeof(struct pcomp));
> + if (pcomp == NULL)
> + return NULL;
> +
> p = pcomp[0].str = rpath;
> for (; p; p = slash, i++) {
> slash = strchr(p, '/');
> @@ -114,12 +118,13 @@
>
> path = pcomp_construct(pcomp, comps);
> free(rpath);
> + free(pcomp);
> return path;
> }
>
> char *nspath_absolute(const char *rel_path, const char *base)
> {
> - char cwd[4096];
> + char cwd[PATH_MAX];
> int len;
> char *path = NULL, *normpath;
>
> @@ -127,7 +132,7 @@
> return nspath_normalize(rel_path);
>
> if (!base) {
> - if (getcwd(cwd, sizeof(cwd) - 1) < 0)
> + if (getcwd(cwd, sizeof(cwd)) == NULL)
> return NULL;
> base = cwd;
> }
>
> ------------------------------------------------------------------------------
> The Windows 8 Center
> In partnership with Sourceforge
> Your idea - your app - 30 days. Get started!
> http://windows8center.sourceforge.net/
> what-html-developers-need-to-know-about-coding-windows-8-metro-style-apps/
> _______________________________________________
> Nagios-devel mailing list
> Nagios-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/nagios-devel
>


--
Andreas Ericsson andreas.ericsson@op5.se
OP5 AB www.op5.se
Tel: +46 8-230225 Fax: +46 8-230231

Considering the successes of the wars on alcohol, poverty, drugs and
terror, I think we should give some serious thought to declaring war
on peace.





This post was automatically imported from historical nagios-devel mailing list archives
Original poster: ae@op5.se
Guest
 

Return to Open Source Nagios Projects

Who is online

Users browsing this forum: No registered users and 10 guests