oracle / oracle/dtrace

printf %s does not allow char * argument

Open
#112 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
189
Forks
23
PR merge metrics
No merged PRs in 30d

Description

The printf action's %s (and %S) conversion accepts an array of char or a string as argument, but not char*. This is documented as such, but there is not really any good technical reason I can see why that would be the case. The documentation says:

The argument must be an array of char or a string. Bytes from the array or string are read up to a terminating null character or the end of the data and interpreted and printed as ASCII characters. If the precision is not specified, it is taken to be infinite so that all characters up to the first null character are printed. If the precision is specified, only the portion of the character array that is displayed in the corresponding number of screen columns is printed. If an argument of type char * is to be formatted, it should be cast to string or prefixed with the D stringof operator to indicate that DTrace should trace the bytes of the string and format them.

But if %s is specified for the argument then it is abundantly clear that the intent is to have the char * argument formatted as a string in the output. It would therefore make sense to do an automatic promotion to string rather than insisting on an explicit (string) cats or using the stringof() operator. In general, D does consider char * to be compatible with string, so why not here?

From the docs:

You can also assign to a string variable an expression of a type that is compatible with strings. In this case, the D compiler automatically promotes the source expression to the string type and performs a string assignment. The D compiler permits any expression of type char * or of type char[n], that is, a scalar array of char of any size, to be promoted to a string.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the D compiler's printf action and its %s/%S conversion handling, then trace how char * is treated versus char[n] and string. Review the documented promotion rules and verify the behavior with char * arguments; done means both conversions format char * without an explicit cast or stringof().

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.