# [PATCH] gitweb: Remove "uninitialized value" Perl warning

2 messages from 2016-01-12 to 2016-01-12. Participants: Øyvind A. Holm, Junio C Hamano.
Thread: https://gitlist.dev/t/41170

## Øyvind A. Holm, 2016-01-12 03:31

Subject: [PATCH] gitweb: Remove "uninitialized value" Perl warning
Message-ID: <1452569516-25657-1-git-send-email-sunny@sunbase.org>
URL: https://gitlist.dev/e/1452569516-25657-1-git-send-email-sunny%40sunbase.org

```
git_object(): Check if $type is defined before chomping it. This removes
a Perl warning in the server error log:

  gitweb.cgi: Use of uninitialized value $type in scalar chomp at
  [...]/gitweb.cgi line 7579., referer: [...]

when trying to access a non-existing commit, for example:

  http://HOST/?p=PROJECT.git;a=commit;h=NON_EXISTING_COMMIT

Signed-off-by: Øyvind A. Holm <sunny@sunbase.org>
---
 gitweb/gitweb.perl | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index 7a5b23a..05d7910 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -7576,7 +7576,7 @@ sub git_object {
 			git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
 			or die_error(404, "Object does not exist");
 		$type = <$fd>;
-		chomp $type;
+		defined $type && chomp $type;
 		close $fd
 			or die_error(404, "Object does not exist");
 
-- 
2.7.0

```

## Junio C Hamano, 2016-01-12 21:20

Subject: Re: [PATCH] gitweb: Remove "uninitialized value" Perl warning
Message-ID: <CAPc5daVYHKehuDV7hFB9npw9qYt24jLTrnQr1LxDPdHQjWpkYQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAPc5daVYHKehuDV7hFB9npw9qYt24jLTrnQr1LxDPdHQjWpkYQ%40mail.gmail.com
In-Reply-To: <xmqq4meitsce.fsf@gitster.mtv.corp.google.com>

```
On Tue, Jan 12, 2016 at 1:12 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Øyvind A. Holm <sunny@sunbase.org> writes:
>
>> git_object(): Check if $type is defined before chomping it. This removes
>> a Perl warning in the server error log:
>>
>>   gitweb.cgi: Use of uninitialized value $type in scalar chomp at
>>   [...]/gitweb.cgi line 7579., referer: [...]
>>
>> when trying to access a non-existing commit, for example:
>>
>>   http://HOST/?p=PROJECT.git;a=commit;h=NON_EXISTING_COMMIT
>
> Thanks.  The analysis and description of the issue and the fix both
> make sense to me.

With this, you would then do

 print $cgi->redirect(-uri => href(action=>$type, ...);

but then href treats $param{action} that is an undef as if it does not
even exist,
so the effect will not be felt during the invocation of this request.

I am not sure what happens to a request that lacks action (mapped to 'a'
parameter) that results from this redirect, though.  Would that eventually
hit dispatch where if (!defined $action) would cause it to say "Object does
not exist"?

In any case, this looks like a strict improvement. Thanks again.

>> Signed-off-by: Øyvind A. Holm <sunny@sunbase.org>
>> ---
>>  gitweb/gitweb.perl | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
>> index 7a5b23a..05d7910 100755
>> --- a/gitweb/gitweb.perl
>> +++ b/gitweb/gitweb.perl
>> @@ -7576,7 +7576,7 @@ sub git_object {
>>                       git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
>>                       or die_error(404, "Object does not exist");
>>               $type = <$fd>;
>> -             chomp $type;
>> +             defined $type && chomp $type;
>>               close $fd
>>                       or die_error(404, "Object does not exist");
>

```
