Ошибка no argument for format

Sergey Shved

Guest

2003-11-07 14:31

WinSCP3.4.*

When I mark a few files and try to run Copy or Move commands, I get diagnostic message » Error: No argument for format «%s» «.

Reply with quote

Advertisement

martin◆

Site Admin
martin avatar
Joined:
2002-12-10
Posts:
38,957
Location:
Prague, Czechia

2003-11-07

I would bet that you use translated version of WinSCP. Am I right? It this case I need to know the language. If I am not right than I need additional information too. You can probably imagin that, if it were imposible to copy any files as your post suggest, someone would have already noticed (probably even me). So I suppose that there must be some special conditions.

Reply with quote

Sergey Shved

Guest

2003-11-08 19:00

You are right, I use international version of WinSCP with Russian user interface.

There are no errors with single file operations.

Reply with quote

martin◆

Site Admin
martin avatar

2003-11-10

So please switch back to english (Languages button on login dialog). It should work then. I’ll post here notice, when corrected russian translation is released.

Reply with quote

Advertisement

I want to remove the warning that i get on this line of the code,

FILE *fil;
char *imp;
(...)
fprintf(fil,imp);

the thing is when i do this it writes on the file exactly what i want, but if i apply the format %s it doesn’t, like this

fprintf(fil, "%s", imp);

James McNellis's user avatar

asked Dec 11, 2010 at 22:20

Unzi's user avatar

6

This warning is gcc’s way of telling you that it cannot verify the format string argument to the printf style function (printf, fprintf… etc). This warning is generated when the compiler can’t manually peek into the string and ensure that everything will go as you intend during runtime. Lets look at a couple of examples.

Case 1. This string can be verified at compile time and the compiler will allow it without warning:

printf("This string has no format");

Case 2: For this case, the compiler can detect that you have a format specifier and will raise a different warning. On my machine it said «warning: too few arguments for format».

// This will most probably crash your machine
printf("Not a safe string to %s"); 

Case 3. Now this is somewhat your case. You are taking a string generated at runtime and trying to print it. The warning you are getting is the compiler warning you that there could be a format specifier in the string. Say for eg «bad%sdata». In this case, the runtime will try to access a non-existent argument to match the %s. Even worse, this could be a user trying to exploit your program (causing it to read data that is not safe to read).

char str[200];
scanf("%s", str)
printf(str)

D.Shawley's user avatar

D.Shawley

58k10 gold badges98 silver badges113 bronze badges

answered Dec 11, 2010 at 22:37

Sonny Saluja's user avatar

Sonny SalujaSonny Saluja

7,1732 gold badges25 silver badges39 bronze badges

7

While technically there’s nothing wrong with calling a printf-like function with a string, it is still bad practice because the string may contain format tokens like %s. If imp is %s test for example, bad things will happen.

If you just want to print the imp without formatting, you should use fputs(imp, fil) (note the reversed arguments).

Community's user avatar

answered Dec 11, 2010 at 22:27

terminus's user avatar

terminusterminus

13.6k8 gold badges34 silver badges37 bronze badges

3

I think the accepted answer explained it very well. Basically, as the documentation also indicates, the compiler can not guarantee that the string variable (in this case imp) is a string literal. You may disable this warning if you are not concerened with safety by puting

#ifdef _WIN32
#pragma warning (disable : 4774)
#endif

in the header of your code or in the CMake:

if (CMAKE_C_COMPILER_ID STREQUAL "MSVC")
  set(CMAKE_C_FLAGS "/wd4774") 
endif()

answered Mar 24, 2020 at 0:41

Foad S. Farimani's user avatar

Foad S. FarimaniFoad S. Farimani

12.2k15 gold badges74 silver badges187 bronze badges

1

This guide will walk you through the process of resolving the common ‘Format Not a String Literal and No Format Arguments’ errors encountered in programming languages like C and Python. We will provide step-by-step instructions and examples to help you understand the issue and fix it in your code.

Table of Contents

  1. Understanding ‘Format Not a String Literal and No Format Arguments’ Errors
  2. How to Fix the Error in C
  3. How to Fix the Error in Python
  4. Frequently Asked Questions (FAQ)

Understanding ‘Format Not a String Literal and No Format Arguments’ Errors

‘Format Not a String Literal and No Format Arguments’ is a warning usually generated by the compiler or interpreter when a format string is passed as an argument to a function like printf() in C or format() in Python without any format specifiers or arguments. This warning is raised to alert you to potential security vulnerabilities or bugs in your code, as it may be susceptible to format string attacks.

For example, consider the following code snippet in C:

#include <stdio.h>

int main() {
    char *name = "John";
    printf(name);
    return 0;
}

In this example, the printf() function is given a variable name as its argument. Since name is not a string literal and there are no format arguments, the compiler will generate a warning.

How to Fix the Error in C

To fix the ‘Format Not a String Literal and No Format Arguments’ error in C, you need to provide a proper format string with the appropriate format specifiers and arguments. In our example, you can fix the error by modifying the printf() function as follows:

#include <stdio.h>

int main() {
    char *name = "John";
    printf("%sn", name);
    return 0;
}

By adding the %s format specifier and passing name as an argument, we have resolved the error. The %s specifier tells the printf() function to expect a string argument, which is provided by name.

How to Fix the Error in Python

In Python, the ‘Format Not a String Literal and No Format Arguments’ error can occur when using the format() function. For example, consider the following code snippet:

name = "John"
print("Hello, {}".format)

In this example, the format() function is missing the required arguments. To fix this error, you need to provide the appropriate arguments to the format() function like this:

name = "John"
print("Hello, {}".format(name))

By passing name as an argument to the format() function, we have resolved the error.

Frequently Asked Questions (FAQ)

What is a format string?

A format string is a string that contains placeholders, called format specifiers, which are replaced by the values of corresponding arguments when the string is printed or otherwise formatted. Format strings are used in functions like printf() in C and format() in Python to create formatted output.

Why do I need to use format specifiers?

Format specifiers help you control the appearance of your formatted output, such as the number of decimal places for floating-point numbers or the alignment of text in columns. They also provide type safety by ensuring that the arguments you pass to a function match the expected data types.

What are some common format specifiers?

Some common format specifiers include %s for strings, %d or %i for integers, and %f for floating-point numbers in C. In Python, you can use {} as a placeholder and provide additional formatting options inside the braces, like {:d} for integers or {:f} for floating-point numbers.

How can I avoid format string vulnerabilities?

To avoid format string vulnerabilities, always use a proper format string with the appropriate format specifiers and arguments. Never pass user-controlled data directly to a formatting function without validating and sanitizing it first. Additionally, consider using functions like snprintf() in C or f-strings in Python, which offer better safety and performance.

What is a format string attack?

A format string attack is a type of security vulnerability that occurs when an attacker exploits the lack of proper format strings in a program to execute arbitrary code, read sensitive data, or cause a denial of service. This is typically achieved by providing malicious input that includes format specifiers, which cause the program to read or write memory locations specified by the attacker.

Learn more about format string vulnerabilities and how to prevent them

  • C Programming: Format Specifiers
  • Python String Formatting: Best Practices
  • Understanding and Preventing Format String Vulnerabilities

You should use fputs(Usage, stderr);

There is no need to use fprintf if you arn’t doing formatting. If you want to use fprintf, use fprintf(stderr, "%s", Usage);

The default compiler flags on Ubuntu includes -Wformat -Wformat-security which is what gives this error.

That flag is used as a precaution against introducing security related bugs, imagine what
would happen if you somehow did this:

char *Usage = "Usage %s, [options] ... ";
...
fprintf(stderr, Usage);

This would be the same as
fprintf(stderr, "Usage %s, [options] ... ]"); which is wrong.

Now the Usage string includes a format specifier, %s, but you do not provide that argument to fprintf, resulting in undefined behavior, possibly crashing your program or allowing it to be exploited. This is more relevant if the string you pass to fprintf comes from user input.

But if you do fprintf(stderr,"%s", "Usage %s, [options] ... ]"); There is no such problem. The 2. %s will not be interpreted as a format specifer.
gcc can warn about this, and the default Ubuntu compiler flags makes it a compiler error.

  • Remove From My Forums

 locked

No argument for format ‘%s’ — Toad for Oracle 10.1

  • Question

  • Hi,

    I tried to sequence Toad for oracle 10.1, When launching the shortcut, the below  errors.are coming

    «No argument for format ‘%s’.»

    followed by

    «Toad has encountered an unexpected error. Be sure that you have full read and write access to you user profile directory and to HKEY_CURRENT_USERSoftware.»

    I m using App-V  sequencer 4.5.0.1485 in Windows Vista.

    I tried to sequence Oracle Instant client 10.2.0.1.0 also along with toad 10.1. But no luck… :(

    Getting the same error.

    any resolution???

    Thanks in advance…

    Velmurugan.

Answers

  • Hello,

    A more specific blog-entry;

    • Proposed as answer by

      Monday, July 19, 2010 9:00 PM

    • Marked as answer by
      Aaron.ParkerModerator
      Sunday, January 8, 2012 10:18 PM

Понравилась статья? Поделить с друзьями:
  • Ошибка no any devices exist
  • Ошибка no any device exists что это
  • Ошибка no any device exist
  • Ошибка no amd graphics driver is installed что делать
  • Ошибка no address associated with hostname