Project

General

Profile

Actions

Bug #5763

closed

sprintf not throwing error for wrong number of arguments

Added by chaitanyav (NagaChaitanya Vellanki) over 12 years ago. Updated over 12 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
-
Backport:
[ruby-core:41656]

Description

sprintf should raise error when the number of arguments does not match the number of format specifiers

sprintf with correct number of format specifiers, arguments

sprintf('%s', 'hello')
=> "hello"

sprintf with a single format specifier and two arguments

sprintf('%s', 'hello', 'world!')
=> "hello"

sprintf with three format specifiers and two arguments

sprintf('%s-%s-%s', 'hello', 'world!')
ArgumentError: too few arguments
from (irb):3:in sprintf' from (irb):3 from /home/chaitanyav/development/tools_64/bin/irb:12:in '


Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #2709: $VERBOSE, $DEBUG and Kernel#sprintfRejectedshyouhei (Shyouhei Urabe)Actions

Updated by chaitanyav (NagaChaitanya Vellanki) over 12 years ago

check for similar behaviour of sprintf in a c program

#include <stdio.h>

int main()
{
char buffer[50];
char *p = buffer;
sprintf(buffer, "%d", 1, 2, 3);
printf("%s", p);
return 0;
}

gcc test_str.c

$ gcc -Wall test_str.c [13:03:36]
test_str.c: In function main:
test_str.c:7:3: warning: too many arguments for format [-Wformat-extra-args]

Updated by shyouhei (Shyouhei Urabe) over 12 years ago

check for similar behaviour of sprintf in a c program

This is a GCCism, not a universal C behaviour. If you think sprintf should warn because C does, that is a false assertion.

Updated by kosaki (Motohiro KOSAKI) over 12 years ago

  • ruby -v changed from ruby -v [12:11:28] ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux] to -

check for similar behaviour of sprintf in a c program

This is a GCCism, not a universal C behaviour.  If you think sprintf should warn because C does, that is a false assertion.

But I can't imagine any useful usecase of current behavior. And Java's
printf throw IllegalFormatException in this case. So, I think this has
a enough worth of discussion.

Updated by shyouhei (Shyouhei Urabe) over 12 years ago

Motohiro KOSAKI wrote:

But I can't imagine any useful usecase of current behavior.

Then please remember #2709.
http://bugs.ruby-lang.org/issues/show/2709

note: I'm neither for nor against it.

Updated by chaitanyav (NagaChaitanya Vellanki) over 12 years ago

Shyouhei Urabe wrote:

check for similar behaviour of sprintf in a c program

This is a GCCism, not a universal C behaviour. If you think sprintf should warn because C does, that is a false assertion.

I encountered this issue today, i was missing a format specifier in my code and it took sometime to figure it out. It would be nice if there was a warning/error this case.

Updated by nobu (Nobuyoshi Nakada) over 12 years ago

  • Status changed from Open to Closed

=begin
$ ruby -w -e 'printf "%d\n", 1, 2'
-e:1: warning: too many arguments for format string
1
=end

Updated by kosaki (Motohiro KOSAKI) over 12 years ago

Motohiro KOSAKI wrote:

 But I can't imagine any useful usecase of current behavior.

Then please remember #2709.
http://bugs.ruby-lang.org/issues/show/2709

Oh, I didn't know it. Thank you. I'll withdraw my claim then.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0