302

I was wondering about the copyright of code snippets from answers. Let's say you ask a question and the solution consists of ten lines of code, which are given in an answer. Are there any restrictions about the use of that code?

It seems that the content of this page is under the creative commons license. So does one have to attribute the author if code is used in an open source project? Can it be legally used in closed source projects at all?

I guess ten lines of code are not really problematic, but is there a critical number of lines?

Return to FAQ index

6
  • 3
    See my answer for a way to work around this.
    – waffles
    Commented May 22, 2009 at 7:53
  • 1
    possible duplicate of What is up with the source code license on Stack Overflow?
    – lofidevops
    Commented Sep 9, 2014 at 13:59
  • 4
    @d3vid: this is the central FAQ; if there is any dupe closing it'll have to be in the other direction. Commented Sep 9, 2014 at 14:35
  • 3
    Strictly talking about GPL software, see Sharing computer code between GPL software and CC-BY-SA communities.
    – jweyrich
    Commented Sep 2, 2015 at 17:46
  • If you consider Stack as a "public space" and code as a form of art taking a photo of it (or recalling it from memory) should be fine—whereas a direct copy would require attribution under the current license.
    – vhs
    Commented Feb 16, 2019 at 11:20
  • You may not use it in the Linux kernel source tree, for example. I.o.w. it depends to the acceptance of the open source project in mind.
    – 0andriy
    Commented Sep 22, 2022 at 17:54

6 Answers 6

68

Anything that you post to Stack Overflow will be under the terms of the Creative Commons BY-SA Attribution-ShareAlike license. Depending on when you posted, a different version of the license will be used. You can find more information in the Stack Overflow terms of service.

As far as more specific legal issues, you should probably consult an attorney.

10
  • 56
    So basically if I paste a 10-line snippet of code from stackoverflow into my project, I must now distribute my project onder CC by-sa as well?
    – davr
    Commented Sep 24, 2008 at 17:54
  • 1
    If you include a code snippet from another source, you can't arbitrarily for that content to Creative Commons ... it stays under it's original license until the copyright holder says otherwise.
    – Steve Moyer
    Commented Sep 24, 2008 at 17:55
  • 29
    Nothing here changes who owned the code before it was posted. If the poster didn't have the right to post it (because of his company's IP provisions, or because it was taken from elsewhere) the CC license doesn't stop the original owner from claiming infringement against someone who downloads.
    – Will M
    Commented Sep 24, 2008 at 18:03
  • 3
    @Davr - If you plan on releasing the code under a license that would be incompatible with the creative commons license, I wouldn't post it to StackOverflow. I would imagine that this wouldn't be a problem for most FOSS licenses. And if this is proprietary code, I wouldn't post it to begin with. :) Commented Sep 24, 2008 at 18:24
  • 65
    No...I'm talking about the other way around. I ask "How do I do Foo?", then someone answers with some source code, presumably licensed under CC by-sa. What happens when I take that source code, and add it to my non-opensource application?
    – davr
    Commented Sep 24, 2008 at 20:28
  • 7
    I modified a code I found on stackoverflow and add it to my program (which I intend to release under GPL). I mentioned in comments the person who wrote it and added the url to the topic on stackoverflow where I found it. Do I need to do anything else?
    – Adinan
    Commented Nov 10, 2013 at 15:43
  • 1
    @Jason: Realizing this wasn't Davr's question, I think your earlier comment missed something. IANAL, but afaict you can post a portion of closed source to SO and "dual license" (since it's all yours). If I have closed foo, sublicense foo to you, your posting foo to SO doesn't stop me from continuing to treat foo-superset as otherwise closed. Same if that "sublicensee" is myself. You can use foo on SO under CC, and I can use foo-superset as closed without suing myself. ;^) See MySql and dual licensing.
    – ruffin
    Commented Nov 30, 2013 at 17:12
  • 7
    @JasonBaker I would imagine that this wouldn't be a problem for most FOSS licenses. WRONG! From Creative Commons > Compatible Licenses, under "BY-SA" section "Version 3.0": Currently, no non-CC licenses have been designated as compatible with BY-SA 3.0. So you are effectively forbidden to use code posted by other/yourself on Stack Exchange and any sub-site in any projects unless it is licensed under and only under CC BY-SA 3.0 (because no other license is compatible).
    – zypA13510
    Commented Mar 31, 2018 at 20:50
  • 5
    Sorry, this does not really answer the questions and is more of an link-answer.
    – keuleJ
    Commented Sep 12, 2018 at 11:38
  • 1
    @jeyko While I understand the edit, it's not strictly speaking correct. The version of CC BY-SA is dependent on when the content was posted, it's not necessarily 3.0 Commented Jul 1, 2023 at 14:53
150

This is a software licensing nightmare. If you paste in snippets in your answer that are in an incompatible license to cc-wiki, you are violating the terms of SO. I would imagine there are probably thousands of examples of such violations.

In addition to that major issue, I personally like to choose how to license my work. I am happy for all the text to be the viral cc-wiki. But am very unhappy to have my little code samples or classes under cc-wiki.

To combat this issue I just added this snippet to my profile on SO

All original source snippets I post on stackoverflow are dedicated to the public domain. Do with them as you see fit.

All those who are against the viral nature of the source samples should explicitly say what license their samples are in.

Another option is to post any larger snippets to sites like gist and have a license in the header of the file.

16
  • 27
    The "Public Domain" is not a license. Licenses are a type of contracts. The Public Domain is the set of works that fall outside of copyright, for whatever reason (eg too old, not creative, explicitly put there). Even so, your intent is clear enough to put your code in it.
    – MSalters
    Commented May 22, 2009 at 8:10
  • 50
    "Public domain" does not exist in many countries, so if you want everybody to be able to use your code snippets without worries, consider using CC0 (if you like Creative Commons licences), WTFPL, Unlicence, or the like. Commented Nov 30, 2011 at 21:08
  • 2
    This is much more helpful than the accepted answer, but for such a re-dedication to be meaningful, it really needs to be a license in itself (as @ChrisJester-Young noted). The license closest to the public domain is the Unlicense.
    – bukzor
    Commented Mar 10, 2014 at 18:25
  • Thanks! I've added this statement to my profile as well, including the Unlicense specifically.
    – Chris
    Commented May 5, 2014 at 20:37
  • 3
    @ChrisJester-Young, Is WTFPL a real copyright that actually works in court?
    – Pacerier
    Commented Jun 17, 2014 at 16:16
  • 1
    @Pacerier (I'm not a lawyer.) The only free software licence that I'm aware of having been tested in court is GPL. The only time WTFPL will get tested in court is if a copyright holder reneges on the licence and tries to sue "after the fact", since technically there is no way for the licensee to violate the licence. I have not heard of such a case. Commented Jun 17, 2014 at 17:03
  • 2
    @ChrisJester-Young, So shouldn't we instead opt for GPL rather than Unlicense and friends?
    – Pacerier
    Commented Jun 17, 2014 at 19:03
  • 10
    @Pacerier No, because here, our intent (as copyright holders) is to enable everyone to do anything they want with our code snippets. Using the GPL won't achieve that. CC0, Unlicence, and WTFPL are supposed to be "as close to public domain as possible" licences. The GPL is not such a licence. Commented Jun 17, 2014 at 19:05
  • @ChrisJester-Young, Actually, effectively wouldn't WTFPL and Unlicence actually allow other people to start claiming that they created our work?
    – Pacerier
    Commented Jun 18, 2014 at 18:55
  • 3
    @Pacerier nope. In many countries, attribution, integrity and a few other such rights can not be changed or given away or such.
    – chx
    Commented Oct 12, 2014 at 0:26
  • @chx, However no one knows who is speaking the truth. They can claim that it is theirs and there's no way to counter-prove that it belongs to me.
    – Pacerier
    Commented Oct 12, 2014 at 5:45
  • 4
    That snippet only has value until someone comes along and edits your question, then the license goes back to cc-wiki again. Commented Jun 8, 2016 at 16:14
  • 2
    I would also make sure that you phrase that this is an addition to the Creative Commons "default" of stackoverflow. If you present your signature as voiding instead of expanding upon the stackoverflow terms of use, you may not have a legal footing.
    – Tormod
    Commented Jan 15, 2018 at 10:19
  • 1
    You might want to write that on all Stack Exchange profiles you have then: meta.stackexchange.com/questions/333089/… - all Stack Exchange websites use the same license. Very cool idea and I'm putting it on all my profiles now (with the WTFPL license though).
    – Edw590
    Commented Dec 23, 2020 at 21:32
  • 1
    Or actually, CC0, as WTFPL and Unlicense might have problems with some parts. But I don't understand enough of that, so I'll just go with this on Wikipedia: "The Free Software Foundation and the Open Knowledge Foundation approved CC0 as a recommended license to dedicate content to the public domain. The FSF and the Open Source Initiative, however, do not recommend the usage of this license for software due to inclusion of a clause expressly stating it does not grant patent licenses.". So CC0 it is for SE websites. Just putting this here for licenses newbies like me to be warned about it.
    – Edw590
    Commented Dec 24, 2020 at 1:32
82

If you are talking about taking code from Stack Overflow for use in your work, I would be very careful. Even if it is the opinion of the people here that posts to Stack Overflow are covered by Creative Commons, you will need to be able to prove that you got the code from here.

A short story to illustrate:

  • Someone else decides the code is a good solution for a problem they are facing and decides to copy it into their open source GnuFizzBuzz project, covered by the GPL.
  • Five years from now, someone is doing an open-source audit on your code (maybe you are selling to a nervous customer, or your company is going public).
  • The open-source audit finds the snippet of code you got from Stack Overflow and recognizes it as originating in GnuFizzBuzz.
  • You are now stuck explaining how/why you have GPL code mixed into your commercial product or proving that the code was actually from Stack Overflow. Can you prove that the person posting the code to Stack Overflow owned it to begin with (maybe they actually copied it from GnuFizzBuzz to begin with).

It may sound unlikely, but I have seen this exact situation with my own eyes. The safe thing to do is to regard any code snippets you find as explanations of how something could work. Read, understand, and write a solution based on your understanding. Don't ever cut/paste code you found on the Internet if you are working on a commercial product.

5
  • 38
    ...so, if including code from SO, include the URL in a comment.
    – msh210
    Commented Dec 19, 2011 at 21:30
  • 2
    If you rely on specific code (and to which copyright applies), contact the author. CC-WIKI is not a software license and you want to have usage terms more clear than the CC-WIKI license is able to specify it.
    – hakre
    Commented Jan 30, 2012 at 15:05
  • 5
    In this answer (by Jeff Atwood actually) it's written that "a snippet of code falls under excerpt category and thus should be free to use under fair use" — but when is a snippet too long to be a snippet?
    – KajMagnus
    Commented Feb 22, 2013 at 11:00
  • 22
    @KajMagnus: That's a legal judgement, and it's wrong. Fair use doctrine does not apply to embedding excerpts of copyrighted work into source code.
    – bukzor
    Commented Mar 10, 2014 at 18:44
  • Would be interesting to either be able to choose the license to have content under, or to be able to have a signature option which would say something like, in my case, "All things originally from me posted on any Stack Exchange website are under the following license: [license name/link/whatever], not the default one.". I can put it on my profile, but will be lucky for anyone to go there check about it. Unless I manually write it, which I think it's weird, but maybe it's better than having it under the wrong license?
    – Edw590
    Commented Dec 24, 2020 at 20:19
32

This has been discussed on UserVoice already. Jeff closed the request with this response:

I would hope that people are not posting giant blocks of code at SO, making it more of a quote / fair use type situation.

For commercial work, you should see your company lawyer before using any code from SO.

6
  • 1
    Thanks for the uservoice reference, seems that this is as clear as it gets.
    – nikow
    Commented Sep 30, 2008 at 15:36
  • 12
    Its clear, but as the owner of a copyright you have the right to license your work under whichever license you see fit. (in addition to the cc-wiki on this site) I attached a disclaimer to my profile saying that I am giving away my snippets. I hope others follow suite
    – waffles
    Commented May 22, 2009 at 7:54
  • 2
    The only trouble is that there are legislations which unfortunately do not have a concept of "fair use". But I also agree that such small snippets will probably cause no problem, since all implementations of "standard" problems tend to look alike. I also want to hint to this CC FAQ about the use of CC licenses for software.
    – quazgar
    Commented May 23, 2013 at 11:26
  • 10
    Considering the Code Review site, where long/giant blocks of code is the norm, I'd say that Jeff's answer is quite outdated. Commented Dec 19, 2013 at 1:45
  • 3
    @SimonAndréForsberg, Exactly, we even have Stack Snippets now which indirectly encourages longer code.
    – Pacerier
    Commented Apr 12, 2015 at 12:16
  • 1
    A lot of people don't realise Fair Use is a USA doctrine codified in the USC, it is not a universal doctrine. In the UK we have Fair Dealing, which is very restrictive - for example not allowing backups or format shifting (despite the latter being briefly allowed in the past). IANAL, in my private opinion if a snippet of code is useful it couldn't be copied under Fair Dealing except perhaps as part of a lesson in a registered educational establishment, say.
    – pbhj
    Commented Aug 16 at 14:41
7

Only a lawyer can answer this question. But, very relevant are fair use and whether a small snippet of code is copyrightable.

Also, the implicit license it was posted under is relevant when asking a lawyer their opinion. A lawyer might say that it would be difficult for someone to reasonably argue that their copyright was violated when code they post in response to a question is used in any way. Legal concepts such as promissory estoppel might come into play. It would also be interesting to see if reputation points could be deemed "consideration" under the law.

Patent issues are a much bigger risk, because the patent law is so nonsensical and keeps getting worse. Software patents are very dangerous and have already caused great damage to the software industry and free software.

Also, export restrictions (e.g. ITAR) are very dangerous, although some important precedents protecting source code as speech have been set. There was a time when simply having hooks to process data before sending it on its way was restricted as a munition in the US.

1
  • 8
    There is no implicit license. There's an explicit one - CC-BY-SA. Commented Oct 15, 2009 at 21:37
6

I am not a lawyer, but I think if you do a direct cut and paste of the code then you are going to have to look at what the site's cc-wiki gives you permission to do. Personally, I think that if you read the code, understand the code, and then use that knowledge to write you own take on what the code is doing you might skirt by the copyright issue as you aren't doing a cut and paste of the code.

However, it is worth noting that you do have to be aware that there might be a patent on the code in which case you have to be aware of those issues. That is an area were you are starting to look at having a lawyer render an option on it - if you work at a company then someone in the legal department might be able to give you an answer on the quesiton.

2
  • This is a much more pragmatic answer than the others. I think sometimes the way to go is to understand the algorithm/methodology, check several similar examples and write your own. "Copying from one person is plagiarism. Copying from many is research." Commented Aug 18, 2023 at 1:50
  • 1
    @JardelLucca And for OpenAI, copying from everyone is... something nobody has done before, but we'll let the legal team sort out the ramifications later. (I'm not justifying nor condemning, I just think it's interesting.)
    – Mentalist
    Commented Jun 26 at 1:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .